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/grit/chromium_strings.h"
27#include "chrome/grit/generated_resources.h"
28#include "chrome/service/cloud_print/cloud_print_proxy.h"
29#include "chrome/service/net/service_url_request_context_getter.h"
30#include "chrome/service/service_ipc_server.h"
31#include "chrome/service/service_process_prefs.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
42ServiceProcess* g_service_process = NULL;
43
44namespace {
45
46// Delay in seconds after the last service is disabled before we attempt
47// a shutdown.
48const int kShutdownDelaySeconds = 60;
49
50// Delay in hours between launching a browser process to check the
51// policy for us.
52const int64 kPolicyCheckDelayHours = 8;
53
54const char kDefaultServiceProcessLocale[] = "en-US";
55
56class 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
68ServiceIOThread::ServiceIOThread(const char* name) : base::Thread(name) {}
69ServiceIOThread::~ServiceIOThread() {
70  Stop();
71}
72
73void 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.
81void 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
116ServiceProcess::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
125bool 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  ui::ResourceBundle::InitSharedInstanceWithLocale(
181      locale, NULL, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
182
183  PrepareRestartOnCrashEnviroment(command_line);
184
185  // Enable Cloud Print if needed. First check the command-line.
186  // Then check if the cloud print proxy was previously enabled.
187  if (command_line.HasSwitch(switches::kEnableCloudPrintProxy) ||
188      service_prefs_->GetBoolean(prefs::kCloudPrintProxyEnabled, false)) {
189    GetCloudPrintProxy()->EnableForUser();
190  }
191
192  VLOG(1) << "Starting Service Process IPC Server";
193  ipc_server_.reset(new ServiceIPCServer(
194      service_process_state_->GetServiceProcessChannel()));
195  ipc_server_->Init();
196
197  // After the IPC server has started we signal that the service process is
198  // ready.
199  if (!service_process_state_->SignalReady(
200          io_thread_->message_loop_proxy().get(),
201          base::Bind(&ServiceProcess::Terminate, base::Unretained(this)))) {
202    return false;
203  }
204
205  // See if we need to stay running.
206  ScheduleShutdownCheck();
207
208  // Occasionally check to see if we need to launch the browser to get the
209  // policy state information.
210  CloudPrintPolicyCheckIfNeeded();
211  return true;
212}
213
214bool ServiceProcess::Teardown() {
215  service_prefs_.reset();
216  cloud_print_proxy_.reset();
217
218  ipc_server_.reset();
219  // Signal this event before shutting down the service process. That way all
220  // background threads can cleanup.
221  shutdown_event_.Signal();
222  io_thread_.reset();
223  file_thread_.reset();
224
225  if (blocking_pool_.get()) {
226    // The goal is to make it impossible for chrome to 'infinite loop' during
227    // shutdown, but to reasonably expect that all BLOCKING_SHUTDOWN tasks
228    // queued during shutdown get run. There's nothing particularly scientific
229    // about the number chosen.
230    const int kMaxNewShutdownBlockingTasks = 1000;
231    blocking_pool_->Shutdown(kMaxNewShutdownBlockingTasks);
232    blocking_pool_ = NULL;
233  }
234
235  // The NetworkChangeNotifier must be destroyed after all other threads that
236  // might use it have been shut down.
237  network_change_notifier_.reset();
238
239  service_process_state_->SignalStopped();
240  return true;
241}
242
243// This method is called when a shutdown command is received from IPC channel
244// or there was an error in the IPC channel.
245void ServiceProcess::Shutdown() {
246#if defined(OS_MACOSX)
247  // On MacOS X the service must be removed from the launchd job list.
248  // http://www.chromium.org/developers/design-documents/service-processes
249  // The best way to do that is to go through the ForceServiceProcessShutdown
250  // path. If it succeeds Terminate() will be called from the handler registered
251  // via service_process_state_->SignalReady().
252  // On failure call Terminate() directly to force the process to actually
253  // terminate.
254  if (!ForceServiceProcessShutdown("", 0)) {
255    Terminate();
256  }
257#else
258  Terminate();
259#endif
260}
261
262void ServiceProcess::Terminate() {
263  main_message_loop_->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
264}
265
266bool ServiceProcess::HandleClientDisconnect() {
267  // If there are no enabled services or if there is an update available
268  // we want to shutdown right away. Else we want to keep listening for
269  // new connections.
270  if (!enabled_services_ || update_available()) {
271    Shutdown();
272    return false;
273  }
274  return true;
275}
276
277cloud_print::CloudPrintProxy* ServiceProcess::GetCloudPrintProxy() {
278  if (!cloud_print_proxy_.get()) {
279    cloud_print_proxy_.reset(new cloud_print::CloudPrintProxy());
280    cloud_print_proxy_->Initialize(service_prefs_.get(), this);
281  }
282  return cloud_print_proxy_.get();
283}
284
285void ServiceProcess::OnCloudPrintProxyEnabled(bool persist_state) {
286  if (persist_state) {
287    // Save the preference that we have enabled the cloud print proxy.
288    service_prefs_->SetBoolean(prefs::kCloudPrintProxyEnabled, true);
289    service_prefs_->WritePrefs();
290  }
291  OnServiceEnabled();
292}
293
294void ServiceProcess::OnCloudPrintProxyDisabled(bool persist_state) {
295  if (persist_state) {
296    // Save the preference that we have disabled the cloud print proxy.
297    service_prefs_->SetBoolean(prefs::kCloudPrintProxyEnabled, false);
298    service_prefs_->WritePrefs();
299  }
300  OnServiceDisabled();
301}
302
303ServiceURLRequestContextGetter*
304ServiceProcess::GetServiceURLRequestContextGetter() {
305  DCHECK(request_context_getter_.get());
306  return request_context_getter_.get();
307}
308
309void ServiceProcess::OnServiceEnabled() {
310  enabled_services_++;
311  if ((1 == enabled_services_) &&
312      !CommandLine::ForCurrentProcess()->HasSwitch(
313          switches::kNoServiceAutorun)) {
314    if (!service_process_state_->AddToAutoRun()) {
315      // TODO(scottbyer/sanjeevr/dmaclach): Handle error condition
316      LOG(ERROR) << "Unable to AddToAutoRun";
317    }
318  }
319}
320
321void ServiceProcess::OnServiceDisabled() {
322  DCHECK_NE(enabled_services_, 0);
323  enabled_services_--;
324  if (0 == enabled_services_) {
325    if (!service_process_state_->RemoveFromAutoRun()) {
326      // TODO(scottbyer/sanjeevr/dmaclach): Handle error condition
327      LOG(ERROR) << "Unable to RemoveFromAutoRun";
328    }
329    // We will wait for some time to respond to IPCs before shutting down.
330    ScheduleShutdownCheck();
331  }
332}
333
334void ServiceProcess::ScheduleShutdownCheck() {
335  base::MessageLoop::current()->PostDelayedTask(
336      FROM_HERE,
337      base::Bind(&ServiceProcess::ShutdownIfNeeded, base::Unretained(this)),
338      base::TimeDelta::FromSeconds(kShutdownDelaySeconds));
339}
340
341void ServiceProcess::ShutdownIfNeeded() {
342  if (0 == enabled_services_) {
343    if (ipc_server_->is_client_connected()) {
344      // If there is a client connected, we need to try again later.
345      // Note that there is still a timing window here because a client may
346      // decide to connect at this point.
347      // TODO(sanjeevr): Fix this timing window.
348      ScheduleShutdownCheck();
349    } else {
350      Shutdown();
351    }
352  }
353}
354
355void ServiceProcess::ScheduleCloudPrintPolicyCheck() {
356  base::MessageLoop::current()->PostDelayedTask(
357      FROM_HERE,
358      base::Bind(&ServiceProcess::CloudPrintPolicyCheckIfNeeded,
359                 base::Unretained(this)),
360      base::TimeDelta::FromHours(kPolicyCheckDelayHours));
361}
362
363void ServiceProcess::CloudPrintPolicyCheckIfNeeded() {
364  if (enabled_services_ && !ipc_server_->is_client_connected()) {
365    GetCloudPrintProxy()->CheckCloudPrintProxyPolicy();
366  }
367  ScheduleCloudPrintPolicyCheck();
368}
369
370ServiceProcess::~ServiceProcess() {
371  Teardown();
372  g_service_process = NULL;
373}
374