OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h" | 5 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h" |
6 | 6 |
7 #include <stack> | 7 #include <stack> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/notifications/desktop_notification_service.h" | 14 #include "chrome/browser/notifications/desktop_notification_service.h" |
15 #include "chrome/browser/notifications/notification.h" | 15 #include "chrome/browser/notifications/notification.h" |
16 #include "chrome/browser/notifications/notification_ui_manager.h" | 16 #include "chrome/browser/notifications/notification_ui_manager.h" |
17 #include "chrome/browser/prefs/pref_service.h" | 17 #include "chrome/browser/prefs/pref_service.h" |
18 #include "chrome/browser/printing/cloud_print/cloud_print_setup_flow.h" | 18 #include "chrome/browser/printing/cloud_print/cloud_print_setup_flow.h" |
19 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
20 #include "chrome/browser/service/service_process_control.h" | 20 #include "chrome/browser/service/service_process_control.h" |
21 #include "chrome/browser/ui/browser_list.h" | 21 #include "chrome/browser/ui/browser_list.h" |
22 #include "chrome/common/chrome_notification_types.h" | |
22 #include "chrome/common/cloud_print/cloud_print_proxy_info.h" | 23 #include "chrome/common/cloud_print/cloud_print_proxy_info.h" |
23 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
24 #include "chrome/common/service_messages.h" | 25 #include "chrome/common/service_messages.h" |
25 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
26 #include "grit/generated_resources.h" | 27 #include "grit/generated_resources.h" |
27 #include "ui/base/l10n/l10n_util.h" | 28 #include "ui/base/l10n/l10n_util.h" |
28 | 29 |
29 using content::BrowserThread; | 30 using content::BrowserThread; |
30 | 31 |
31 // TODO(sanjeevr): Localize the product name? | 32 // TODO(sanjeevr): Localize the product name? |
(...skipping 27 matching lines...) Expand all Loading... | |
59 : profile_(profile), | 60 : profile_(profile), |
60 token_expired_delegate_(NULL), | 61 token_expired_delegate_(NULL), |
61 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 62 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
62 } | 63 } |
63 | 64 |
64 CloudPrintProxyService::~CloudPrintProxyService() { | 65 CloudPrintProxyService::~CloudPrintProxyService() { |
65 } | 66 } |
66 | 67 |
67 void CloudPrintProxyService::Initialize() { | 68 void CloudPrintProxyService::Initialize() { |
68 if (profile_->GetPrefs()->HasPrefPath(prefs::kCloudPrintEmail) && | 69 if (profile_->GetPrefs()->HasPrefPath(prefs::kCloudPrintEmail) && |
69 !profile_->GetPrefs()->GetString(prefs::kCloudPrintEmail).empty()) { | 70 (!profile_->GetPrefs()->GetString(prefs::kCloudPrintEmail).empty() || |
70 // If the cloud print proxy is enabled, establish a channel with the | 71 !profile_->GetPrefs()->GetBoolean(prefs::kCloudPrintProxyEnabled))) { |
71 // service process and update the status. | 72 // If the cloud print proxy is enabled, or the policy preventing it from |
73 // being enabled is set, establish a channel with the service process and | |
74 // update the status. This will check the policy when the status is sent | |
75 // back. | |
72 RefreshStatusFromService(); | 76 RefreshStatusFromService(); |
73 } | 77 } |
78 | |
79 pref_change_registrar_.Init(profile_->GetPrefs()); | |
80 pref_change_registrar_.Add(prefs::kCloudPrintProxyEnabled, this); | |
74 } | 81 } |
75 | 82 |
76 void CloudPrintProxyService::RefreshStatusFromService() { | 83 void CloudPrintProxyService::RefreshStatusFromService() { |
77 InvokeServiceTask( | 84 InvokeServiceTask( |
78 base::Bind(&CloudPrintProxyService::RefreshCloudPrintProxyStatus, | 85 base::Bind(&CloudPrintProxyService::RefreshCloudPrintProxyStatus, |
79 weak_factory_.GetWeakPtr())); | 86 weak_factory_.GetWeakPtr())); |
80 } | 87 } |
81 | 88 |
82 void CloudPrintProxyService::EnableForUser(const std::string& lsid, | 89 void CloudPrintProxyService::EnableForUser(const std::string& lsid, |
83 const std::string& email) { | 90 const std::string& email) { |
84 InvokeServiceTask( | 91 if (profile_->GetPrefs()->GetBoolean(prefs::kCloudPrintProxyEnabled)) { |
85 base::Bind(&CloudPrintProxyService::EnableCloudPrintProxy, | 92 InvokeServiceTask( |
86 weak_factory_.GetWeakPtr(), lsid, email)); | 93 base::Bind(&CloudPrintProxyService::EnableCloudPrintProxy, |
94 weak_factory_.GetWeakPtr(), lsid, email)); | |
95 } | |
87 } | 96 } |
88 | 97 |
89 void CloudPrintProxyService::EnableForUserWithRobot( | 98 void CloudPrintProxyService::EnableForUserWithRobot( |
90 const std::string& robot_auth_code, | 99 const std::string& robot_auth_code, |
91 const std::string& robot_email, | 100 const std::string& robot_email, |
92 const std::string& user_email) { | 101 const std::string& user_email) { |
93 InvokeServiceTask( | 102 if (profile_->GetPrefs()->GetBoolean(prefs::kCloudPrintProxyEnabled)) { |
94 base::Bind(&CloudPrintProxyService::EnableCloudPrintProxyWithRobot, | 103 InvokeServiceTask( |
95 weak_factory_.GetWeakPtr(), robot_auth_code, robot_email, | 104 base::Bind(&CloudPrintProxyService::EnableCloudPrintProxyWithRobot, |
96 user_email)); | 105 weak_factory_.GetWeakPtr(), robot_auth_code, robot_email, |
106 user_email)); | |
107 } | |
97 } | 108 } |
98 | 109 |
99 | |
100 void CloudPrintProxyService::DisableForUser() { | 110 void CloudPrintProxyService::DisableForUser() { |
101 InvokeServiceTask( | 111 InvokeServiceTask( |
102 base::Bind(&CloudPrintProxyService::DisableCloudPrintProxy, | 112 base::Bind(&CloudPrintProxyService::DisableCloudPrintProxy, |
103 weak_factory_.GetWeakPtr())); | 113 weak_factory_.GetWeakPtr())); |
104 } | 114 } |
105 | 115 |
106 bool CloudPrintProxyService::ShowTokenExpiredNotification() { | 116 bool CloudPrintProxyService::ShowTokenExpiredNotification() { |
107 // If we already have a pending notification, don't show another one. | 117 // If we already have a pending notification, don't show another one. |
108 if (token_expired_delegate_.get()) | 118 if (token_expired_delegate_.get()) |
109 return false; | 119 return false; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 void CloudPrintProxyService::TokenExpiredNotificationDone(bool keep_alive) { | 156 void CloudPrintProxyService::TokenExpiredNotificationDone(bool keep_alive) { |
147 if (token_expired_delegate_.get()) { | 157 if (token_expired_delegate_.get()) { |
148 g_browser_process->notification_ui_manager()->CancelById( | 158 g_browser_process->notification_ui_manager()->CancelById( |
149 token_expired_delegate_->id()); | 159 token_expired_delegate_->id()); |
150 token_expired_delegate_ = NULL; | 160 token_expired_delegate_ = NULL; |
151 if (!keep_alive) | 161 if (!keep_alive) |
152 BrowserList::EndKeepAlive(); | 162 BrowserList::EndKeepAlive(); |
153 } | 163 } |
154 } | 164 } |
155 | 165 |
166 void CloudPrintProxyService::ApplyCloudPrintConnectorPolicy() { | |
167 if (!profile_->GetPrefs()->GetBoolean(prefs::kCloudPrintProxyEnabled)) { | |
168 std::string email; | |
Peter Kasting
2011/11/21 20:51:32
Nit: Combine this line with the next
| |
169 email = profile_->GetPrefs()->GetString(prefs::kCloudPrintEmail); | |
170 if (!email.empty()) { | |
171 DisableForUser(); | |
172 profile_->GetPrefs()->SetString(prefs::kCloudPrintEmail, std::string()); | |
173 } | |
174 } | |
175 } | |
176 | |
156 void CloudPrintProxyService::OnCloudPrintSetupClosed() { | 177 void CloudPrintProxyService::OnCloudPrintSetupClosed() { |
157 MessageLoop::current()->PostTask( | 178 MessageLoop::current()->PostTask( |
158 FROM_HERE, base::Bind(&BrowserList::EndKeepAlive)); | 179 FROM_HERE, base::Bind(&BrowserList::EndKeepAlive)); |
159 } | 180 } |
160 | 181 |
182 void CloudPrintProxyService::Observe( | |
183 int type, | |
184 const content::NotificationSource& source, | |
185 const content::NotificationDetails& details) { | |
186 if (type == chrome::NOTIFICATION_PREF_CHANGED) { | |
Peter Kasting
2011/11/21 20:51:32
Nit: Convert this conditional to a simple:
DCHE
| |
187 ApplyCloudPrintConnectorPolicy(); | |
188 } else { | |
189 NOTREACHED(); | |
190 } | |
191 } | |
192 | |
161 void CloudPrintProxyService::RefreshCloudPrintProxyStatus() { | 193 void CloudPrintProxyService::RefreshCloudPrintProxyStatus() { |
162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
163 ServiceProcessControl* process_control = ServiceProcessControl::GetInstance(); | 195 ServiceProcessControl* process_control = GetServiceProcessControl(); |
164 DCHECK(process_control->is_connected()); | 196 DCHECK(process_control->is_connected()); |
165 ServiceProcessControl::CloudPrintProxyInfoHandler callback = | 197 ServiceProcessControl::CloudPrintProxyInfoHandler callback = |
166 base::Bind(&CloudPrintProxyService::ProxyInfoCallback, | 198 base::Bind(&CloudPrintProxyService::ProxyInfoCallback, |
167 base::Unretained(this)); | 199 base::Unretained(this)); |
168 // GetCloudPrintProxyInfo takes ownership of callback. | 200 // GetCloudPrintProxyInfo takes ownership of callback. |
169 process_control->GetCloudPrintProxyInfo(callback); | 201 process_control->GetCloudPrintProxyInfo(callback); |
170 } | 202 } |
171 | 203 |
172 void CloudPrintProxyService::EnableCloudPrintProxy(const std::string& lsid, | 204 void CloudPrintProxyService::EnableCloudPrintProxy(const std::string& lsid, |
173 const std::string& email) { | 205 const std::string& email) { |
174 ServiceProcessControl* process_control = ServiceProcessControl::GetInstance(); | 206 ServiceProcessControl* process_control = GetServiceProcessControl(); |
175 DCHECK(process_control->is_connected()); | 207 DCHECK(process_control->is_connected()); |
176 process_control->Send(new ServiceMsg_EnableCloudPrintProxy(lsid)); | 208 process_control->Send(new ServiceMsg_EnableCloudPrintProxy(lsid)); |
177 // Assume the IPC worked. | 209 // Assume the IPC worked. |
178 profile_->GetPrefs()->SetString(prefs::kCloudPrintEmail, email); | 210 profile_->GetPrefs()->SetString(prefs::kCloudPrintEmail, email); |
179 } | 211 } |
180 | 212 |
181 void CloudPrintProxyService::EnableCloudPrintProxyWithRobot( | 213 void CloudPrintProxyService::EnableCloudPrintProxyWithRobot( |
182 const std::string& robot_auth_code, | 214 const std::string& robot_auth_code, |
183 const std::string& robot_email, | 215 const std::string& robot_email, |
184 const std::string& user_email) { | 216 const std::string& user_email) { |
185 ServiceProcessControl* process_control = ServiceProcessControl::GetInstance(); | 217 ServiceProcessControl* process_control = GetServiceProcessControl(); |
186 DCHECK(process_control->is_connected()); | 218 DCHECK(process_control->is_connected()); |
187 process_control->Send(new ServiceMsg_EnableCloudPrintProxyWithRobot( | 219 process_control->Send(new ServiceMsg_EnableCloudPrintProxyWithRobot( |
188 robot_auth_code, | 220 robot_auth_code, |
189 robot_email, | 221 robot_email, |
190 user_email)); | 222 user_email)); |
191 // Assume the IPC worked. | 223 // Assume the IPC worked. |
192 profile_->GetPrefs()->SetString(prefs::kCloudPrintEmail, user_email); | 224 profile_->GetPrefs()->SetString(prefs::kCloudPrintEmail, user_email); |
193 } | 225 } |
194 | 226 |
195 | |
196 void CloudPrintProxyService::DisableCloudPrintProxy() { | 227 void CloudPrintProxyService::DisableCloudPrintProxy() { |
197 ServiceProcessControl* process_control = ServiceProcessControl::GetInstance(); | 228 ServiceProcessControl* process_control = GetServiceProcessControl(); |
198 DCHECK(process_control->is_connected()); | 229 DCHECK(process_control->is_connected()); |
199 process_control->Send(new ServiceMsg_DisableCloudPrintProxy); | 230 process_control->Send(new ServiceMsg_DisableCloudPrintProxy); |
200 // Assume the IPC worked. | 231 // Assume the IPC worked. |
201 profile_->GetPrefs()->SetString(prefs::kCloudPrintEmail, std::string()); | 232 profile_->GetPrefs()->SetString(prefs::kCloudPrintEmail, std::string()); |
202 } | 233 } |
203 | 234 |
204 void CloudPrintProxyService::ProxyInfoCallback( | 235 void CloudPrintProxyService::ProxyInfoCallback( |
205 const cloud_print::CloudPrintProxyInfo& proxy_info) { | 236 const cloud_print::CloudPrintProxyInfo& proxy_info) { |
206 proxy_id_ = proxy_info.proxy_id; | 237 proxy_id_ = proxy_info.proxy_id; |
207 profile_->GetPrefs()->SetString( | 238 profile_->GetPrefs()->SetString( |
208 prefs::kCloudPrintEmail, | 239 prefs::kCloudPrintEmail, |
209 proxy_info.enabled ? proxy_info.email : std::string()); | 240 proxy_info.enabled ? proxy_info.email : std::string()); |
241 ApplyCloudPrintConnectorPolicy(); | |
210 } | 242 } |
211 | 243 |
212 bool CloudPrintProxyService::InvokeServiceTask(const base::Closure& task) { | 244 bool CloudPrintProxyService::InvokeServiceTask(const base::Closure& task) { |
213 ServiceProcessControl::GetInstance()->Launch(task, base::Closure()); | 245 GetServiceProcessControl()->Launch(task, base::Closure()); |
214 return true; | 246 return true; |
215 } | 247 } |
248 | |
249 ServiceProcessControl* CloudPrintProxyService::GetServiceProcessControl() { | |
250 return ServiceProcessControl::GetInstance(); | |
251 } | |
OLD | NEW |