1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h"
#include <stack>
#include <vector>
#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/notifications/desktop_notification_service.h"
#include "chrome/browser/notifications/notification.h"
#include "chrome/browser/notifications/notification_ui_manager.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/printing/cloud_print/cloud_print_setup_flow.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/service/service_process_control.h"
#include "chrome/browser/service/service_process_control_manager.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/service_messages.h"
#include "content/browser/browser_thread.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
// TODO(sanjeevr): Localize the product name?
const char kCloudPrintProductName[] = "Google Cloud Print";
class CloudPrintProxyService::TokenExpiredNotificationDelegate
: public NotificationDelegate {
public:
explicit TokenExpiredNotificationDelegate(
CloudPrintProxyService* cloud_print_service)
: cloud_print_service_(cloud_print_service) {
}
void Display() {}
void Error() {
cloud_print_service_->OnTokenExpiredNotificationError();
}
void Close(bool by_user) {
cloud_print_service_->OnTokenExpiredNotificationClosed(by_user);
}
void Click() {
cloud_print_service_->OnTokenExpiredNotificationClick();
}
std::string id() const { return "cloudprint.tokenexpired"; }
private:
CloudPrintProxyService* cloud_print_service_;
DISALLOW_COPY_AND_ASSIGN(TokenExpiredNotificationDelegate);
};
CloudPrintProxyService::CloudPrintProxyService(Profile* profile)
: profile_(profile), token_expired_delegate_(NULL) {
}
CloudPrintProxyService::~CloudPrintProxyService() {
}
void CloudPrintProxyService::Initialize() {
if (profile_->GetPrefs()->HasPrefPath(prefs::kCloudPrintEmail) &&
!profile_->GetPrefs()->GetString(prefs::kCloudPrintEmail).empty()) {
// If the cloud print proxy is enabled, establish a channel with the
// service process and update the status.
RefreshStatusFromService();
}
}
void CloudPrintProxyService::RefreshStatusFromService() {
InvokeServiceTask(
NewRunnableMethod(
this, &CloudPrintProxyService::RefreshCloudPrintProxyStatus));
}
void CloudPrintProxyService::EnableForUser(const std::string& lsid,
const std::string& email) {
InvokeServiceTask(
NewRunnableMethod(
this, &CloudPrintProxyService::EnableCloudPrintProxy, lsid, email));
}
void CloudPrintProxyService::DisableForUser() {
InvokeServiceTask(
NewRunnableMethod(
this, &CloudPrintProxyService::DisableCloudPrintProxy));
}
bool CloudPrintProxyService::ShowTokenExpiredNotification() {
// If we already have a pending notification, don't show another one.
if (token_expired_delegate_.get())
return false;
// TODO(sanjeevr): Get icon for this notification.
GURL icon_url;
string16 title = UTF8ToUTF16(kCloudPrintProductName);
string16 message =
l10n_util::GetStringUTF16(IDS_CLOUD_PRINT_TOKEN_EXPIRED_MESSAGE);
string16 content_url = DesktopNotificationService::CreateDataUrl(
icon_url, title, message, WebKit::WebTextDirectionDefault);
token_expired_delegate_ = new TokenExpiredNotificationDelegate(this);
Notification notification(GURL(), GURL(content_url), string16(), string16(),
token_expired_delegate_.get());
g_browser_process->notification_ui_manager()->Add(notification, profile_);
// Keep the browser alive while we are showing the notification.
BrowserList::StartKeepAlive();
return true;
}
void CloudPrintProxyService::OnTokenExpiredNotificationError() {
TokenExpiredNotificationDone(false);
}
void CloudPrintProxyService::OnTokenExpiredNotificationClosed(bool by_user) {
TokenExpiredNotificationDone(false);
}
void CloudPrintProxyService::OnTokenExpiredNotificationClick() {
TokenExpiredNotificationDone(true);
// Clear the cached cloud print email pref so that the cloud print setup
// flow happens.
profile_->GetPrefs()->SetString(prefs::kCloudPrintEmail, std::string());
cloud_print_setup_handler_.reset(new CloudPrintSetupHandler(this));
CloudPrintSetupFlow::OpenDialog(
profile_, cloud_print_setup_handler_->AsWeakPtr(), NULL);
}
void CloudPrintProxyService::TokenExpiredNotificationDone(bool keep_alive) {
if (token_expired_delegate_.get()) {
g_browser_process->notification_ui_manager()->CancelById(
token_expired_delegate_->id());
token_expired_delegate_ = NULL;
if (!keep_alive)
BrowserList::EndKeepAlive();
}
}
void CloudPrintProxyService::OnCloudPrintSetupClosed() {
MessageLoop::current()->PostTask(
FROM_HERE, NewRunnableFunction(&BrowserList::EndKeepAlive));
}
void CloudPrintProxyService::RefreshCloudPrintProxyStatus() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
ServiceProcessControl* process_control =
ServiceProcessControlManager::GetInstance()->GetProcessControl(profile_);
DCHECK(process_control->is_connected());
Callback2<bool, std::string>::Type* callback =
NewCallback(this, &CloudPrintProxyService::StatusCallback);
// GetCloudPrintProxyStatus takes ownership of callback.
process_control->GetCloudPrintProxyStatus(callback);
}
void CloudPrintProxyService::EnableCloudPrintProxy(const std::string& lsid,
const std::string& email) {
ServiceProcessControl* process_control =
ServiceProcessControlManager::GetInstance()->GetProcessControl(profile_);
DCHECK(process_control->is_connected());
process_control->Send(new ServiceMsg_EnableCloudPrintProxy(lsid));
// Assume the IPC worked.
profile_->GetPrefs()->SetString(prefs::kCloudPrintEmail, email);
}
void CloudPrintProxyService::DisableCloudPrintProxy() {
ServiceProcessControl* process_control =
ServiceProcessControlManager::GetInstance()->GetProcessControl(profile_);
DCHECK(process_control->is_connected());
process_control->Send(new ServiceMsg_DisableCloudPrintProxy);
// Assume the IPC worked.
profile_->GetPrefs()->SetString(prefs::kCloudPrintEmail, std::string());
}
void CloudPrintProxyService::StatusCallback(bool enabled, std::string email) {
profile_->GetPrefs()->SetString(prefs::kCloudPrintEmail,
enabled ? email : std::string());
}
bool CloudPrintProxyService::InvokeServiceTask(Task* task) {
ServiceProcessControl* process_control =
ServiceProcessControlManager::GetInstance()->GetProcessControl(profile_);
DCHECK(process_control);
if (process_control)
process_control->Launch(task, NULL);
return !!process_control;
}
|