• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/service/service_process.h"
6 
7 #include <algorithm>
8 
9 #include "base/basictypes.h"
10 #include "base/callback.h"
11 #include "base/command_line.h"
12 #include "base/environment.h"
13 #include "base/i18n/rtl.h"
14 #include "base/memory/singleton.h"
15 #include "base/path_service.h"
16 #include "base/prefs/json_pref_store.h"
17 #include "base/strings/string16.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/values.h"
20 #include "chrome/common/chrome_constants.h"
21 #include "chrome/common/chrome_paths.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/common/env_vars.h"
24 #include "chrome/common/pref_names.h"
25 #include "chrome/common/service_process_util.h"
26 #include "chrome/service/cloud_print/cloud_print_proxy.h"
27 #include "chrome/service/net/service_url_request_context.h"
28 #include "chrome/service/service_ipc_server.h"
29 #include "chrome/service/service_process_prefs.h"
30 #include "grit/chromium_strings.h"
31 #include "grit/generated_resources.h"
32 #include "net/base/network_change_notifier.h"
33 #include "net/url_request/url_fetcher.h"
34 #include "ui/base/l10n/l10n_util.h"
35 #include "ui/base/resource/resource_bundle.h"
36 #include "ui/base/ui_base_switches.h"
37 
38 #if defined(USE_GLIB)
39 #include <glib-object.h>
40 #endif
41 
42 ServiceProcess* g_service_process = NULL;
43 
44 namespace {
45 
46 // Delay in seconds after the last service is disabled before we attempt
47 // a shutdown.
48 const int kShutdownDelaySeconds = 60;
49 
50 // Delay in hours between launching a browser process to check the
51 // policy for us.
52 const int64 kPolicyCheckDelayHours = 8;
53 
54 const char kDefaultServiceProcessLocale[] = "en-US";
55 
56 class ServiceIOThread : public base::Thread {
57  public:
58   explicit ServiceIOThread(const char* name);
59   virtual ~ServiceIOThread();
60 
61  protected:
62   virtual void CleanUp() OVERRIDE;
63 
64  private:
65   DISALLOW_COPY_AND_ASSIGN(ServiceIOThread);
66 };
67 
ServiceIOThread(const char * name)68 ServiceIOThread::ServiceIOThread(const char* name) : base::Thread(name) {}
~ServiceIOThread()69 ServiceIOThread::~ServiceIOThread() {
70   Stop();
71 }
72 
CleanUp()73 void ServiceIOThread::CleanUp() {
74   net::URLFetcher::CancelAll();
75 }
76 
77 // Prepares the localized strings that are going to be displayed to
78 // the user if the service process dies. These strings are stored in the
79 // environment block so they are accessible in the early stages of the
80 // chrome executable's lifetime.
PrepareRestartOnCrashEnviroment(const CommandLine & parsed_command_line)81 void PrepareRestartOnCrashEnviroment(
82     const CommandLine &parsed_command_line) {
83   scoped_ptr<base::Environment> env(base::Environment::Create());
84   // Clear this var so child processes don't show the dialog by default.
85   env->UnSetVar(env_vars::kShowRestart);
86 
87   // For non-interactive tests we don't restart on crash.
88   if (env->HasVar(env_vars::kHeadless))
89     return;
90 
91   // If the known command-line test options are used we don't create the
92   // environment block which means we don't get the restart dialog.
93   if (parsed_command_line.HasSwitch(switches::kNoErrorDialogs))
94     return;
95 
96   // The encoding we use for the info is "title|context|direction" where
97   // direction is either env_vars::kRtlLocale or env_vars::kLtrLocale depending
98   // on the current locale.
99   base::string16 dlg_strings(
100       l10n_util::GetStringUTF16(IDS_CRASH_RECOVERY_TITLE));
101   dlg_strings.push_back('|');
102   base::string16 adjusted_string(l10n_util::GetStringFUTF16(
103       IDS_SERVICE_CRASH_RECOVERY_CONTENT,
104       l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT)));
105   base::i18n::AdjustStringForLocaleDirection(&adjusted_string);
106   dlg_strings.append(adjusted_string);
107   dlg_strings.push_back('|');
108   dlg_strings.append(base::ASCIIToUTF16(
109       base::i18n::IsRTL() ? env_vars::kRtlLocale : env_vars::kLtrLocale));
110 
111   env->SetVar(env_vars::kRestartInfo, base::UTF16ToUTF8(dlg_strings));
112 }
113 
114 }  // namespace
115 
ServiceProcess()116 ServiceProcess::ServiceProcess()
117   : shutdown_event_(true, false),
118     main_message_loop_(NULL),
119     enabled_services_(0),
120     update_available_(false) {
121   DCHECK(!g_service_process);
122   g_service_process = this;
123 }
124 
Initialize(base::MessageLoopForUI * message_loop,const CommandLine & command_line,ServiceProcessState * state)125 bool ServiceProcess::Initialize(base::MessageLoopForUI* message_loop,
126                                 const CommandLine& command_line,
127                                 ServiceProcessState* state) {
128 #if defined(USE_GLIB)
129   // g_type_init has been deprecated since version 2.35.
130 #if !GLIB_CHECK_VERSION(2, 35, 0)
131   // GLib type system initialization is needed for gconf.
132   g_type_init();
133 #endif
134 #endif // defined(OS_LINUX) || defined(OS_OPENBSD)
135   main_message_loop_ = message_loop;
136   service_process_state_.reset(state);
137   network_change_notifier_.reset(net::NetworkChangeNotifier::Create());
138   base::Thread::Options options;
139   options.message_loop_type = base::MessageLoop::TYPE_IO;
140   io_thread_.reset(new ServiceIOThread("ServiceProcess_IO"));
141   file_thread_.reset(new base::Thread("ServiceProcess_File"));
142   if (!io_thread_->StartWithOptions(options) ||
143       !file_thread_->StartWithOptions(options)) {
144     NOTREACHED();
145     Teardown();
146     return false;
147   }
148   blocking_pool_ = new base::SequencedWorkerPool(3, "ServiceBlocking");
149 
150   request_context_getter_ = new ServiceURLRequestContextGetter();
151 
152   base::FilePath user_data_dir;
153   PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
154   base::FilePath pref_path =
155       user_data_dir.Append(chrome::kServiceStateFileName);
156   service_prefs_.reset(new ServiceProcessPrefs(
157       pref_path,
158       JsonPrefStore::GetTaskRunnerForFile(pref_path, blocking_pool_.get())
159           .get()));
160   service_prefs_->ReadPrefs();
161 
162   // This switch it required to run connector with test gaia.
163   if (command_line.HasSwitch(switches::kIgnoreUrlFetcherCertRequests))
164     net::URLFetcher::SetIgnoreCertificateRequests(true);
165 
166   // Check if a locale override has been specified on the command-line.
167   std::string locale = command_line.GetSwitchValueASCII(switches::kLang);
168   if (!locale.empty()) {
169     service_prefs_->SetString(prefs::kApplicationLocale, locale);
170     service_prefs_->WritePrefs();
171   } else {
172     // If no command-line value was specified, read the last used locale from
173     // the prefs.
174     locale =
175         service_prefs_->GetString(prefs::kApplicationLocale, std::string());
176     // If no locale was specified anywhere, use the default one.
177     if (locale.empty())
178       locale = kDefaultServiceProcessLocale;
179   }
180   ResourceBundle::InitSharedInstanceWithLocale(locale, NULL);
181 
182   PrepareRestartOnCrashEnviroment(command_line);
183 
184   // Enable Cloud Print if needed. First check the command-line.
185   // Then check if the cloud print proxy was previously enabled.
186   if (command_line.HasSwitch(switches::kEnableCloudPrintProxy) ||
187       service_prefs_->GetBoolean(prefs::kCloudPrintProxyEnabled, false)) {
188     GetCloudPrintProxy()->EnableForUser();
189   }
190 
191   VLOG(1) << "Starting Service Process IPC Server";
192   ipc_server_.reset(new ServiceIPCServer(
193       service_process_state_->GetServiceProcessChannel()));
194   ipc_server_->Init();
195 
196   // After the IPC server has started we signal that the service process is
197   // ready.
198   if (!service_process_state_->SignalReady(
199           io_thread_->message_loop_proxy().get(),
200           base::Bind(&ServiceProcess::Terminate, base::Unretained(this)))) {
201     return false;
202   }
203 
204   // See if we need to stay running.
205   ScheduleShutdownCheck();
206 
207   // Occasionally check to see if we need to launch the browser to get the
208   // policy state information.
209   CloudPrintPolicyCheckIfNeeded();
210   return true;
211 }
212 
Teardown()213 bool ServiceProcess::Teardown() {
214   service_prefs_.reset();
215   cloud_print_proxy_.reset();
216 
217   ipc_server_.reset();
218   // Signal this event before shutting down the service process. That way all
219   // background threads can cleanup.
220   shutdown_event_.Signal();
221   io_thread_.reset();
222   file_thread_.reset();
223 
224   if (blocking_pool_.get()) {
225     // The goal is to make it impossible for chrome to 'infinite loop' during
226     // shutdown, but to reasonably expect that all BLOCKING_SHUTDOWN tasks
227     // queued during shutdown get run. There's nothing particularly scientific
228     // about the number chosen.
229     const int kMaxNewShutdownBlockingTasks = 1000;
230     blocking_pool_->Shutdown(kMaxNewShutdownBlockingTasks);
231     blocking_pool_ = NULL;
232   }
233 
234   // The NetworkChangeNotifier must be destroyed after all other threads that
235   // might use it have been shut down.
236   network_change_notifier_.reset();
237 
238   service_process_state_->SignalStopped();
239   return true;
240 }
241 
242 // This method is called when a shutdown command is received from IPC channel
243 // or there was an error in the IPC channel.
Shutdown()244 void ServiceProcess::Shutdown() {
245 #if defined(OS_MACOSX)
246   // On MacOS X the service must be removed from the launchd job list.
247   // http://www.chromium.org/developers/design-documents/service-processes
248   // The best way to do that is to go through the ForceServiceProcessShutdown
249   // path. If it succeeds Terminate() will be called from the handler registered
250   // via service_process_state_->SignalReady().
251   // On failure call Terminate() directly to force the process to actually
252   // terminate.
253   if (!ForceServiceProcessShutdown("", 0)) {
254     Terminate();
255   }
256 #else
257   Terminate();
258 #endif
259 }
260 
Terminate()261 void ServiceProcess::Terminate() {
262   main_message_loop_->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
263 }
264 
HandleClientDisconnect()265 bool ServiceProcess::HandleClientDisconnect() {
266   // If there are no enabled services or if there is an update available
267   // we want to shutdown right away. Else we want to keep listening for
268   // new connections.
269   if (!enabled_services_ || update_available()) {
270     Shutdown();
271     return false;
272   }
273   return true;
274 }
275 
GetCloudPrintProxy()276 cloud_print::CloudPrintProxy* ServiceProcess::GetCloudPrintProxy() {
277   if (!cloud_print_proxy_.get()) {
278     cloud_print_proxy_.reset(new cloud_print::CloudPrintProxy());
279     cloud_print_proxy_->Initialize(service_prefs_.get(), this);
280   }
281   return cloud_print_proxy_.get();
282 }
283 
OnCloudPrintProxyEnabled(bool persist_state)284 void ServiceProcess::OnCloudPrintProxyEnabled(bool persist_state) {
285   if (persist_state) {
286     // Save the preference that we have enabled the cloud print proxy.
287     service_prefs_->SetBoolean(prefs::kCloudPrintProxyEnabled, true);
288     service_prefs_->WritePrefs();
289   }
290   OnServiceEnabled();
291 }
292 
OnCloudPrintProxyDisabled(bool persist_state)293 void ServiceProcess::OnCloudPrintProxyDisabled(bool persist_state) {
294   if (persist_state) {
295     // Save the preference that we have disabled the cloud print proxy.
296     service_prefs_->SetBoolean(prefs::kCloudPrintProxyEnabled, false);
297     service_prefs_->WritePrefs();
298   }
299   OnServiceDisabled();
300 }
301 
302 ServiceURLRequestContextGetter*
GetServiceURLRequestContextGetter()303 ServiceProcess::GetServiceURLRequestContextGetter() {
304   DCHECK(request_context_getter_.get());
305   return request_context_getter_.get();
306 }
307 
OnServiceEnabled()308 void ServiceProcess::OnServiceEnabled() {
309   enabled_services_++;
310   if ((1 == enabled_services_) &&
311       !CommandLine::ForCurrentProcess()->HasSwitch(
312           switches::kNoServiceAutorun)) {
313     if (!service_process_state_->AddToAutoRun()) {
314       // TODO(scottbyer/sanjeevr/dmaclach): Handle error condition
315       LOG(ERROR) << "Unable to AddToAutoRun";
316     }
317   }
318 }
319 
OnServiceDisabled()320 void ServiceProcess::OnServiceDisabled() {
321   DCHECK_NE(enabled_services_, 0);
322   enabled_services_--;
323   if (0 == enabled_services_) {
324     if (!service_process_state_->RemoveFromAutoRun()) {
325       // TODO(scottbyer/sanjeevr/dmaclach): Handle error condition
326       LOG(ERROR) << "Unable to RemoveFromAutoRun";
327     }
328     // We will wait for some time to respond to IPCs before shutting down.
329     ScheduleShutdownCheck();
330   }
331 }
332 
ScheduleShutdownCheck()333 void ServiceProcess::ScheduleShutdownCheck() {
334   base::MessageLoop::current()->PostDelayedTask(
335       FROM_HERE,
336       base::Bind(&ServiceProcess::ShutdownIfNeeded, base::Unretained(this)),
337       base::TimeDelta::FromSeconds(kShutdownDelaySeconds));
338 }
339 
ShutdownIfNeeded()340 void ServiceProcess::ShutdownIfNeeded() {
341   if (0 == enabled_services_) {
342     if (ipc_server_->is_client_connected()) {
343       // If there is a client connected, we need to try again later.
344       // Note that there is still a timing window here because a client may
345       // decide to connect at this point.
346       // TODO(sanjeevr): Fix this timing window.
347       ScheduleShutdownCheck();
348     } else {
349       Shutdown();
350     }
351   }
352 }
353 
ScheduleCloudPrintPolicyCheck()354 void ServiceProcess::ScheduleCloudPrintPolicyCheck() {
355   base::MessageLoop::current()->PostDelayedTask(
356       FROM_HERE,
357       base::Bind(&ServiceProcess::CloudPrintPolicyCheckIfNeeded,
358                  base::Unretained(this)),
359       base::TimeDelta::FromHours(kPolicyCheckDelayHours));
360 }
361 
CloudPrintPolicyCheckIfNeeded()362 void ServiceProcess::CloudPrintPolicyCheckIfNeeded() {
363   if (enabled_services_ && !ipc_server_->is_client_connected()) {
364     GetCloudPrintProxy()->CheckCloudPrintProxyPolicy();
365   }
366   ScheduleCloudPrintPolicyCheck();
367 }
368 
~ServiceProcess()369 ServiceProcess::~ServiceProcess() {
370   Teardown();
371   g_service_process = NULL;
372 }
373