blob: 0dc748640f8067791d7167cc3236a4dab1a530d2 [file] [log] [blame]
[email protected]87ab41e72012-01-04 18:45:111// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]73852b8f2010-05-14 00:38:122// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/printing/print_dialog_cloud.h"
[email protected]443e9312013-05-06 06:17:346
[email protected]73852b8f2010-05-14 00:38:127
[email protected]73852b8f2010-05-14 00:38:128#include "base/base64.h"
[email protected]ba4fc242011-10-04 18:56:569#include "base/bind.h"
10#include "base/bind_helpers.h"
[email protected]65c9d89a2011-04-13 21:02:3911#include "base/command_line.h"
[email protected]73852b8f2010-05-14 00:38:1212#include "base/file_util.h"
13#include "base/json/json_reader.h"
[email protected]3853a4c2013-02-11 17:15:5714#include "base/prefs/pref_service.h"
[email protected]e309f312013-06-07 21:50:0815#include "base/strings/utf_string_conversions.h"
[email protected]73852b8f2010-05-14 00:38:1216#include "base/values.h"
[email protected]70019152012-12-19 11:44:1917#include "chrome/browser/devtools/devtools_window.h"
[email protected]2e6389f2012-05-18 19:41:2518#include "chrome/browser/lifetime/application_lifetime.h"
[email protected]2283eead2010-09-29 23:17:3019#include "chrome/browser/printing/cloud_print/cloud_print_url.h"
[email protected]443e9312013-05-06 06:17:3420#include "chrome/browser/printing/print_dialog_cloud_internal.h"
[email protected]8ecad5e2010-12-02 21:18:3321#include "chrome/browser/profiles/profile.h"
[email protected]e39027a2011-01-24 21:41:5422#include "chrome/browser/profiles/profile_manager.h"
[email protected]508326df2012-05-23 16:01:1923#include "chrome/browser/ui/browser_dialogs.h"
[email protected]65c9d89a2011-04-13 21:02:3924#include "chrome/common/chrome_switches.h"
[email protected]ea161da2010-11-02 21:57:3525#include "chrome/common/pref_names.h"
[email protected]1375e3ab2011-03-24 17:07:2226#include "chrome/common/print_messages.h"
[email protected]73852b8f2010-05-14 00:38:1227#include "chrome/common/url_constants.h"
[email protected]75fee372013-03-06 00:42:4428#include "components/user_prefs/pref_registry_syncable.h"
[email protected]c38831a12011-10-28 12:44:4929#include "content/public/browser/browser_thread.h"
[email protected]cdcb1dee2012-01-04 00:46:2030#include "content/public/browser/navigation_controller.h"
[email protected]022af742011-12-28 18:37:2531#include "content/public/browser/navigation_entry.h"
[email protected]6c2381d2011-10-19 02:52:5332#include "content/public/browser/notification_registrar.h"
33#include "content/public/browser/notification_source.h"
[email protected]0d6e9bd2011-10-18 04:29:1634#include "content/public/browser/notification_types.h"
[email protected]9c1662b2012-03-06 15:44:3335#include "content/public/browser/render_view_host.h"
[email protected]0ec4898e2011-12-30 21:09:2436#include "content/public/browser/web_contents.h"
[email protected]8643e6d2012-01-18 20:26:1037#include "content/public/browser/web_contents_view.h"
[email protected]01ec4ec2012-01-18 04:13:4738#include "content/public/browser/web_ui.h"
[email protected]fab55e72013-05-31 07:06:1839#include "webkit/common/webpreferences.h"
[email protected]520c2022012-03-15 00:13:1540
[email protected]88bfd25b2012-06-22 06:28:3341#if defined(USE_AURA)
42#include "ui/aura/root_window.h"
43#include "ui/aura/window.h"
44#endif
45
[email protected]520c2022012-03-15 00:13:1546#if defined(OS_WIN)
47#include "ui/base/win/foreground_helper.h"
48#endif
49
[email protected]73852b8f2010-05-14 00:38:1250// This module implements the UI support in Chrome for cloud printing.
51// This means hosting a dialog containing HTML/JavaScript and using
52// the published cloud print user interface integration APIs to get
53// page setup settings from the dialog contents and provide the
[email protected]a984bdf2011-03-15 20:17:1654// generated print data to the dialog contents for uploading to the
[email protected]73852b8f2010-05-14 00:38:1255// cloud print service.
56
57// Currently, the flow between these classes is as follows:
58
[email protected]a984bdf2011-03-15 20:17:1659// PrintDialogCloud::CreatePrintDialogForFile is called from
[email protected]73852b8f2010-05-14 00:38:1260// resource_message_filter_gtk.cc once the renderer has informed the
[email protected]a984bdf2011-03-15 20:17:1661// renderer host that print data generation into the renderer host provided
[email protected]032682b2011-01-12 22:05:0262// temp file has been completed. That call is on the FILE thread.
[email protected]73852b8f2010-05-14 00:38:1263// That, in turn, hops over to the UI thread to create an instance of
64// PrintDialogCloud.
65
66// The constructor for PrintDialogCloud creates a
[email protected]5835871a2012-04-25 21:56:5567// CloudPrintWebDialogDelegate and asks the current active browser to
[email protected]73852b8f2010-05-14 00:38:1268// show an HTML dialog using that class as the delegate. That class
[email protected]89f550b2011-06-08 18:34:0369// hands in the kChromeUICloudPrintResourcesURL as the URL to visit. That is
[email protected]80a8fad2011-01-29 04:02:3870// recognized by the GetWebUIFactoryFunction as a signal to create an
[email protected]02b5ccc2012-04-30 23:58:3171// ExternalWebDialogUI.
[email protected]73852b8f2010-05-14 00:38:1272
[email protected]5835871a2012-04-25 21:56:5573// CloudPrintWebDialogDelegate also temporarily owns a
[email protected]73852b8f2010-05-14 00:38:1274// CloudPrintFlowHandler, a class which is responsible for the actual
[email protected]a984bdf2011-03-15 20:17:1675// interactions with the dialog contents, including handing in the
[email protected]73852b8f2010-05-14 00:38:1276// print data and getting any page setup parameters that the dialog
77// contents provides. As part of bringing up the dialog,
[email protected]02b5ccc2012-04-30 23:58:3178// WebDialogUI::RenderViewCreated is called (an override of
[email protected]c39f9bf2011-02-12 00:43:5579// WebUI::RenderViewCreated). That routine, in turn, calls the
[email protected]36e12172011-02-08 23:46:0280// delegate's GetWebUIMessageHandlers routine, at which point the
[email protected]73852b8f2010-05-14 00:38:1281// ownership of the CloudPrintFlowHandler is handed over. A pointer
82// to the flow handler is kept to facilitate communication back and
83// forth between the two classes.
84
[email protected]c39f9bf2011-02-12 00:43:5585// The WebUI continues dialog bring-up, calling
[email protected]73852b8f2010-05-14 00:38:1286// CloudPrintFlowHandler::RegisterMessages. This is where the
87// additional object model capabilities are registered for the dialog
88// contents to use. It is also at this time that capabilities for the
89// dialog contents are adjusted to allow the dialog contents to close
90// the window. In addition, the pending URL is redirected to the
91// actual cloud print service URL. The flow controller also registers
92// for notification of when the dialog contents finish loading, which
[email protected]a984bdf2011-03-15 20:17:1693// is currently used to send the data to the dialog contents.
[email protected]73852b8f2010-05-14 00:38:1294
[email protected]a984bdf2011-03-15 20:17:1695// In order to send the data to the dialog contents, the flow
[email protected]73852b8f2010-05-14 00:38:1296// handler uses a CloudPrintDataSender. It creates one, letting it
[email protected]a984bdf2011-03-15 20:17:1697// know the name of the temporary file containing the data, and
[email protected]73852b8f2010-05-14 00:38:1298// posts the task of reading the file
99// (CloudPrintDataSender::ReadPrintDataFile) to the file thread. That
100// routine reads in the file, and then hops over to the IO thread to
101// send that data to the dialog contents.
102
103// When the dialog contents are finished (by either being cancelled or
104// hitting the print button), the delegate is notified, and responds
105// that the dialog should be closed, at which point things are torn
106// down and released.
107
[email protected]631bb742011-11-02 11:29:39108using content::BrowserThread;
[email protected]c5eed492012-01-04 17:07:50109using content::NavigationController;
[email protected]10f417c52011-12-28 21:04:23110using content::NavigationEntry;
[email protected]eaabba22012-03-07 15:02:11111using content::RenderViewHost;
[email protected]a81343d232011-12-27 07:39:20112using content::WebContents;
[email protected]26e2632a2011-12-31 04:02:55113using content::WebUIMessageHandler;
[email protected]20c07f8e2012-05-31 08:43:14114using ui::WebDialogDelegate;
[email protected]631bb742011-11-02 11:29:39115
[email protected]681958c2013-02-21 13:48:14116const int kDefaultWidth = 912;
117const int kDefaultHeight = 633;
118
[email protected]73852b8f2010-05-14 00:38:12119namespace internal_cloud_print_helpers {
120
[email protected]73852b8f2010-05-14 00:38:12121// From the JSON parsed value, get the entries for the page setup
122// parameters.
123bool GetPageSetupParameters(const std::string& json,
[email protected]1375e3ab2011-03-24 17:07:22124 PrintMsg_Print_Params& parameters) {
[email protected]cd5785752012-04-11 00:15:41125 scoped_ptr<Value> parsed_value(base::JSONReader::Read(json));
[email protected]73852b8f2010-05-14 00:38:12126 DLOG_IF(ERROR, (!parsed_value.get() ||
127 !parsed_value->IsType(Value::TYPE_DICTIONARY)))
128 << "PageSetup call didn't have expected contents";
129 if (!parsed_value.get() || !parsed_value->IsType(Value::TYPE_DICTIONARY))
130 return false;
131
132 bool result = true;
133 DictionaryValue* params = static_cast<DictionaryValue*>(parsed_value.get());
[email protected]05c7da62011-05-05 17:23:56134 result &= params->GetDouble("dpi", &parameters.dpi);
135 result &= params->GetDouble("min_shrink", &parameters.min_shrink);
136 result &= params->GetDouble("max_shrink", &parameters.max_shrink);
[email protected]a65175d2010-08-17 04:00:57137 result &= params->GetBoolean("selection_only", &parameters.selection_only);
[email protected]73852b8f2010-05-14 00:38:12138 return result;
139}
140
[email protected]536f86a2013-11-23 01:24:56141base::string16 GetSwitchValueString16(const CommandLine& command_line,
142 const char* switchName) {
[email protected]31662202013-03-23 19:10:54143#if defined(OS_WIN)
[email protected]536f86a2013-11-23 01:24:56144 return command_line.GetSwitchValueNative(switchName);
[email protected]e8368e92011-08-20 04:05:56145#elif defined(OS_POSIX)
146 // POSIX Command line string types are different.
147 CommandLine::StringType native_switch_val;
148 native_switch_val = command_line.GetSwitchValueASCII(switchName);
149 // Convert the ASCII string to UTF16 to prepare to pass.
[email protected]536f86a2013-11-23 01:24:56150 return base::ASCIIToUTF16(native_switch_val);
[email protected]e8368e92011-08-20 04:05:56151#endif
152}
153
[email protected]73852b8f2010-05-14 00:38:12154void CloudPrintDataSenderHelper::CallJavascriptFunction(
[email protected]536f86a2013-11-23 01:24:56155 const std::string& function_name, const Value& arg1, const Value& arg2) {
156 web_ui_->CallJavascriptFunction(function_name, arg1, arg2);
[email protected]e8368e92011-08-20 04:05:56157}
158
[email protected]73852b8f2010-05-14 00:38:12159// Clears out the pointer we're using to communicate. Either routine is
160// potentially expensive enough that stopping whatever is in progress
161// is worth it.
162void CloudPrintDataSender::CancelPrintDataFile() {
[email protected]20305ec2011-01-21 04:55:52163 base::AutoLock lock(lock_);
[email protected]73852b8f2010-05-14 00:38:12164 // We don't own helper, it was passed in to us, so no need to
165 // delete, just let it go.
166 helper_ = NULL;
167}
168
[email protected]0085863a2013-12-06 21:19:03169CloudPrintDataSender::CloudPrintDataSender(
170 CloudPrintDataSenderHelper* helper,
171 const base::string16& print_job_title,
172 const base::string16& print_ticket,
173 const std::string& file_type,
174 const base::RefCountedMemory* data)
[email protected]38e08982010-10-22 17:28:43175 : helper_(helper),
[email protected]a984bdf2011-03-15 20:17:16176 print_job_title_(print_job_title),
[email protected]e8368e92011-08-20 04:05:56177 print_ticket_(print_ticket),
[email protected]a9723e12013-03-05 04:02:45178 file_type_(file_type),
179 data_(data) {
[email protected]38e08982010-10-22 17:28:43180}
181
182CloudPrintDataSender::~CloudPrintDataSender() {}
183
[email protected]73852b8f2010-05-14 00:38:12184// We have the data in hand that needs to be pushed into the dialog
185// contents; do so from the IO thread.
186
187// TODO(scottbyer): If the print data ends up being larger than the
188// upload limit (currently 10MB), what we need to do is upload that
189// large data to google docs and set the URL in the printing
190// JavaScript to that location, and make sure it gets deleted when not
191// needed. - 4/1/2010
[email protected]a9723e12013-03-05 04:02:45192void CloudPrintDataSender::SendPrintData() {
[email protected]ba4f1132010-10-09 02:02:35193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]5173de8b2013-06-02 21:16:02194 if (!data_.get() || !data_->size())
[email protected]a9723e12013-03-05 04:02:45195 return;
196
197 std::string base64_data;
198 base::Base64Encode(
199 base::StringPiece(reinterpret_cast<const char*>(data_->front()),
200 data_->size()),
201 &base64_data);
[email protected]a9723e12013-03-05 04:02:45202 std::string header("data:");
203 header.append(file_type_);
204 header.append(";base64,");
205 base64_data.insert(0, header);
206
[email protected]20305ec2011-01-21 04:55:52207 base::AutoLock lock(lock_);
[email protected]a9723e12013-03-05 04:02:45208 if (helper_) {
[email protected]536f86a2013-11-23 01:24:56209 base::StringValue title(print_job_title_);
210 base::StringValue ticket(print_ticket_);
[email protected]e8368e92011-08-20 04:05:56211 // TODO(abodenha): Change Javascript call to pass in print ticket
212 // after server side support is added. Add test for it.
[email protected]73852b8f2010-05-14 00:38:12213
214 // Send the print data to the dialog contents. The JavaScript
215 // function is a preliminary API for prototyping purposes and is
216 // subject to change.
[email protected]536f86a2013-11-23 01:24:56217 helper_->CallJavascriptFunction(
218 "printApp._printDataUrl", base::StringValue(base64_data), title);
[email protected]73852b8f2010-05-14 00:38:12219 }
220}
221
222
[email protected]0085863a2013-12-06 21:19:03223CloudPrintFlowHandler::CloudPrintFlowHandler(
224 const base::RefCountedMemory* data,
225 const base::string16& print_job_title,
226 const base::string16& print_ticket,
227 const std::string& file_type,
228 bool close_after_signin,
229 const base::Closure& callback)
[email protected]c7bf7452011-09-12 21:31:50230 : dialog_delegate_(NULL),
[email protected]a9723e12013-03-05 04:02:45231 data_(data),
[email protected]a984bdf2011-03-15 20:17:16232 print_job_title_(print_job_title),
[email protected]e8368e92011-08-20 04:05:56233 print_ticket_(print_ticket),
[email protected]4cd49022012-01-19 20:37:37234 file_type_(file_type),
235 close_after_signin_(close_after_signin),
236 callback_(callback) {
[email protected]38e08982010-10-22 17:28:43237}
238
239CloudPrintFlowHandler::~CloudPrintFlowHandler() {
240 // This will also cancel any task in flight.
241 CancelAnyRunningTask();
242}
243
244
[email protected]73852b8f2010-05-14 00:38:12245void CloudPrintFlowHandler::SetDialogDelegate(
[email protected]5835871a2012-04-25 21:56:55246 CloudPrintWebDialogDelegate* delegate) {
[email protected]7b748982011-02-14 19:28:23247 // Even if setting a new WebUI, it means any previous task needs
[email protected]a2c92a1c2012-04-03 12:32:14248 // to be canceled, its now invalid.
[email protected]ba4f1132010-10-09 02:02:35249 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]73852b8f2010-05-14 00:38:12250 CancelAnyRunningTask();
251 dialog_delegate_ = delegate;
252}
253
254// Cancels any print data sender we have in flight and removes our
255// reference to it, so when the task that is calling it finishes and
[email protected]a2c92a1c2012-04-03 12:32:14256// removes its reference, it goes away.
[email protected]73852b8f2010-05-14 00:38:12257void CloudPrintFlowHandler::CancelAnyRunningTask() {
[email protected]ba4f1132010-10-09 02:02:35258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]73852b8f2010-05-14 00:38:12259 if (print_data_sender_.get()) {
260 print_data_sender_->CancelPrintDataFile();
261 print_data_sender_ = NULL;
262 }
263}
264
[email protected]73852b8f2010-05-14 00:38:12265void CloudPrintFlowHandler::RegisterMessages() {
[email protected]73852b8f2010-05-14 00:38:12266 // TODO(scottbyer) - This is where we will register messages for the
267 // UI JS to use. Needed: Call to update page setup parameters.
[email protected]46adf7ff2011-12-30 00:53:09268 web_ui()->RegisterMessageCallback("ShowDebugger",
[email protected]ba4fc242011-10-04 18:56:56269 base::Bind(&CloudPrintFlowHandler::HandleShowDebugger,
270 base::Unretained(this)));
[email protected]46adf7ff2011-12-30 00:53:09271 web_ui()->RegisterMessageCallback("SendPrintData",
[email protected]ba4fc242011-10-04 18:56:56272 base::Bind(&CloudPrintFlowHandler::HandleSendPrintData,
273 base::Unretained(this)));
[email protected]46adf7ff2011-12-30 00:53:09274 web_ui()->RegisterMessageCallback("SetPageParameters",
[email protected]ba4fc242011-10-04 18:56:56275 base::Bind(&CloudPrintFlowHandler::HandleSetPageParameters,
276 base::Unretained(this)));
[email protected]73852b8f2010-05-14 00:38:12277
[email protected]0eb25c42011-08-11 14:50:14278 // Register for appropriate notifications, and re-direct the URL
279 // to the real server URL, now that we've gotten an HTML dialog
280 // going.
[email protected]c5eed492012-01-04 17:07:50281 NavigationController* controller =
[email protected]01ec4ec2012-01-18 04:13:47282 &web_ui()->GetWebContents()->GetController();
[email protected]10f417c52011-12-28 21:04:23283 NavigationEntry* pending_entry = controller->GetPendingEntry();
[email protected]0eb25c42011-08-11 14:50:14284 if (pending_entry) {
[email protected]46adf7ff2011-12-30 00:53:09285 Profile* profile = Profile::FromWebUI(web_ui());
[email protected]4cd49022012-01-19 20:37:37286 if (close_after_signin_) {
287 pending_entry->SetURL(
288 CloudPrintURL(profile).GetCloudPrintSigninURL());
289 } else {
290 pending_entry->SetURL(
291 CloudPrintURL(profile).GetCloudPrintServiceDialogURL());
292 }
[email protected]73852b8f2010-05-14 00:38:12293 }
[email protected]0eb25c42011-08-11 14:50:14294 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50295 content::Source<NavigationController>(controller));
[email protected]57c8cf22013-03-02 16:50:00296 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
297 content::Source<NavigationController>(controller));
[email protected]73852b8f2010-05-14 00:38:12298}
299
[email protected]6c2381d2011-10-19 02:52:53300void CloudPrintFlowHandler::Observe(
301 int type,
302 const content::NotificationSource& source,
303 const content::NotificationDetails& details) {
[email protected]57c8cf22013-03-02 16:50:00304 switch (type) {
305 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: {
306 NavigationEntry* entry =
307 web_ui()->GetWebContents()->GetController().GetActiveEntry();
308 if (entry)
309 NavigationToURLDidCloseDialog(entry->GetURL());
310 break;
[email protected]20c52d22011-06-20 22:42:42311 }
[email protected]57c8cf22013-03-02 16:50:00312 case content::NOTIFICATION_LOAD_STOP: {
[email protected]57c8cf22013-03-02 16:50:00313 GURL url = web_ui()->GetWebContents()->GetURL();
[email protected]6e536cdf2013-05-09 04:49:52314 if (IsCloudPrintDialogUrl(url)) {
315 // Take the opportunity to set some (minimal) additional
316 // script permissions required for the web UI.
[email protected]57c8cf22013-03-02 16:50:00317 RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost();
318 if (rvh) {
[email protected]3184f90b2013-05-01 18:17:53319 WebPreferences webkit_prefs = rvh->GetWebkitPreferences();
[email protected]57c8cf22013-03-02 16:50:00320 webkit_prefs.allow_scripts_to_close_windows = true;
321 rvh->UpdateWebkitPreferences(webkit_prefs);
322 } else {
323 NOTREACHED();
324 }
[email protected]6e536cdf2013-05-09 04:49:52325 // Choose one or the other. If you need to debug, bring up the
326 // debugger. You can then use the various chrome.send()
327 // registrations above to kick of the various function calls,
328 // including chrome.send("SendPrintData") in the javaScript
329 // console and watch things happen with:
330 // HandleShowDebugger(NULL);
331 HandleSendPrintData(NULL);
[email protected]57c8cf22013-03-02 16:50:00332 }
[email protected]57c8cf22013-03-02 16:50:00333 break;
334 }
[email protected]73852b8f2010-05-14 00:38:12335 }
336}
337
[email protected]88942a22010-08-19 20:34:43338void CloudPrintFlowHandler::HandleShowDebugger(const ListValue* args) {
[email protected]73852b8f2010-05-14 00:38:12339 ShowDebugger();
340}
341
342void CloudPrintFlowHandler::ShowDebugger() {
[email protected]46adf7ff2011-12-30 00:53:09343 if (web_ui()) {
[email protected]01ec4ec2012-01-18 04:13:47344 RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost();
[email protected]73852b8f2010-05-14 00:38:12345 if (rvh)
[email protected]aebdd072011-07-07 12:36:59346 DevToolsWindow::OpenDevToolsWindow(rvh);
[email protected]73852b8f2010-05-14 00:38:12347 }
348}
349
350scoped_refptr<CloudPrintDataSender>
351CloudPrintFlowHandler::CreateCloudPrintDataSender() {
[email protected]46adf7ff2011-12-30 00:53:09352 DCHECK(web_ui());
353 print_data_helper_.reset(new CloudPrintDataSenderHelper(web_ui()));
[email protected]a9723e12013-03-05 04:02:45354 scoped_refptr<CloudPrintDataSender> sender(
[email protected]5173de8b2013-06-02 21:16:02355 new CloudPrintDataSender(print_data_helper_.get(),
356 print_job_title_,
357 print_ticket_,
358 file_type_,
359 data_.get()));
[email protected]a9723e12013-03-05 04:02:45360 return sender;
[email protected]73852b8f2010-05-14 00:38:12361}
362
[email protected]88942a22010-08-19 20:34:43363void CloudPrintFlowHandler::HandleSendPrintData(const ListValue* args) {
[email protected]ba4f1132010-10-09 02:02:35364 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]73852b8f2010-05-14 00:38:12365 // This will cancel any ReadPrintDataFile() or SendPrintDataFile()
366 // requests in flight (this is anticipation of when setting page
367 // setup parameters becomes asynchronous and may be set while some
368 // data is in flight). Then we can clear out the print data.
369 CancelAnyRunningTask();
[email protected]46adf7ff2011-12-30 00:53:09370 if (web_ui()) {
[email protected]73852b8f2010-05-14 00:38:12371 print_data_sender_ = CreateCloudPrintDataSender();
[email protected]3e2dd4fa2011-11-10 06:06:40372 BrowserThread::PostTask(
[email protected]a9723e12013-03-05 04:02:45373 BrowserThread::IO, FROM_HERE,
374 base::Bind(&CloudPrintDataSender::SendPrintData, print_data_sender_));
[email protected]73852b8f2010-05-14 00:38:12375 }
376}
377
[email protected]88942a22010-08-19 20:34:43378void CloudPrintFlowHandler::HandleSetPageParameters(const ListValue* args) {
[email protected]036056a32011-03-03 21:05:01379 std::string json;
[email protected]e675f7b2011-06-22 17:32:12380 bool ret = args->GetString(0, &json);
381 if (!ret || json.empty()) {
[email protected]036056a32011-03-03 21:05:01382 NOTREACHED() << "Empty json string";
[email protected]73852b8f2010-05-14 00:38:12383 return;
[email protected]036056a32011-03-03 21:05:01384 }
[email protected]73852b8f2010-05-14 00:38:12385
386 // These are backstop default values - 72 dpi to match the screen,
387 // 8.5x11 inch paper with margins subtracted (1/4 inch top, left,
388 // right and 0.56 bottom), and the min page shrink and max page
389 // shrink values appear all over the place with no explanation.
390
391 // TODO(scottbyer): Get a Linux/ChromeOS edge for PrintSettings
392 // working so that we can get the default values from there. Fix up
393 // PrintWebViewHelper to do the same.
394 const int kDPI = 72;
395 const int kWidth = static_cast<int>((8.5-0.25-0.25)*kDPI);
396 const int kHeight = static_cast<int>((11-0.25-0.56)*kDPI);
397 const double kMinPageShrink = 1.25;
398 const double kMaxPageShrink = 2.0;
399
[email protected]1375e3ab2011-03-24 17:07:22400 PrintMsg_Print_Params default_settings;
[email protected]10980442011-12-04 22:33:05401 default_settings.content_size = gfx::Size(kWidth, kHeight);
[email protected]732b8132012-01-10 23:17:32402 default_settings.printable_area = gfx::Rect(0, 0, kWidth, kHeight);
[email protected]73852b8f2010-05-14 00:38:12403 default_settings.dpi = kDPI;
404 default_settings.min_shrink = kMinPageShrink;
405 default_settings.max_shrink = kMaxPageShrink;
406 default_settings.desired_dpi = kDPI;
407 default_settings.document_cookie = 0;
408 default_settings.selection_only = false;
[email protected]718af822011-08-12 22:11:33409 default_settings.preview_request_id = 0;
410 default_settings.is_first_request = true;
[email protected]732b8132012-01-10 23:17:32411 default_settings.print_to_pdf = false;
[email protected]73852b8f2010-05-14 00:38:12412
413 if (!GetPageSetupParameters(json, default_settings)) {
414 NOTREACHED();
415 return;
416 }
417
418 // TODO(scottbyer) - Here is where we would kick the originating
419 // renderer thread with these new parameters in order to get it to
[email protected]a984bdf2011-03-15 20:17:16420 // re-generate the PDF data and hand it back to us. window.print() is
[email protected]73852b8f2010-05-14 00:38:12421 // currently synchronous, so there's a lot of work to do to get to
422 // that point.
423}
424
[email protected]ea161da2010-11-02 21:57:35425void CloudPrintFlowHandler::StoreDialogClientSize() const {
[email protected]01ec4ec2012-01-18 04:13:47426 if (web_ui() && web_ui()->GetWebContents() &&
427 web_ui()->GetWebContents()->GetView()) {
428 gfx::Size size = web_ui()->GetWebContents()->GetView()->GetContainerSize();
[email protected]46adf7ff2011-12-30 00:53:09429 Profile* profile = Profile::FromWebUI(web_ui());
[email protected]0eb25c42011-08-11 14:50:14430 profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogWidth,
431 size.width());
432 profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogHeight,
433 size.height());
[email protected]ea161da2010-11-02 21:57:35434 }
435}
436
[email protected]a911c4e2012-10-12 00:37:57437bool CloudPrintFlowHandler::NavigationToURLDidCloseDialog(const GURL& url) {
438 if (close_after_signin_) {
[email protected]6e536cdf2013-05-09 04:49:52439 if (IsCloudPrintDialogUrl(url)) {
[email protected]a911c4e2012-10-12 00:37:57440 StoreDialogClientSize();
441 web_ui()->GetWebContents()->GetRenderViewHost()->ClosePage();
442 callback_.Run();
443 return true;
444 }
445 }
446 return false;
447}
448
[email protected]6e536cdf2013-05-09 04:49:52449bool CloudPrintFlowHandler::IsCloudPrintDialogUrl(const GURL& url) {
450 GURL cloud_print_url =
451 CloudPrintURL(Profile::FromWebUI(web_ui())).GetCloudPrintServiceURL();
452 return url.host() == cloud_print_url.host() &&
453 StartsWithASCII(url.path(), cloud_print_url.path(), false) &&
454 url.scheme() == cloud_print_url.scheme();
455}
456
[email protected]5835871a2012-04-25 21:56:55457CloudPrintWebDialogDelegate::CloudPrintWebDialogDelegate(
[email protected]b5b79d72012-05-24 19:42:28458 content::BrowserContext* browser_context,
459 gfx::NativeWindow modal_parent,
[email protected]a9723e12013-03-05 04:02:45460 const base::RefCountedMemory* data,
[email protected]9848c7e2010-06-03 16:06:56461 const std::string& json_arguments,
[email protected]0085863a2013-12-06 21:19:03462 const base::string16& print_job_title,
463 const base::string16& print_ticket,
[email protected]a984bdf2011-03-15 20:17:16464 const std::string& file_type,
[email protected]4cd49022012-01-19 20:37:37465 bool close_after_signin,
466 const base::Closure& callback)
[email protected]a9723e12013-03-05 04:02:45467 : flow_handler_(
468 new CloudPrintFlowHandler(data, print_job_title, print_ticket,
469 file_type, close_after_signin, callback)),
[email protected]b5b79d72012-05-24 19:42:28470 modal_parent_(modal_parent),
[email protected]6ddda232011-04-22 15:41:47471 owns_flow_handler_(true),
[email protected]b5b79d72012-05-24 19:42:28472 keep_alive_when_non_modal_(true) {
473 Init(browser_context, json_arguments);
[email protected]73852b8f2010-05-14 00:38:12474}
475
[email protected]05acb55472011-02-03 00:11:07476// For unit testing.
[email protected]5835871a2012-04-25 21:56:55477CloudPrintWebDialogDelegate::CloudPrintWebDialogDelegate(
[email protected]73852b8f2010-05-14 00:38:12478 CloudPrintFlowHandler* flow_handler,
[email protected]a9723e12013-03-05 04:02:45479 const std::string& json_arguments)
480 : flow_handler_(flow_handler),
[email protected]b5b79d72012-05-24 19:42:28481 modal_parent_(NULL),
482 owns_flow_handler_(true),
483 keep_alive_when_non_modal_(false) {
484 Init(NULL, json_arguments);
[email protected]73852b8f2010-05-14 00:38:12485}
486
[email protected]b5b79d72012-05-24 19:42:28487// Returns the persisted width/height for the print dialog.
488void GetDialogWidthAndHeightFromPrefs(content::BrowserContext* browser_context,
489 int* width,
490 int* height) {
[email protected]b5b79d72012-05-24 19:42:28491 if (!browser_context) {
492 *width = kDefaultWidth;
493 *height = kDefaultHeight;
494 return;
495 }
496
[email protected]c753f142013-02-10 13:14:04497 PrefService* prefs = Profile::FromBrowserContext(browser_context)->GetPrefs();
[email protected]c753f142013-02-10 13:14:04498 *width = prefs->GetInteger(prefs::kCloudPrintDialogWidth);
499 *height = prefs->GetInteger(prefs::kCloudPrintDialogHeight);
[email protected]b5b79d72012-05-24 19:42:28500}
501
502void CloudPrintWebDialogDelegate::Init(content::BrowserContext* browser_context,
[email protected]5835871a2012-04-25 21:56:55503 const std::string& json_arguments) {
[email protected]73852b8f2010-05-14 00:38:12504 // This information is needed to show the dialog HTML content.
[email protected]ba4f1132010-10-09 02:02:35505 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b5b79d72012-05-24 19:42:28506
[email protected]89f550b2011-06-08 18:34:03507 params_.url = GURL(chrome::kChromeUICloudPrintResourcesURL);
[email protected]b5b79d72012-05-24 19:42:28508 GetDialogWidthAndHeightFromPrefs(browser_context,
509 &params_.width,
510 &params_.height);
[email protected]73852b8f2010-05-14 00:38:12511 params_.json_input = json_arguments;
512
513 flow_handler_->SetDialogDelegate(this);
[email protected]e39027a2011-01-24 21:41:54514 // If we're not modal we can show the dialog with no browser.
515 // We need this to keep Chrome alive while our dialog is up.
[email protected]b5b79d72012-05-24 19:42:28516 if (!modal_parent_ && keep_alive_when_non_modal_)
[email protected]313fce12013-01-30 17:09:04517 chrome::StartKeepAlive();
[email protected]73852b8f2010-05-14 00:38:12518}
519
[email protected]5835871a2012-04-25 21:56:55520CloudPrintWebDialogDelegate::~CloudPrintWebDialogDelegate() {
[email protected]73852b8f2010-05-14 00:38:12521 // If the flow_handler_ is about to outlive us because we don't own
[email protected]a2c92a1c2012-04-03 12:32:14522 // it anymore, we need to have it remove its reference to us.
[email protected]ba4f1132010-10-09 02:02:35523 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]73852b8f2010-05-14 00:38:12524 flow_handler_->SetDialogDelegate(NULL);
525 if (owns_flow_handler_) {
526 delete flow_handler_;
527 }
528}
529
[email protected]5835871a2012-04-25 21:56:55530ui::ModalType CloudPrintWebDialogDelegate::GetDialogModalType() const {
[email protected]b5b79d72012-05-24 19:42:28531 return modal_parent_ ? ui::MODAL_TYPE_WINDOW : ui::MODAL_TYPE_NONE;
[email protected]73852b8f2010-05-14 00:38:12532}
533
[email protected]6a72a632013-12-12 22:22:00534base::string16 CloudPrintWebDialogDelegate::GetDialogTitle() const {
[email protected]0085863a2013-12-06 21:19:03535 return base::string16();
[email protected]73852b8f2010-05-14 00:38:12536}
537
[email protected]5835871a2012-04-25 21:56:55538GURL CloudPrintWebDialogDelegate::GetDialogContentURL() const {
[email protected]73852b8f2010-05-14 00:38:12539 return params_.url;
540}
541
[email protected]5835871a2012-04-25 21:56:55542void CloudPrintWebDialogDelegate::GetWebUIMessageHandlers(
[email protected]36e12172011-02-08 23:46:02543 std::vector<WebUIMessageHandler*>* handlers) const {
[email protected]73852b8f2010-05-14 00:38:12544 handlers->push_back(flow_handler_);
545 // We don't own flow_handler_ anymore, but it sticks around until at
546 // least right after OnDialogClosed() is called (and this object is
547 // destroyed).
548 owns_flow_handler_ = false;
549}
550
[email protected]5835871a2012-04-25 21:56:55551void CloudPrintWebDialogDelegate::GetDialogSize(gfx::Size* size) const {
[email protected]73852b8f2010-05-14 00:38:12552 size->set_width(params_.width);
553 size->set_height(params_.height);
554}
555
[email protected]5835871a2012-04-25 21:56:55556std::string CloudPrintWebDialogDelegate::GetDialogArgs() const {
[email protected]73852b8f2010-05-14 00:38:12557 return params_.json_input;
558}
559
[email protected]5835871a2012-04-25 21:56:55560void CloudPrintWebDialogDelegate::OnDialogClosed(
[email protected]73852b8f2010-05-14 00:38:12561 const std::string& json_retval) {
[email protected]ea161da2010-11-02 21:57:35562 // Get the final dialog size and store it.
563 flow_handler_->StoreDialogClientSize();
[email protected]6ddda232011-04-22 15:41:47564
[email protected]e39027a2011-01-24 21:41:54565 // If we're modal we can show the dialog with no browser.
566 // End the keep-alive so that Chrome can exit.
[email protected]129bedf2013-11-20 07:34:03567 if (!modal_parent_ && keep_alive_when_non_modal_) {
568 // Post to prevent recursive call tho this function.
569 base::MessageLoop::current()->PostTask(FROM_HERE,
570 base::Bind(&chrome::EndKeepAlive));
571 }
[email protected]73852b8f2010-05-14 00:38:12572 delete this;
573}
574
[email protected]5835871a2012-04-25 21:56:55575void CloudPrintWebDialogDelegate::OnCloseContents(WebContents* source,
576 bool* out_close_dialog) {
[email protected]18137e02010-05-25 21:10:35577 if (out_close_dialog)
578 *out_close_dialog = true;
579}
580
[email protected]5835871a2012-04-25 21:56:55581bool CloudPrintWebDialogDelegate::ShouldShowDialogTitle() const {
[email protected]ea161da2010-11-02 21:57:35582 return false;
583}
584
[email protected]5835871a2012-04-25 21:56:55585bool CloudPrintWebDialogDelegate::HandleContextMenu(
[email protected]35be7ec2012-02-12 20:42:51586 const content::ContextMenuParams& params) {
[email protected]34478212011-04-19 01:35:46587 return true;
588}
589
[email protected]a911c4e2012-10-12 00:37:57590bool CloudPrintWebDialogDelegate::HandleOpenURLFromTab(
591 content::WebContents* source,
592 const content::OpenURLParams& params,
593 content::WebContents** out_new_contents) {
594 return flow_handler_->NavigationToURLDidCloseDialog(params.url);
595}
596
[email protected]6085c70d2011-03-22 22:51:07597// Called from the UI thread, starts up the dialog.
[email protected]b5b79d72012-05-24 19:42:28598void CreateDialogImpl(content::BrowserContext* browser_context,
599 gfx::NativeWindow modal_parent,
[email protected]a9723e12013-03-05 04:02:45600 const base::RefCountedMemory* data,
[email protected]0085863a2013-12-06 21:19:03601 const base::string16& print_job_title,
602 const base::string16& print_ticket,
[email protected]6085c70d2011-03-22 22:51:07603 const std::string& file_type,
[email protected]4cd49022012-01-19 20:37:37604 bool close_after_signin,
605 const base::Closure& callback) {
[email protected]ba4f1132010-10-09 02:02:35606 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]5835871a2012-04-25 21:56:55607 WebDialogDelegate* dialog_delegate =
608 new internal_cloud_print_helpers::CloudPrintWebDialogDelegate(
[email protected]a9723e12013-03-05 04:02:45609 browser_context, modal_parent, data, std::string(), print_job_title,
610 print_ticket, file_type, close_after_signin, callback);
[email protected]88bfd25b2012-06-22 06:28:33611#if defined(OS_WIN)
612 gfx::NativeWindow window =
613#endif
[email protected]87586fb72012-07-02 13:15:02614 chrome::ShowWebDialog(modal_parent,
615 Profile::FromBrowserContext(browser_context),
616 dialog_delegate);
[email protected]88bfd25b2012-06-22 06:28:33617#if defined(OS_WIN)
[email protected]a9723e12013-03-05 04:02:45618 if (window) {
[email protected]88bfd25b2012-06-22 06:28:33619 HWND dialog_handle;
620#if defined(USE_AURA)
[email protected]228f0f02013-11-15 05:58:36621 dialog_handle = window->GetDispatcher()->host()->GetAcceleratedWidget();
[email protected]88bfd25b2012-06-22 06:28:33622#else
623 dialog_handle = window;
624#endif
625 if (::GetForegroundWindow() != dialog_handle) {
626 ui::ForegroundHelper::SetForeground(dialog_handle);
627 }
628 }
629#endif
[email protected]73852b8f2010-05-14 00:38:12630}
[email protected]6085c70d2011-03-22 22:51:07631
[email protected]b5b79d72012-05-24 19:42:28632void CreateDialogSigninImpl(content::BrowserContext* browser_context,
633 gfx::NativeWindow modal_parent,
634 const base::Closure& callback) {
[email protected]0085863a2013-12-06 21:19:03635 CreateDialogImpl(browser_context, modal_parent, NULL, base::string16(),
636 base::string16(), std::string(), true, callback);
[email protected]4cd49022012-01-19 20:37:37637}
638
[email protected]a9723e12013-03-05 04:02:45639void CreateDialogForFileImpl(content::BrowserContext* browser_context,
640 gfx::NativeWindow modal_parent,
641 const base::FilePath& path_to_file,
[email protected]0085863a2013-12-06 21:19:03642 const base::string16& print_job_title,
643 const base::string16& print_ticket,
[email protected]a9723e12013-03-05 04:02:45644 const std::string& file_type,
645 bool delete_on_close) {
646 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
647 scoped_refptr<base::RefCountedMemory> data;
648 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:49649 if (base::GetFileSize(path_to_file, &file_size) && file_size != 0) {
[email protected]a9723e12013-03-05 04:02:45650 std::string file_data;
651 if (file_size < kuint32max) {
652 file_data.reserve(static_cast<unsigned int>(file_size));
653 } else {
654 DLOG(WARNING) << " print data file too large to reserve space";
655 }
[email protected]82f84b92013-08-30 18:23:50656 if (base::ReadFileToString(path_to_file, &file_data)) {
[email protected]a9723e12013-03-05 04:02:45657 data = base::RefCountedString::TakeString(&file_data);
658 }
659 }
660 // Proceed even for empty data to simplify testing.
661 BrowserThread::PostTask(
662 BrowserThread::UI, FROM_HERE,
663 base::Bind(&print_dialog_cloud::CreatePrintDialogForBytes,
664 browser_context, modal_parent, data, print_job_title,
665 print_ticket, file_type));
666 if (delete_on_close)
[email protected]dd3aa792013-07-16 19:10:23667 base::DeleteFile(path_to_file, false);
[email protected]6ddda232011-04-22 15:41:47668}
669
[email protected]6085c70d2011-03-22 22:51:07670} // namespace internal_cloud_print_helpers
671
672namespace print_dialog_cloud {
673
[email protected]37ca3fe02013-07-05 15:32:44674void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
[email protected]681958c2013-02-21 13:48:14675 registry->RegisterIntegerPref(
676 prefs::kCloudPrintDialogWidth,
677 kDefaultWidth,
[email protected]443e9312013-05-06 06:17:34678 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
[email protected]681958c2013-02-21 13:48:14679 registry->RegisterIntegerPref(
680 prefs::kCloudPrintDialogHeight,
681 kDefaultHeight,
[email protected]443e9312013-05-06 06:17:34682 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
[email protected]681958c2013-02-21 13:48:14683}
684
[email protected]6085c70d2011-03-22 22:51:07685// Called on the FILE or UI thread. This is the main entry point into creating
686// the dialog.
687
[email protected]b5b79d72012-05-24 19:42:28688void CreatePrintDialogForFile(content::BrowserContext* browser_context,
689 gfx::NativeWindow modal_parent,
[email protected]650b2d52013-02-10 03:41:45690 const base::FilePath& path_to_file,
[email protected]0085863a2013-12-06 21:19:03691 const base::string16& print_job_title,
692 const base::string16& print_ticket,
[email protected]6085c70d2011-03-22 22:51:07693 const std::string& file_type,
[email protected]d955fc92011-09-19 20:49:03694 bool delete_on_close) {
[email protected]6085c70d2011-03-22 22:51:07695 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE) ||
696 BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]6085c70d2011-03-22 22:51:07697 BrowserThread::PostTask(
[email protected]a9723e12013-03-05 04:02:45698 BrowserThread::FILE, FROM_HERE,
699 base::Bind(&internal_cloud_print_helpers::CreateDialogForFileImpl,
[email protected]b5b79d72012-05-24 19:42:28700 browser_context, modal_parent, path_to_file, print_job_title,
701 print_ticket, file_type, delete_on_close));
[email protected]d955fc92011-09-19 20:49:03702}
703
[email protected]b5b79d72012-05-24 19:42:28704void CreateCloudPrintSigninDialog(content::BrowserContext* browser_context,
705 gfx::NativeWindow modal_parent,
706 const base::Closure& callback) {
[email protected]4cd49022012-01-19 20:37:37707 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
708
709 BrowserThread::PostTask(
710 BrowserThread::UI, FROM_HERE,
711 base::Bind(&internal_cloud_print_helpers::CreateDialogSigninImpl,
[email protected]a9723e12013-03-05 04:02:45712 browser_context, modal_parent, callback));
[email protected]4cd49022012-01-19 20:37:37713}
714
[email protected]b5b79d72012-05-24 19:42:28715void CreatePrintDialogForBytes(content::BrowserContext* browser_context,
716 gfx::NativeWindow modal_parent,
[email protected]a9723e12013-03-05 04:02:45717 const base::RefCountedMemory* data,
[email protected]0085863a2013-12-06 21:19:03718 const base::string16& print_job_title,
719 const base::string16& print_ticket,
[email protected]b5b79d72012-05-24 19:42:28720 const std::string& file_type) {
[email protected]a9723e12013-03-05 04:02:45721 internal_cloud_print_helpers::CreateDialogImpl(browser_context, modal_parent,
722 data, print_job_title,
723 print_ticket, file_type, false,
724 base::Closure());
[email protected]6085c70d2011-03-22 22:51:07725}
726
[email protected]65c9d89a2011-04-13 21:02:39727bool CreatePrintDialogFromCommandLine(const CommandLine& command_line) {
[email protected]87ab41e72012-01-04 18:45:11728 DCHECK(command_line.HasSwitch(switches::kCloudPrintFile));
[email protected]65c9d89a2011-04-13 21:02:39729 if (!command_line.GetSwitchValuePath(switches::kCloudPrintFile).empty()) {
[email protected]650b2d52013-02-10 03:41:45730 base::FilePath cloud_print_file;
[email protected]65c9d89a2011-04-13 21:02:39731 cloud_print_file =
732 command_line.GetSwitchValuePath(switches::kCloudPrintFile);
733 if (!cloud_print_file.empty()) {
[email protected]0085863a2013-12-06 21:19:03734 base::string16 print_job_title;
735 base::string16 print_job_print_ticket;
[email protected]65c9d89a2011-04-13 21:02:39736 if (command_line.HasSwitch(switches::kCloudPrintJobTitle)) {
[email protected]e8368e92011-08-20 04:05:56737 print_job_title =
738 internal_cloud_print_helpers::GetSwitchValueString16(
739 command_line, switches::kCloudPrintJobTitle);
740 }
741 if (command_line.HasSwitch(switches::kCloudPrintPrintTicket)) {
742 print_job_print_ticket =
743 internal_cloud_print_helpers::GetSwitchValueString16(
744 command_line, switches::kCloudPrintPrintTicket);
[email protected]65c9d89a2011-04-13 21:02:39745 }
746 std::string file_type = "application/pdf";
747 if (command_line.HasSwitch(switches::kCloudPrintFileType)) {
748 file_type = command_line.GetSwitchValueASCII(
749 switches::kCloudPrintFileType);
750 }
[email protected]e8368e92011-08-20 04:05:56751
[email protected]d955fc92011-09-19 20:49:03752 bool delete_on_close = CommandLine::ForCurrentProcess()->HasSwitch(
753 switches::kCloudPrintDeleteFile);
754
[email protected]b5b79d72012-05-24 19:42:28755 print_dialog_cloud::CreatePrintDialogForFile(
756 ProfileManager::GetDefaultProfile(),
757 NULL,
758 cloud_print_file,
759 print_job_title,
760 print_job_print_ticket,
761 file_type,
762 delete_on_close);
[email protected]65c9d89a2011-04-13 21:02:39763 return true;
764 }
765 }
766 return false;
767}
768
[email protected]228f0f02013-11-15 05:58:36769} // namespace print_dialog_cloud