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.
5 #include "chrome/service/service_process.h"
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"
39 #include <glib-object.h>
42 #if defined(TOOLKIT_GTK)
44 #include "ui/gfx/gtk_util.h"
47 ServiceProcess
* g_service_process
= NULL
;
51 // Delay in seconds after the last service is disabled before we attempt
53 const int kShutdownDelaySeconds
= 60;
55 // Delay in hours between launching a browser process to check the
57 const int64 kPolicyCheckDelayHours
= 8;
59 const char kDefaultServiceProcessLocale
[] = "en-US";
61 class ServiceIOThread
: public base::Thread
{
63 explicit ServiceIOThread(const char* name
);
64 virtual ~ServiceIOThread();
67 virtual void CleanUp() OVERRIDE
;
70 DISALLOW_COPY_AND_ASSIGN(ServiceIOThread
);
73 ServiceIOThread::ServiceIOThread(const char* name
) : base::Thread(name
) {}
74 ServiceIOThread::~ServiceIOThread() {
78 void ServiceIOThread::CleanUp() {
79 net::URLFetcher::CancelAll();
82 // Prepares the localized strings that are going to be displayed to
83 // the user if the service process dies. These strings are stored in the
84 // environment block so they are accessible in the early stages of the
85 // chrome executable's lifetime.
86 void PrepareRestartOnCrashEnviroment(
87 const CommandLine
&parsed_command_line
) {
88 scoped_ptr
<base::Environment
> env(base::Environment::Create());
89 // Clear this var so child processes don't show the dialog by default.
90 env
->UnSetVar(env_vars::kShowRestart
);
92 // For non-interactive tests we don't restart on crash.
93 if (env
->HasVar(env_vars::kHeadless
))
96 // If the known command-line test options are used we don't create the
97 // environment block which means we don't get the restart dialog.
98 if (parsed_command_line
.HasSwitch(switches::kNoErrorDialogs
))
101 // The encoding we use for the info is "title|context|direction" where
102 // direction is either env_vars::kRtlLocale or env_vars::kLtrLocale depending
103 // on the current locale.
104 base::string16
dlg_strings(
105 l10n_util::GetStringUTF16(IDS_CRASH_RECOVERY_TITLE
));
106 dlg_strings
.push_back('|');
107 base::string16
adjusted_string(l10n_util::GetStringFUTF16(
108 IDS_SERVICE_CRASH_RECOVERY_CONTENT
,
109 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT
)));
110 base::i18n::AdjustStringForLocaleDirection(&adjusted_string
);
111 dlg_strings
.append(adjusted_string
);
112 dlg_strings
.push_back('|');
113 dlg_strings
.append(base::ASCIIToUTF16(
114 base::i18n::IsRTL() ? env_vars::kRtlLocale
: env_vars::kLtrLocale
));
116 env
->SetVar(env_vars::kRestartInfo
, base::UTF16ToUTF8(dlg_strings
));
121 ServiceProcess::ServiceProcess()
122 : shutdown_event_(true, false),
123 main_message_loop_(NULL
),
124 enabled_services_(0),
125 update_available_(false) {
126 DCHECK(!g_service_process
);
127 g_service_process
= this;
130 bool ServiceProcess::Initialize(base::MessageLoopForUI
* message_loop
,
131 const CommandLine
& command_line
,
132 ServiceProcessState
* state
) {
133 #if defined(TOOLKIT_GTK)
134 // TODO(jamiewalch): Calling GtkInitFromCommandLine here causes the process
135 // to abort if run headless. The correct fix for this is to refactor the
136 // service process to be more modular, a task that is currently underway.
137 // However, since this problem is blocking cloud print, the following quick
138 // hack will have to do. Note that the situation with this hack in place is
139 // no worse than it was when we weren't initializing GTK at all.
141 scoped_ptr
<char*[]> argv(new char*[2]);
142 argv
[0] = strdup(command_line
.argv()[0].c_str());
144 char **argv_pointer
= argv
.get();
145 gtk_init_check(&argc
, &argv_pointer
);
147 #elif defined(USE_GLIB)
148 // g_type_init has been deprecated since version 2.35.
149 #if !GLIB_CHECK_VERSION(2, 35, 0)
150 // GLib type system initialization is needed for gconf.
153 #endif // defined(OS_LINUX) || defined(OS_OPENBSD)
154 main_message_loop_
= message_loop
;
155 service_process_state_
.reset(state
);
156 network_change_notifier_
.reset(net::NetworkChangeNotifier::Create());
157 base::Thread::Options options
;
158 options
.message_loop_type
= base::MessageLoop::TYPE_IO
;
159 io_thread_
.reset(new ServiceIOThread("ServiceProcess_IO"));
160 file_thread_
.reset(new base::Thread("ServiceProcess_File"));
161 if (!io_thread_
->StartWithOptions(options
) ||
162 !file_thread_
->StartWithOptions(options
)) {
167 blocking_pool_
= new base::SequencedWorkerPool(3, "ServiceBlocking");
169 request_context_getter_
= new ServiceURLRequestContextGetter();
171 base::FilePath user_data_dir
;
172 PathService::Get(chrome::DIR_USER_DATA
, &user_data_dir
);
173 base::FilePath pref_path
=
174 user_data_dir
.Append(chrome::kServiceStateFileName
);
175 service_prefs_
.reset(new ServiceProcessPrefs(
177 JsonPrefStore::GetTaskRunnerForFile(pref_path
, blocking_pool_
.get())
179 service_prefs_
->ReadPrefs();
181 // This switch it required to run connector with test gaia.
182 if (command_line
.HasSwitch(switches::kIgnoreUrlFetcherCertRequests
))
183 net::URLFetcher::SetIgnoreCertificateRequests(true);
185 // Check if a locale override has been specified on the command-line.
186 std::string locale
= command_line
.GetSwitchValueASCII(switches::kLang
);
187 if (!locale
.empty()) {
188 service_prefs_
->SetString(prefs::kApplicationLocale
, locale
);
189 service_prefs_
->WritePrefs();
191 // If no command-line value was specified, read the last used locale from
194 service_prefs_
->GetString(prefs::kApplicationLocale
, std::string());
195 // If no locale was specified anywhere, use the default one.
197 locale
= kDefaultServiceProcessLocale
;
199 ResourceBundle::InitSharedInstanceWithLocale(locale
, NULL
);
201 PrepareRestartOnCrashEnviroment(command_line
);
203 // Enable Cloud Print if needed. First check the command-line.
204 // Then check if the cloud print proxy was previously enabled.
205 if (command_line
.HasSwitch(switches::kEnableCloudPrintProxy
) ||
206 service_prefs_
->GetBoolean(prefs::kCloudPrintProxyEnabled
, false)) {
207 GetCloudPrintProxy()->EnableForUser();
210 VLOG(1) << "Starting Service Process IPC Server";
211 ipc_server_
.reset(new ServiceIPCServer(
212 service_process_state_
->GetServiceProcessChannel()));
215 // After the IPC server has started we signal that the service process is
217 if (!service_process_state_
->SignalReady(
218 io_thread_
->message_loop_proxy().get(),
219 base::Bind(&ServiceProcess::Terminate
, base::Unretained(this)))) {
223 // See if we need to stay running.
224 ScheduleShutdownCheck();
226 // Occasionally check to see if we need to launch the browser to get the
227 // policy state information.
228 CloudPrintPolicyCheckIfNeeded();
232 bool ServiceProcess::Teardown() {
233 service_prefs_
.reset();
234 cloud_print_proxy_
.reset();
237 // Signal this event before shutting down the service process. That way all
238 // background threads can cleanup.
239 shutdown_event_
.Signal();
241 file_thread_
.reset();
243 if (blocking_pool_
.get()) {
244 // The goal is to make it impossible for chrome to 'infinite loop' during
245 // shutdown, but to reasonably expect that all BLOCKING_SHUTDOWN tasks
246 // queued during shutdown get run. There's nothing particularly scientific
247 // about the number chosen.
248 const int kMaxNewShutdownBlockingTasks
= 1000;
249 blocking_pool_
->Shutdown(kMaxNewShutdownBlockingTasks
);
250 blocking_pool_
= NULL
;
253 // The NetworkChangeNotifier must be destroyed after all other threads that
254 // might use it have been shut down.
255 network_change_notifier_
.reset();
257 service_process_state_
->SignalStopped();
261 // This method is called when a shutdown command is received from IPC channel
262 // or there was an error in the IPC channel.
263 void ServiceProcess::Shutdown() {
264 #if defined(OS_MACOSX)
265 // On MacOS X the service must be removed from the launchd job list.
266 // http://www.chromium.org/developers/design-documents/service-processes
267 // The best way to do that is to go through the ForceServiceProcessShutdown
268 // path. If it succeeds Terminate() will be called from the handler registered
269 // via service_process_state_->SignalReady().
270 // On failure call Terminate() directly to force the process to actually
272 if (!ForceServiceProcessShutdown("", 0)) {
280 void ServiceProcess::Terminate() {
281 main_message_loop_
->PostTask(FROM_HERE
, base::MessageLoop::QuitClosure());
284 bool ServiceProcess::HandleClientDisconnect() {
285 // If there are no enabled services or if there is an update available
286 // we want to shutdown right away. Else we want to keep listening for
288 if (!enabled_services_
|| update_available()) {
295 cloud_print::CloudPrintProxy
* ServiceProcess::GetCloudPrintProxy() {
296 if (!cloud_print_proxy_
.get()) {
297 cloud_print_proxy_
.reset(new cloud_print::CloudPrintProxy());
298 cloud_print_proxy_
->Initialize(service_prefs_
.get(), this);
300 return cloud_print_proxy_
.get();
303 void ServiceProcess::OnCloudPrintProxyEnabled(bool persist_state
) {
305 // Save the preference that we have enabled the cloud print proxy.
306 service_prefs_
->SetBoolean(prefs::kCloudPrintProxyEnabled
, true);
307 service_prefs_
->WritePrefs();
312 void ServiceProcess::OnCloudPrintProxyDisabled(bool persist_state
) {
314 // Save the preference that we have disabled the cloud print proxy.
315 service_prefs_
->SetBoolean(prefs::kCloudPrintProxyEnabled
, false);
316 service_prefs_
->WritePrefs();
321 ServiceURLRequestContextGetter
*
322 ServiceProcess::GetServiceURLRequestContextGetter() {
323 DCHECK(request_context_getter_
.get());
324 return request_context_getter_
.get();
327 void ServiceProcess::OnServiceEnabled() {
329 if ((1 == enabled_services_
) &&
330 !CommandLine::ForCurrentProcess()->HasSwitch(
331 switches::kNoServiceAutorun
)) {
332 if (!service_process_state_
->AddToAutoRun()) {
333 // TODO(scottbyer/sanjeevr/dmaclach): Handle error condition
334 LOG(ERROR
) << "Unable to AddToAutoRun";
339 void ServiceProcess::OnServiceDisabled() {
340 DCHECK_NE(enabled_services_
, 0);
342 if (0 == enabled_services_
) {
343 if (!service_process_state_
->RemoveFromAutoRun()) {
344 // TODO(scottbyer/sanjeevr/dmaclach): Handle error condition
345 LOG(ERROR
) << "Unable to RemoveFromAutoRun";
347 // We will wait for some time to respond to IPCs before shutting down.
348 ScheduleShutdownCheck();
352 void ServiceProcess::ScheduleShutdownCheck() {
353 base::MessageLoop::current()->PostDelayedTask(
355 base::Bind(&ServiceProcess::ShutdownIfNeeded
, base::Unretained(this)),
356 base::TimeDelta::FromSeconds(kShutdownDelaySeconds
));
359 void ServiceProcess::ShutdownIfNeeded() {
360 if (0 == enabled_services_
) {
361 if (ipc_server_
->is_client_connected()) {
362 // If there is a client connected, we need to try again later.
363 // Note that there is still a timing window here because a client may
364 // decide to connect at this point.
365 // TODO(sanjeevr): Fix this timing window.
366 ScheduleShutdownCheck();
373 void ServiceProcess::ScheduleCloudPrintPolicyCheck() {
374 base::MessageLoop::current()->PostDelayedTask(
376 base::Bind(&ServiceProcess::CloudPrintPolicyCheckIfNeeded
,
377 base::Unretained(this)),
378 base::TimeDelta::FromHours(kPolicyCheckDelayHours
));
381 void ServiceProcess::CloudPrintPolicyCheckIfNeeded() {
382 if (enabled_services_
&& !ipc_server_
->is_client_connected()) {
383 GetCloudPrintProxy()->CheckCloudPrintProxyPolicy();
385 ScheduleCloudPrintPolicyCheck();
388 ServiceProcess::~ServiceProcess() {
390 g_service_process
= NULL
;