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/location.h"
15 #include "base/memory/singleton.h"
16 #include "base/path_service.h"
17 #include "base/prefs/json_pref_store.h"
18 #include "base/single_thread_task_runner.h"
19 #include "base/strings/string16.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/synchronization/waitable_event.h"
22 #include "base/thread_task_runner_handle.h"
23 #include "base/threading/sequenced_worker_pool.h"
24 #include "base/values.h"
25 #include "chrome/common/chrome_constants.h"
26 #include "chrome/common/chrome_paths.h"
27 #include "chrome/common/chrome_switches.h"
28 #include "chrome/common/env_vars.h"
29 #include "chrome/common/pref_names.h"
30 #include "chrome/common/service_process_util.h"
31 #include "chrome/grit/chromium_strings.h"
32 #include "chrome/grit/generated_resources.h"
33 #include "chrome/service/cloud_print/cloud_print_message_handler.h"
34 #include "chrome/service/cloud_print/cloud_print_proxy.h"
35 #include "chrome/service/net/service_url_request_context_getter.h"
36 #include "chrome/service/service_process_prefs.h"
37 #include "net/base/network_change_notifier.h"
38 #include "net/url_request/url_fetcher.h"
39 #include "ui/base/l10n/l10n_util.h"
40 #include "ui/base/resource/resource_bundle.h"
41 #include "ui/base/ui_base_switches.h"
44 #include <glib-object.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 const char kDefaultServiceProcessLocale
[] = "en-US";
57 class ServiceIOThread
: public base::Thread
{
59 explicit ServiceIOThread(const char* name
);
60 ~ServiceIOThread() override
;
63 void CleanUp() override
;
66 DISALLOW_COPY_AND_ASSIGN(ServiceIOThread
);
69 ServiceIOThread::ServiceIOThread(const char* name
) : base::Thread(name
) {}
70 ServiceIOThread::~ServiceIOThread() {
74 void ServiceIOThread::CleanUp() {
75 net::URLFetcher::CancelAll();
78 // Prepares the localized strings that are going to be displayed to
79 // the user if the service process dies. These strings are stored in the
80 // environment block so they are accessible in the early stages of the
81 // chrome executable's lifetime.
82 void PrepareRestartOnCrashEnviroment(
83 const base::CommandLine
& parsed_command_line
) {
84 scoped_ptr
<base::Environment
> env(base::Environment::Create());
85 // Clear this var so child processes don't show the dialog by default.
86 env
->UnSetVar(env_vars::kShowRestart
);
88 // For non-interactive tests we don't restart on crash.
89 if (env
->HasVar(env_vars::kHeadless
))
92 // If the known command-line test options are used we don't create the
93 // environment block which means we don't get the restart dialog.
94 if (parsed_command_line
.HasSwitch(switches::kNoErrorDialogs
))
97 // The encoding we use for the info is "title|context|direction" where
98 // direction is either env_vars::kRtlLocale or env_vars::kLtrLocale depending
99 // on the current locale.
100 base::string16
dlg_strings(
101 l10n_util::GetStringUTF16(IDS_CRASH_RECOVERY_TITLE
));
102 dlg_strings
.push_back('|');
103 base::string16
adjusted_string(l10n_util::GetStringFUTF16(
104 IDS_SERVICE_CRASH_RECOVERY_CONTENT
,
105 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT
)));
106 base::i18n::AdjustStringForLocaleDirection(&adjusted_string
);
107 dlg_strings
.append(adjusted_string
);
108 dlg_strings
.push_back('|');
109 dlg_strings
.append(base::ASCIIToUTF16(
110 base::i18n::IsRTL() ? env_vars::kRtlLocale
: env_vars::kLtrLocale
));
112 env
->SetVar(env_vars::kRestartInfo
, base::UTF16ToUTF8(dlg_strings
));
117 ServiceProcess::ServiceProcess()
118 : shutdown_event_(true /* manual_reset */, false /* initially_signaled */),
119 main_message_loop_(NULL
),
120 enabled_services_(0),
121 update_available_(false) {
122 DCHECK(!g_service_process
);
123 g_service_process
= this;
126 bool ServiceProcess::Initialize(base::MessageLoopForUI
* message_loop
,
127 const base::CommandLine
& command_line
,
128 ServiceProcessState
* state
) {
129 #if defined(USE_GLIB)
130 // g_type_init has been deprecated since version 2.35.
131 #if !GLIB_CHECK_VERSION(2, 35, 0)
132 // GLib type system initialization is needed for gconf.
135 #endif // defined(OS_LINUX) || defined(OS_OPENBSD)
136 main_message_loop_
= message_loop
;
137 service_process_state_
.reset(state
);
138 network_change_notifier_
.reset(net::NetworkChangeNotifier::Create());
139 base::Thread::Options options
;
140 options
.message_loop_type
= base::MessageLoop::TYPE_IO
;
141 io_thread_
.reset(new ServiceIOThread("ServiceProcess_IO"));
142 file_thread_
.reset(new base::Thread("ServiceProcess_File"));
143 if (!io_thread_
->StartWithOptions(options
) ||
144 !file_thread_
->StartWithOptions(options
)) {
149 blocking_pool_
= new base::SequencedWorkerPool(3, "ServiceBlocking");
151 request_context_getter_
= new ServiceURLRequestContextGetter();
153 base::FilePath user_data_dir
;
154 PathService::Get(chrome::DIR_USER_DATA
, &user_data_dir
);
155 base::FilePath pref_path
=
156 user_data_dir
.Append(chrome::kServiceStateFileName
);
157 service_prefs_
.reset(new ServiceProcessPrefs(
159 JsonPrefStore::GetTaskRunnerForFile(pref_path
, blocking_pool_
.get())
161 service_prefs_
->ReadPrefs();
163 // This switch it required to run connector with test gaia.
164 if (command_line
.HasSwitch(switches::kIgnoreUrlFetcherCertRequests
))
165 net::URLFetcher::SetIgnoreCertificateRequests(true);
167 // Check if a locale override has been specified on the command-line.
168 std::string locale
= command_line
.GetSwitchValueASCII(switches::kLang
);
169 if (!locale
.empty()) {
170 service_prefs_
->SetString(prefs::kApplicationLocale
, locale
);
171 service_prefs_
->WritePrefs();
173 // If no command-line value was specified, read the last used locale from
176 service_prefs_
->GetString(prefs::kApplicationLocale
, std::string());
177 // If no locale was specified anywhere, use the default one.
179 locale
= kDefaultServiceProcessLocale
;
181 ui::ResourceBundle::InitSharedInstanceWithLocale(
182 locale
, NULL
, ui::ResourceBundle::LOAD_COMMON_RESOURCES
);
184 PrepareRestartOnCrashEnviroment(command_line
);
186 // Enable Cloud Print if needed. First check the command-line.
187 // Then check if the cloud print proxy was previously enabled.
188 if (command_line
.HasSwitch(switches::kEnableCloudPrintProxy
) ||
189 service_prefs_
->GetBoolean(prefs::kCloudPrintProxyEnabled
, false)) {
190 GetCloudPrintProxy()->EnableForUser();
193 VLOG(1) << "Starting Service Process IPC Server";
194 ipc_server_
.reset(new ServiceIPCServer(
197 service_process_state_
->GetServiceProcessChannel(),
199 ipc_server_
->AddMessageHandler(make_scoped_ptr(
200 new cloud_print::CloudPrintMessageHandler(ipc_server_
.get(), this)));
203 // After the IPC server has started we signal that the service process is
205 if (!service_process_state_
->SignalReady(
206 io_task_runner().get(),
207 base::Bind(&ServiceProcess::Terminate
, base::Unretained(this)))) {
211 // See if we need to stay running.
212 ScheduleShutdownCheck();
217 bool ServiceProcess::Teardown() {
218 service_prefs_
.reset();
219 cloud_print_proxy_
.reset();
222 // Signal this event before shutting down the service process. That way all
223 // background threads can cleanup.
224 shutdown_event_
.Signal();
226 file_thread_
.reset();
228 if (blocking_pool_
.get()) {
229 // The goal is to make it impossible for chrome to 'infinite loop' during
230 // shutdown, but to reasonably expect that all BLOCKING_SHUTDOWN tasks
231 // queued during shutdown get run. There's nothing particularly scientific
232 // about the number chosen.
233 const int kMaxNewShutdownBlockingTasks
= 1000;
234 blocking_pool_
->Shutdown(kMaxNewShutdownBlockingTasks
);
235 blocking_pool_
= NULL
;
238 // The NetworkChangeNotifier must be destroyed after all other threads that
239 // might use it have been shut down.
240 network_change_notifier_
.reset();
242 service_process_state_
->SignalStopped();
246 // This method is called when a shutdown command is received from IPC channel
247 // or there was an error in the IPC channel.
248 void ServiceProcess::Shutdown() {
249 #if defined(OS_MACOSX)
250 // On MacOS X the service must be removed from the launchd job list.
251 // http://www.chromium.org/developers/design-documents/service-processes
252 // The best way to do that is to go through the ForceServiceProcessShutdown
253 // path. If it succeeds Terminate() will be called from the handler registered
254 // via service_process_state_->SignalReady().
255 // On failure call Terminate() directly to force the process to actually
257 if (!ForceServiceProcessShutdown("", 0)) {
265 void ServiceProcess::Terminate() {
266 main_message_loop_
->task_runner()->PostTask(FROM_HERE
,
267 base::MessageLoop::QuitClosure());
270 void ServiceProcess::OnShutdown() {
274 void ServiceProcess::OnUpdateAvailable() {
275 update_available_
= true;
278 bool ServiceProcess::OnIPCClientDisconnect() {
279 // If there are no enabled services or if there is an update available
280 // we want to shutdown right away. Else we want to keep listening for
282 if (!enabled_services_
|| update_available_
) {
289 cloud_print::CloudPrintProxy
* ServiceProcess::GetCloudPrintProxy() {
290 if (!cloud_print_proxy_
.get()) {
291 cloud_print_proxy_
.reset(new cloud_print::CloudPrintProxy());
292 cloud_print_proxy_
->Initialize(service_prefs_
.get(), this);
294 return cloud_print_proxy_
.get();
297 void ServiceProcess::OnCloudPrintProxyEnabled(bool persist_state
) {
299 // Save the preference that we have enabled the cloud print proxy.
300 service_prefs_
->SetBoolean(prefs::kCloudPrintProxyEnabled
, true);
301 service_prefs_
->WritePrefs();
306 void ServiceProcess::OnCloudPrintProxyDisabled(bool persist_state
) {
308 // Save the preference that we have disabled the cloud print proxy.
309 service_prefs_
->SetBoolean(prefs::kCloudPrintProxyEnabled
, false);
310 service_prefs_
->WritePrefs();
315 ServiceURLRequestContextGetter
*
316 ServiceProcess::GetServiceURLRequestContextGetter() {
317 DCHECK(request_context_getter_
.get());
318 return request_context_getter_
.get();
321 void ServiceProcess::OnServiceEnabled() {
323 if ((1 == enabled_services_
) &&
324 !base::CommandLine::ForCurrentProcess()->HasSwitch(
325 switches::kNoServiceAutorun
)) {
326 if (!service_process_state_
->AddToAutoRun()) {
327 // TODO(scottbyer/sanjeevr/dmaclach): Handle error condition
328 LOG(ERROR
) << "Unable to AddToAutoRun";
333 void ServiceProcess::OnServiceDisabled() {
334 DCHECK_NE(enabled_services_
, 0);
336 if (0 == enabled_services_
) {
337 if (!service_process_state_
->RemoveFromAutoRun()) {
338 // TODO(scottbyer/sanjeevr/dmaclach): Handle error condition
339 LOG(ERROR
) << "Unable to RemoveFromAutoRun";
341 // We will wait for some time to respond to IPCs before shutting down.
342 ScheduleShutdownCheck();
346 void ServiceProcess::ScheduleShutdownCheck() {
347 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
349 base::Bind(&ServiceProcess::ShutdownIfNeeded
, base::Unretained(this)),
350 base::TimeDelta::FromSeconds(kShutdownDelaySeconds
));
353 void ServiceProcess::ShutdownIfNeeded() {
354 if (0 == enabled_services_
) {
355 if (ipc_server_
->is_ipc_client_connected()) {
356 // If there is an IPC client connected, we need to try again later.
357 // Note that there is still a timing window here because a client may
358 // decide to connect at this point.
359 // TODO(sanjeevr): Fix this timing window.
360 ScheduleShutdownCheck();
367 ServiceProcess::~ServiceProcess() {
369 g_service_process
= NULL
;