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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
// Copyright (c) 2010 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_setup_flow.h"
#include "app/gfx/font_util.h"
#include "base/json/json_writer.h"
#include "base/singleton.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/dom_ui/chrome_url_data_manager.h"
#include "chrome/browser/dom_ui/dom_ui_util.h"
#if defined(TOOLKIT_GTK)
#include "chrome/browser/gtk/html_dialog_gtk.h"
#endif // defined(TOOLKIT_GTK)
#include "chrome/browser/platform_util.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h"
#include "chrome/browser/printing/cloud_print/cloud_print_setup_message_handler.h"
#include "chrome/browser/printing/cloud_print/cloud_print_setup_source.h"
#include "chrome/browser/printing/cloud_print/cloud_print_url.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/browser/service/service_process_control.h"
#include "chrome/browser/service/service_process_control_manager.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#if defined(TOOLKIT_VIEWS)
#include "chrome/browser/views/browser_dialogs.h"
#endif // defined(TOOLKIT_GTK)
#include "chrome/common/net/gaia/gaia_auth_fetcher.h"
#include "chrome/common/net/gaia/gaia_constants.h"
#include "chrome/common/net/gaia/google_service_auth_error.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/service_messages.h"
#include "gfx/font.h"
#include "grit/chromium_strings.h"
#include "grit/locale_settings.h"
static const wchar_t kGaiaLoginIFrameXPath[] = L"//iframe[@id='gaialogin']";
static const wchar_t kDoneIframeXPath[] = L"//iframe[@id='setupdone']";
////////////////////////////////////////////////////////////////////////////////
// CloudPrintSetupFlow implementation.
// static
CloudPrintSetupFlow* CloudPrintSetupFlow::OpenDialog(
Profile* profile, Delegate* delegate, gfx::NativeWindow parent_window) {
// Set the arguments for showing the gaia login page.
DictionaryValue args;
args.SetString("user", "");
args.SetInteger("error", 0);
args.SetBoolean("editable_user", true);
bool setup_done = false;
if (profile->GetPrefs()->HasPrefPath(prefs::kCloudPrintEmail) &&
!profile->GetPrefs()->GetString(prefs::kCloudPrintEmail).empty())
setup_done = true;
args.SetString("pageToShow", setup_done ? "setupdone" : "cloudprintsetup");
std::string json_args;
base::JSONWriter::Write(&args, false, &json_args);
CloudPrintSetupFlow* flow = new CloudPrintSetupFlow(json_args, profile,
delegate, setup_done);
// We may not always have a browser. This can happen when we are being
// invoked in the context of a "token expired" notfication. If we don't have
// a brower, use the underlying dialog system to show the dialog without
// using a browser.
Browser* browser = BrowserList::GetLastActive();
if (browser) {
browser->BrowserShowHtmlDialog(flow, parent_window);
} else {
#if defined(TOOLKIT_VIEWS)
browser::ShowHtmlDialogView(parent_window, profile, flow);
#elif defined(TOOLKIT_GTK)
HtmlDialogGtk* html_dialog =
new HtmlDialogGtk(profile, flow, parent_window);
html_dialog->InitDialog();
#endif // defined(TOOLKIT_VIEWS)
// TODO(sanjeevr): Implement the "no browser" scenario for the Mac.
}
return flow;
}
CloudPrintSetupFlow::CloudPrintSetupFlow(const std::string& args,
Profile* profile,
Delegate* delegate,
bool setup_done)
: dom_ui_(NULL),
dialog_start_args_(args),
setup_done_(setup_done),
process_control_(NULL),
delegate_(delegate) {
// TODO(hclam): The data source should be added once.
profile_ = profile;
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
NewRunnableMethod(Singleton<ChromeURLDataManager>::get(),
&ChromeURLDataManager::AddDataSource,
make_scoped_refptr(new CloudPrintSetupSource())));
}
CloudPrintSetupFlow::~CloudPrintSetupFlow() {
}
void CloudPrintSetupFlow::Focus() {
// TODO(pranavk): implement this method.
NOTIMPLEMENTED();
}
///////////////////////////////////////////////////////////////////////////////
// HtmlDialogUIDelegate implementation.
GURL CloudPrintSetupFlow::GetDialogContentURL() const {
return GURL("chrome://cloudprintsetup/setupflow");
}
void CloudPrintSetupFlow::GetDOMMessageHandlers(
std::vector<DOMMessageHandler*>* handlers) const {
// Create the message handler only after we are asked, the caller is
// responsible for deleting the objects.
handlers->push_back(
new CloudPrintSetupMessageHandler(
const_cast<CloudPrintSetupFlow*>(this)));
}
void CloudPrintSetupFlow::GetDialogSize(gfx::Size* size) const {
PrefService* prefs = profile_->GetPrefs();
gfx::Font approximate_web_font(
UTF8ToWide(prefs->GetString(prefs::kWebKitSansSerifFontFamily)),
prefs->GetInteger(prefs::kWebKitDefaultFontSize));
if (setup_done_) {
*size = gfx::GetLocalizedContentsSizeForFont(
IDS_CLOUD_PRINT_SETUP_WIZARD_DONE_WIDTH_CHARS,
IDS_CLOUD_PRINT_SETUP_WIZARD_DONE_HEIGHT_LINES,
approximate_web_font);
} else {
*size = gfx::GetLocalizedContentsSizeForFont(
IDS_CLOUD_PRINT_SETUP_WIZARD_WIDTH_CHARS,
IDS_CLOUD_PRINT_SETUP_WIZARD_HEIGHT_LINES,
approximate_web_font);
}
}
// A callback to notify the delegate that the dialog closed.
void CloudPrintSetupFlow::OnDialogClosed(const std::string& json_retval) {
// If we are fetching the token then cancel the request.
if (authenticator_.get())
authenticator_->CancelRequest();
if (delegate_) {
delegate_->OnDialogClosed();
}
delete this;
}
std::string CloudPrintSetupFlow::GetDialogArgs() const {
return dialog_start_args_;
}
void CloudPrintSetupFlow::OnCloseContents(TabContents* source,
bool* out_close_dialog) {
}
std::wstring CloudPrintSetupFlow::GetDialogTitle() const {
return l10n_util::GetString(IDS_CLOUD_PRINT_SETUP_DIALOG_TITLE);
}
bool CloudPrintSetupFlow::IsDialogModal() const {
// We are always modeless.
return false;
}
///////////////////////////////////////////////////////////////////////////////
// GaiaAuthConsumer implementation.
void CloudPrintSetupFlow::OnClientLoginFailure(
const GoogleServiceAuthError& error) {
ShowGaiaFailed(error);
authenticator_.reset();
}
void CloudPrintSetupFlow::OnClientLoginSuccess(
const GaiaAuthConsumer::ClientLoginResult& credentials) {
// Save the token for the cloud print proxy.
lsid_ = credentials.lsid;
// Show that Gaia login has succeeded.
ShowGaiaSuccessAndSettingUp();
authenticator_.reset();
profile_->GetCloudPrintProxyService()->EnableForUser(credentials.lsid,
login_);
// TODO(sanjeevr): Should we wait and verify that the enable succeeded?
ShowSetupDone();
}
///////////////////////////////////////////////////////////////////////////////
// Methods called by CloudPrintSetupMessageHandler
void CloudPrintSetupFlow::Attach(DOMUI* dom_ui) {
dom_ui_ = dom_ui;
}
void CloudPrintSetupFlow::OnUserSubmittedAuth(const std::string& user,
const std::string& password,
const std::string& captcha) {
// Save the login name only.
login_ = user;
// Start the authenticator.
authenticator_.reset(
new GaiaAuthFetcher(this, GaiaConstants::kChromeSource,
profile_->GetRequestContext()));
authenticator_->StartClientLogin(user, password,
GaiaConstants::kCloudPrintService,
"", captcha,
GaiaAuthFetcher::HostedAccountsAllowed);
}
void CloudPrintSetupFlow::OnUserClickedLearnMore() {
dom_ui_->tab_contents()->OpenURL(CloudPrintURL::GetCloudPrintLearnMoreURL(),
GURL(), NEW_FOREGROUND_TAB,
PageTransition::LINK);
}
void CloudPrintSetupFlow::OnUserClickedPrintTestPage() {
dom_ui_->tab_contents()->OpenURL(CloudPrintURL::GetCloudPrintTestPageURL(),
GURL(), NEW_FOREGROUND_TAB,
PageTransition::LINK);
}
///////////////////////////////////////////////////////////////////////////////
// Helper methods for showing contents of the DOM UI
void CloudPrintSetupFlow::ShowGaiaLogin(const DictionaryValue& args) {
if (dom_ui_)
dom_ui_->CallJavascriptFunction(L"cloudprint.showSetupLogin");
std::string json;
base::JSONWriter::Write(&args, false, &json);
std::wstring javascript = std::wstring(L"showGaiaLogin") +
L"(" + UTF8ToWide(json) + L");";
ExecuteJavascriptInIFrame(kGaiaLoginIFrameXPath, javascript);
}
void CloudPrintSetupFlow::ShowGaiaSuccessAndSettingUp() {
ExecuteJavascriptInIFrame(kGaiaLoginIFrameXPath,
L"showGaiaSuccessAndSettingUp();");
}
void CloudPrintSetupFlow::ShowGaiaFailed(const GoogleServiceAuthError& error) {
DictionaryValue args;
args.SetString("pageToShow", "cloudprintsetup");
args.SetString("user", "");
args.SetInteger("error", error.state());
args.SetBoolean("editable_user", true);
args.SetString("captchaUrl", error.captcha().image_url.spec());
ShowGaiaLogin(args);
}
void CloudPrintSetupFlow::ShowSetupDone() {
setup_done_ = true;
std::wstring product_name = l10n_util::GetString(IDS_PRODUCT_NAME);
std::wstring message = l10n_util::GetStringF(IDS_CLOUD_PRINT_SETUP_DONE,
product_name,
UTF8ToWide(login_));
std::wstring javascript = L"cloudprint.setMessage('" + message + L"');";
ExecuteJavascriptInIFrame(kDoneIframeXPath, javascript);
if (dom_ui_) {
PrefService* prefs = profile_->GetPrefs();
gfx::Font approximate_web_font(
UTF8ToWide(prefs->GetString(prefs::kWebKitSansSerifFontFamily)),
prefs->GetInteger(prefs::kWebKitDefaultFontSize));
gfx::Size done_size = gfx::GetLocalizedContentsSizeForFont(
IDS_CLOUD_PRINT_SETUP_WIZARD_DONE_WIDTH_CHARS,
IDS_CLOUD_PRINT_SETUP_WIZARD_DONE_HEIGHT_LINES,
approximate_web_font);
FundamentalValue new_width(done_size.width());
FundamentalValue new_height(done_size.height());
dom_ui_->CallJavascriptFunction(L"cloudprint.showSetupDone",
new_width, new_height);
}
ExecuteJavascriptInIFrame(kDoneIframeXPath, L"cloudprint.onPageShown();");
}
void CloudPrintSetupFlow::ExecuteJavascriptInIFrame(
const std::wstring& iframe_xpath,
const std::wstring& js) {
if (dom_ui_) {
RenderViewHost* rvh = dom_ui_->tab_contents()->render_view_host();
rvh->ExecuteJavascriptInWebFrame(iframe_xpath, js);
}
}
|