This source file includes following definitions.
- CleanUp
- PrepareRestartOnCrashEnviroment
- update_available_
- Initialize
- Teardown
- Shutdown
- Terminate
- HandleClientDisconnect
- GetCloudPrintProxy
- OnCloudPrintProxyEnabled
- OnCloudPrintProxyDisabled
- GetServiceURLRequestContextGetter
- OnServiceEnabled
- OnServiceDisabled
- ScheduleShutdownCheck
- ShutdownIfNeeded
- ScheduleCloudPrintPolicyCheck
- CloudPrintPolicyCheckIfNeeded
#include "chrome/service/service_process.h"
#include <algorithm>
#include "base/basictypes.h"
#include "base/callback.h"
#include "base/command_line.h"
#include "base/environment.h"
#include "base/i18n/rtl.h"
#include "base/memory/singleton.h"
#include "base/path_service.h"
#include "base/prefs/json_pref_store.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/env_vars.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/service_process_util.h"
#include "chrome/service/cloud_print/cloud_print_proxy.h"
#include "chrome/service/net/service_url_request_context.h"
#include "chrome/service/service_ipc_server.h"
#include "chrome/service/service_process_prefs.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "net/base/network_change_notifier.h"
#include "net/url_request/url_fetcher.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_switches.h"
#if defined(USE_GLIB)
#include <glib-object.h>
#endif
#if defined(TOOLKIT_GTK)
#include <gtk/gtk.h>
#include "ui/gfx/gtk_util.h"
#endif
ServiceProcess* g_service_process = NULL;
namespace {
const int kShutdownDelaySeconds = 60;
const int64 kPolicyCheckDelayHours = 8;
const char kDefaultServiceProcessLocale[] = "en-US";
class ServiceIOThread : public base::Thread {
public:
explicit ServiceIOThread(const char* name);
virtual ~ServiceIOThread();
protected:
virtual void CleanUp() OVERRIDE;
private:
DISALLOW_COPY_AND_ASSIGN(ServiceIOThread);
};
ServiceIOThread::ServiceIOThread(const char* name) : base::Thread(name) {}
ServiceIOThread::~ServiceIOThread() {
Stop();
}
void ServiceIOThread::CleanUp() {
net::URLFetcher::CancelAll();
}
void PrepareRestartOnCrashEnviroment(
const CommandLine &parsed_command_line) {
scoped_ptr<base::Environment> env(base::Environment::Create());
env->UnSetVar(env_vars::kShowRestart);
if (env->HasVar(env_vars::kHeadless))
return;
if (parsed_command_line.HasSwitch(switches::kNoErrorDialogs))
return;
base::string16 dlg_strings(
l10n_util::GetStringUTF16(IDS_CRASH_RECOVERY_TITLE));
dlg_strings.push_back('|');
base::string16 adjusted_string(l10n_util::GetStringFUTF16(
IDS_SERVICE_CRASH_RECOVERY_CONTENT,
l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT)));
base::i18n::AdjustStringForLocaleDirection(&adjusted_string);
dlg_strings.append(adjusted_string);
dlg_strings.push_back('|');
dlg_strings.append(base::ASCIIToUTF16(
base::i18n::IsRTL() ? env_vars::kRtlLocale : env_vars::kLtrLocale));
env->SetVar(env_vars::kRestartInfo, base::UTF16ToUTF8(dlg_strings));
}
}
ServiceProcess::ServiceProcess()
: shutdown_event_(true, false),
main_message_loop_(NULL),
enabled_services_(0),
update_available_(false) {
DCHECK(!g_service_process);
g_service_process = this;
}
bool ServiceProcess::Initialize(base::MessageLoopForUI* message_loop,
const CommandLine& command_line,
ServiceProcessState* state) {
#if defined(TOOLKIT_GTK)
int argc = 1;
scoped_ptr<char*[]> argv(new char*[2]);
argv[0] = strdup(command_line.argv()[0].c_str());
argv[1] = NULL;
char **argv_pointer = argv.get();
gtk_init_check(&argc, &argv_pointer);
free(argv[0]);
#elif defined(USE_GLIB)
#if !GLIB_CHECK_VERSION(2, 35, 0)
g_type_init();
#endif
#endif
main_message_loop_ = message_loop;
service_process_state_.reset(state);
network_change_notifier_.reset(net::NetworkChangeNotifier::Create());
base::Thread::Options options;
options.message_loop_type = base::MessageLoop::TYPE_IO;
io_thread_.reset(new ServiceIOThread("ServiceProcess_IO"));
file_thread_.reset(new base::Thread("ServiceProcess_File"));
if (!io_thread_->StartWithOptions(options) ||
!file_thread_->StartWithOptions(options)) {
NOTREACHED();
Teardown();
return false;
}
blocking_pool_ = new base::SequencedWorkerPool(3, "ServiceBlocking");
request_context_getter_ = new ServiceURLRequestContextGetter();
base::FilePath user_data_dir;
PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
base::FilePath pref_path =
user_data_dir.Append(chrome::kServiceStateFileName);
service_prefs_.reset(new ServiceProcessPrefs(
pref_path,
JsonPrefStore::GetTaskRunnerForFile(pref_path, blocking_pool_.get())
.get()));
service_prefs_->ReadPrefs();
if (command_line.HasSwitch(switches::kIgnoreUrlFetcherCertRequests))
net::URLFetcher::SetIgnoreCertificateRequests(true);
std::string locale = command_line.GetSwitchValueASCII(switches::kLang);
if (!locale.empty()) {
service_prefs_->SetString(prefs::kApplicationLocale, locale);
service_prefs_->WritePrefs();
} else {
locale =
service_prefs_->GetString(prefs::kApplicationLocale, std::string());
if (locale.empty())
locale = kDefaultServiceProcessLocale;
}
ResourceBundle::InitSharedInstanceWithLocale(locale, NULL);
PrepareRestartOnCrashEnviroment(command_line);
if (command_line.HasSwitch(switches::kEnableCloudPrintProxy) ||
service_prefs_->GetBoolean(prefs::kCloudPrintProxyEnabled, false)) {
GetCloudPrintProxy()->EnableForUser();
}
VLOG(1) << "Starting Service Process IPC Server";
ipc_server_.reset(new ServiceIPCServer(
service_process_state_->GetServiceProcessChannel()));
ipc_server_->Init();
if (!service_process_state_->SignalReady(
io_thread_->message_loop_proxy().get(),
base::Bind(&ServiceProcess::Terminate, base::Unretained(this)))) {
return false;
}
ScheduleShutdownCheck();
CloudPrintPolicyCheckIfNeeded();
return true;
}
bool ServiceProcess::Teardown() {
service_prefs_.reset();
cloud_print_proxy_.reset();
ipc_server_.reset();
shutdown_event_.Signal();
io_thread_.reset();
file_thread_.reset();
if (blocking_pool_.get()) {
const int kMaxNewShutdownBlockingTasks = 1000;
blocking_pool_->Shutdown(kMaxNewShutdownBlockingTasks);
blocking_pool_ = NULL;
}
network_change_notifier_.reset();
service_process_state_->SignalStopped();
return true;
}
void ServiceProcess::Shutdown() {
#if defined(OS_MACOSX)
if (!ForceServiceProcessShutdown("", 0)) {
Terminate();
}
#else
Terminate();
#endif
}
void ServiceProcess::Terminate() {
main_message_loop_->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
}
bool ServiceProcess::HandleClientDisconnect() {
if (!enabled_services_ || update_available()) {
Shutdown();
return false;
}
return true;
}
cloud_print::CloudPrintProxy* ServiceProcess::GetCloudPrintProxy() {
if (!cloud_print_proxy_.get()) {
cloud_print_proxy_.reset(new cloud_print::CloudPrintProxy());
cloud_print_proxy_->Initialize(service_prefs_.get(), this);
}
return cloud_print_proxy_.get();
}
void ServiceProcess::OnCloudPrintProxyEnabled(bool persist_state) {
if (persist_state) {
service_prefs_->SetBoolean(prefs::kCloudPrintProxyEnabled, true);
service_prefs_->WritePrefs();
}
OnServiceEnabled();
}
void ServiceProcess::OnCloudPrintProxyDisabled(bool persist_state) {
if (persist_state) {
service_prefs_->SetBoolean(prefs::kCloudPrintProxyEnabled, false);
service_prefs_->WritePrefs();
}
OnServiceDisabled();
}
ServiceURLRequestContextGetter*
ServiceProcess::GetServiceURLRequestContextGetter() {
DCHECK(request_context_getter_.get());
return request_context_getter_.get();
}
void ServiceProcess::OnServiceEnabled() {
enabled_services_++;
if ((1 == enabled_services_) &&
!CommandLine::ForCurrentProcess()->HasSwitch(
switches::kNoServiceAutorun)) {
if (!service_process_state_->AddToAutoRun()) {
LOG(ERROR) << "Unable to AddToAutoRun";
}
}
}
void ServiceProcess::OnServiceDisabled() {
DCHECK_NE(enabled_services_, 0);
enabled_services_--;
if (0 == enabled_services_) {
if (!service_process_state_->RemoveFromAutoRun()) {
LOG(ERROR) << "Unable to RemoveFromAutoRun";
}
ScheduleShutdownCheck();
}
}
void ServiceProcess::ScheduleShutdownCheck() {
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&ServiceProcess::ShutdownIfNeeded, base::Unretained(this)),
base::TimeDelta::FromSeconds(kShutdownDelaySeconds));
}
void ServiceProcess::ShutdownIfNeeded() {
if (0 == enabled_services_) {
if (ipc_server_->is_client_connected()) {
ScheduleShutdownCheck();
} else {
Shutdown();
}
}
}
void ServiceProcess::ScheduleCloudPrintPolicyCheck() {
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&ServiceProcess::CloudPrintPolicyCheckIfNeeded,
base::Unretained(this)),
base::TimeDelta::FromHours(kPolicyCheckDelayHours));
}
void ServiceProcess::CloudPrintPolicyCheckIfNeeded() {
if (enabled_services_ && !ipc_server_->is_client_connected()) {
GetCloudPrintProxy()->CheckCloudPrintProxyPolicy();
}
ScheduleCloudPrintPolicyCheck();
}
ServiceProcess::~ServiceProcess() {
Teardown();
g_service_process = NULL;
}