Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / printing / cloud_print / cloud_print_proxy_service.cc
blob323a4439112453e7c3e717c7a67d363ac4c69b54
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/browser/printing/cloud_print/cloud_print_proxy_service.h"
7 #include <stack>
8 #include <vector>
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/command_line.h"
13 #include "base/files/file_util.h"
14 #include "base/json/json_reader.h"
15 #include "base/location.h"
16 #include "base/metrics/histogram.h"
17 #include "base/prefs/pref_service.h"
18 #include "base/single_thread_task_runner.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/thread_task_runner_handle.h"
21 #include "chrome/browser/browser_process.h"
22 #include "chrome/browser/chrome_notification_types.h"
23 #include "chrome/browser/lifetime/application_lifetime.h"
24 #include "chrome/browser/notifications/desktop_notification_service.h"
25 #include "chrome/browser/notifications/notification.h"
26 #include "chrome/browser/notifications/notification_ui_manager.h"
27 #include "chrome/browser/profiles/profile.h"
28 #include "chrome/browser/service_process/service_process_control.h"
29 #include "chrome/common/chrome_switches.h"
30 #include "chrome/common/cloud_print/cloud_print_proxy_info.h"
31 #include "chrome/common/pref_names.h"
32 #include "chrome/common/service_messages.h"
33 #include "content/public/browser/browser_thread.h"
34 #include "printing/backend/print_backend.h"
36 using content::BrowserThread;
38 CloudPrintProxyService::CloudPrintProxyService(Profile* profile)
39 : profile_(profile),
40 weak_factory_(this) {
43 CloudPrintProxyService::~CloudPrintProxyService() {
46 void CloudPrintProxyService::Initialize() {
47 DCHECK_CURRENTLY_ON(BrowserThread::UI);
48 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
49 ServiceProcessControl::SERVICE_EVENT_INITIALIZE,
50 ServiceProcessControl::SERVICE_EVENT_MAX);
51 if (profile_->GetPrefs()->HasPrefPath(prefs::kCloudPrintEmail) &&
52 (!profile_->GetPrefs()->GetString(prefs::kCloudPrintEmail).empty() ||
53 !profile_->GetPrefs()->GetBoolean(prefs::kCloudPrintProxyEnabled))) {
54 // If the cloud print proxy is enabled, or the policy preventing it from
55 // being enabled is set, establish a channel with the service process and
56 // update the status. This will check the policy when the status is sent
57 // back.
58 UMA_HISTOGRAM_ENUMERATION(
59 "CloudPrint.ServiceEvents",
60 ServiceProcessControl::SERVICE_EVENT_ENABLED_ON_LAUNCH,
61 ServiceProcessControl::SERVICE_EVENT_MAX);
62 RefreshStatusFromService();
65 pref_change_registrar_.Init(profile_->GetPrefs());
66 pref_change_registrar_.Add(
67 prefs::kCloudPrintProxyEnabled,
68 base::Bind(
69 base::IgnoreResult(
70 &CloudPrintProxyService::ApplyCloudPrintConnectorPolicy),
71 base::Unretained(this)));
74 void CloudPrintProxyService::RefreshStatusFromService() {
75 DCHECK_CURRENTLY_ON(BrowserThread::UI);
76 InvokeServiceTask(
77 base::Bind(&CloudPrintProxyService::RefreshCloudPrintProxyStatus,
78 weak_factory_.GetWeakPtr()));
81 void CloudPrintProxyService::EnableForUserWithRobot(
82 const std::string& robot_auth_code,
83 const std::string& robot_email,
84 const std::string& user_email,
85 const base::DictionaryValue& user_preferences) {
86 DCHECK_CURRENTLY_ON(BrowserThread::UI);
87 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
88 ServiceProcessControl::SERVICE_EVENT_ENABLE,
89 ServiceProcessControl::SERVICE_EVENT_MAX);
90 if (profile_->GetPrefs()->GetBoolean(prefs::kCloudPrintProxyEnabled)) {
91 InvokeServiceTask(
92 base::Bind(&CloudPrintProxyService::EnableCloudPrintProxyWithRobot,
93 weak_factory_.GetWeakPtr(), robot_auth_code, robot_email,
94 user_email, base::Owned(user_preferences.DeepCopy())));
98 void CloudPrintProxyService::DisableForUser() {
99 DCHECK_CURRENTLY_ON(BrowserThread::UI);
100 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
101 ServiceProcessControl::SERVICE_EVENT_DISABLE,
102 ServiceProcessControl::SERVICE_EVENT_MAX);
103 InvokeServiceTask(
104 base::Bind(&CloudPrintProxyService::DisableCloudPrintProxy,
105 weak_factory_.GetWeakPtr()));
108 bool CloudPrintProxyService::ApplyCloudPrintConnectorPolicy() {
109 DCHECK_CURRENTLY_ON(BrowserThread::UI);
110 if (!profile_->GetPrefs()->GetBoolean(prefs::kCloudPrintProxyEnabled)) {
111 std::string email =
112 profile_->GetPrefs()->GetString(prefs::kCloudPrintEmail);
113 if (!email.empty()) {
114 UMA_HISTOGRAM_ENUMERATION(
115 "CloudPrint.ServiceEvents",
116 ServiceProcessControl::SERVICE_EVENT_DISABLE_BY_POLICY,
117 ServiceProcessControl::SERVICE_EVENT_MAX);
118 DisableForUser();
119 profile_->GetPrefs()->SetString(prefs::kCloudPrintEmail, std::string());
120 return false;
123 return true;
126 void CloudPrintProxyService::GetPrinters(const PrintersCallback& callback) {
127 DCHECK_CURRENTLY_ON(BrowserThread::UI);
128 if (!profile_->GetPrefs()->GetBoolean(prefs::kCloudPrintProxyEnabled))
129 return;
131 base::FilePath list_path(
132 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
133 switches::kCloudPrintSetupProxy));
134 if (!list_path.empty()) {
135 std::string printers_json;
136 base::ReadFileToString(list_path, &printers_json);
137 scoped_ptr<base::Value> value = base::JSONReader::Read(printers_json);
138 base::ListValue* list = NULL;
139 std::vector<std::string> printers;
140 if (value && value->GetAsList(&list) && list) {
141 for (size_t i = 0; i < list->GetSize(); ++i) {
142 std::string printer;
143 if (list->GetString(i, &printer))
144 printers.push_back(printer);
147 UMA_HISTOGRAM_COUNTS_10000("CloudPrint.AvailablePrintersList",
148 printers.size());
149 base::ThreadTaskRunnerHandle::Get()->PostTask(
150 FROM_HERE, base::Bind(callback, printers));
151 } else {
152 InvokeServiceTask(
153 base::Bind(&CloudPrintProxyService::GetCloudPrintProxyPrinters,
154 weak_factory_.GetWeakPtr(),
155 callback));
159 void CloudPrintProxyService::GetCloudPrintProxyPrinters(
160 const PrintersCallback& callback) {
161 DCHECK_CURRENTLY_ON(BrowserThread::UI);
162 ServiceProcessControl* process_control = GetServiceProcessControl();
163 DCHECK(process_control->IsConnected());
164 process_control->GetPrinters(callback);
167 void CloudPrintProxyService::RefreshCloudPrintProxyStatus() {
168 DCHECK_CURRENTLY_ON(BrowserThread::UI);
169 ServiceProcessControl* process_control = GetServiceProcessControl();
170 DCHECK(process_control->IsConnected());
171 ServiceProcessControl::CloudPrintProxyInfoCallback callback = base::Bind(
172 &CloudPrintProxyService::ProxyInfoCallback, base::Unretained(this));
173 process_control->GetCloudPrintProxyInfo(callback);
176 void CloudPrintProxyService::EnableCloudPrintProxyWithRobot(
177 const std::string& robot_auth_code,
178 const std::string& robot_email,
179 const std::string& user_email,
180 const base::DictionaryValue* user_preferences) {
181 ServiceProcessControl* process_control = GetServiceProcessControl();
182 DCHECK(process_control->IsConnected());
183 process_control->Send(
184 new ServiceMsg_EnableCloudPrintProxyWithRobot(
185 robot_auth_code, robot_email, user_email, *user_preferences));
186 // Assume the IPC worked.
187 profile_->GetPrefs()->SetString(prefs::kCloudPrintEmail, user_email);
190 void CloudPrintProxyService::DisableCloudPrintProxy() {
191 ServiceProcessControl* process_control = GetServiceProcessControl();
192 DCHECK(process_control->IsConnected());
193 process_control->Send(new ServiceMsg_DisableCloudPrintProxy);
194 // Assume the IPC worked.
195 profile_->GetPrefs()->SetString(prefs::kCloudPrintEmail, std::string());
198 void CloudPrintProxyService::ProxyInfoCallback(
199 const cloud_print::CloudPrintProxyInfo& proxy_info) {
200 proxy_id_ = proxy_info.proxy_id;
201 profile_->GetPrefs()->SetString(
202 prefs::kCloudPrintEmail,
203 proxy_info.enabled ? proxy_info.email : std::string());
204 ApplyCloudPrintConnectorPolicy();
207 bool CloudPrintProxyService::InvokeServiceTask(const base::Closure& task) {
208 GetServiceProcessControl()->Launch(task, base::Closure());
209 return true;
212 ServiceProcessControl* CloudPrintProxyService::GetServiceProcessControl() {
213 return ServiceProcessControl::GetInstance();