blob: 98b18c058ff61221d20d4de673a61f8d01e0cccd [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"
6#include "chrome/browser/printing/print_dialog_cloud_internal.h"
7
[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]4b8852a2011-06-10 17:24:4614#include "base/utf_string_conversions.h"
[email protected]73852b8f2010-05-14 00:38:1215#include "base/values.h"
[email protected]0744cc72011-08-12 12:05:4516#include "chrome/browser/browser_process_impl.h"
[email protected]dce502762011-07-20 08:53:4917#include "chrome/browser/debugger/devtools_window.h"
[email protected]ea161da2010-11-02 21:57:3518#include "chrome/browser/prefs/pref_service.h"
[email protected]2283eead2010-09-29 23:17:3019#include "chrome/browser/printing/cloud_print/cloud_print_url.h"
[email protected]8ecad5e2010-12-02 21:18:3320#include "chrome/browser/profiles/profile.h"
[email protected]e39027a2011-01-24 21:41:5421#include "chrome/browser/profiles/profile_manager.h"
[email protected]eb2d7902011-02-02 18:19:5622#include "chrome/browser/ui/browser_dialogs.h"
[email protected]6768ac02011-04-06 17:41:0423#include "chrome/browser/ui/browser_list.h"
[email protected]22c830d2011-11-29 20:00:3724#include "chrome/browser/ui/dialog_style.h"
[email protected]0ec4898e2011-12-30 21:09:2425#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
[email protected]65c9d89a2011-04-13 21:02:3926#include "chrome/common/chrome_switches.h"
[email protected]ea161da2010-11-02 21:57:3527#include "chrome/common/pref_names.h"
[email protected]1375e3ab2011-03-24 17:07:2228#include "chrome/common/print_messages.h"
[email protected]73852b8f2010-05-14 00:38:1229#include "chrome/common/url_constants.h"
[email protected]5f945a0e2011-03-01 17:47:5330#include "content/browser/renderer_host/render_view_host.h"
[email protected]5f945a0e2011-03-01 17:47:5331#include "content/browser/tab_contents/tab_contents_view.h"
[email protected]c38831a12011-10-28 12:44:4932#include "content/public/browser/browser_thread.h"
[email protected]cdcb1dee2012-01-04 00:46:2033#include "content/public/browser/navigation_controller.h"
[email protected]022af742011-12-28 18:37:2534#include "content/public/browser/navigation_entry.h"
[email protected]6c2381d2011-10-19 02:52:5335#include "content/public/browser/notification_registrar.h"
36#include "content/public/browser/notification_source.h"
[email protected]0d6e9bd2011-10-18 04:29:1637#include "content/public/browser/notification_types.h"
[email protected]0ec4898e2011-12-30 21:09:2438#include "content/public/browser/web_contents.h"
[email protected]01ec4ec2012-01-18 04:13:4739#include "content/public/browser/web_ui.h"
[email protected]c051a1b2011-01-21 23:30:1740#include "ui/base/l10n/l10n_util.h"
[email protected]939856a2010-08-24 20:29:0241#include "webkit/glue/webpreferences.h"
[email protected]73852b8f2010-05-14 00:38:1242
43#include "grit/generated_resources.h"
44
[email protected]b73889c2012-01-11 20:13:2345#if defined(USE_AURA)
46#include "chrome/browser/ui/browser_window.h"
47#include "chrome/browser/ui/views/html_dialog_view.h"
48#include "ui/views/widget/widget.h"
49#endif
50
[email protected]73852b8f2010-05-14 00:38:1251// This module implements the UI support in Chrome for cloud printing.
52// This means hosting a dialog containing HTML/JavaScript and using
53// the published cloud print user interface integration APIs to get
54// page setup settings from the dialog contents and provide the
[email protected]a984bdf2011-03-15 20:17:1655// generated print data to the dialog contents for uploading to the
[email protected]73852b8f2010-05-14 00:38:1256// cloud print service.
57
58// Currently, the flow between these classes is as follows:
59
[email protected]a984bdf2011-03-15 20:17:1660// PrintDialogCloud::CreatePrintDialogForFile is called from
[email protected]73852b8f2010-05-14 00:38:1261// resource_message_filter_gtk.cc once the renderer has informed the
[email protected]a984bdf2011-03-15 20:17:1662// renderer host that print data generation into the renderer host provided
[email protected]032682b2011-01-12 22:05:0263// temp file has been completed. That call is on the FILE thread.
[email protected]73852b8f2010-05-14 00:38:1264// That, in turn, hops over to the UI thread to create an instance of
65// PrintDialogCloud.
66
67// The constructor for PrintDialogCloud creates a
68// CloudPrintHtmlDialogDelegate and asks the current active browser to
69// show an HTML dialog using that class as the delegate. That class
[email protected]89f550b2011-06-08 18:34:0370// hands in the kChromeUICloudPrintResourcesURL as the URL to visit. That is
[email protected]80a8fad2011-01-29 04:02:3871// recognized by the GetWebUIFactoryFunction as a signal to create an
[email protected]73852b8f2010-05-14 00:38:1272// ExternalHtmlDialogUI.
73
74// CloudPrintHtmlDialogDelegate also temporarily owns a
75// CloudPrintFlowHandler, a class which is responsible for the actual
[email protected]a984bdf2011-03-15 20:17:1676// interactions with the dialog contents, including handing in the
[email protected]73852b8f2010-05-14 00:38:1277// print data and getting any page setup parameters that the dialog
78// contents provides. As part of bringing up the dialog,
79// HtmlDialogUI::RenderViewCreated is called (an override of
[email protected]c39f9bf2011-02-12 00:43:5580// WebUI::RenderViewCreated). That routine, in turn, calls the
[email protected]36e12172011-02-08 23:46:0281// delegate's GetWebUIMessageHandlers routine, at which point the
[email protected]73852b8f2010-05-14 00:38:1282// ownership of the CloudPrintFlowHandler is handed over. A pointer
83// to the flow handler is kept to facilitate communication back and
84// forth between the two classes.
85
[email protected]c39f9bf2011-02-12 00:43:5586// The WebUI continues dialog bring-up, calling
[email protected]73852b8f2010-05-14 00:38:1287// CloudPrintFlowHandler::RegisterMessages. This is where the
88// additional object model capabilities are registered for the dialog
89// contents to use. It is also at this time that capabilities for the
90// dialog contents are adjusted to allow the dialog contents to close
91// the window. In addition, the pending URL is redirected to the
92// actual cloud print service URL. The flow controller also registers
93// for notification of when the dialog contents finish loading, which
[email protected]a984bdf2011-03-15 20:17:1694// is currently used to send the data to the dialog contents.
[email protected]73852b8f2010-05-14 00:38:1295
[email protected]a984bdf2011-03-15 20:17:1696// In order to send the data to the dialog contents, the flow
[email protected]73852b8f2010-05-14 00:38:1297// handler uses a CloudPrintDataSender. It creates one, letting it
[email protected]a984bdf2011-03-15 20:17:1698// know the name of the temporary file containing the data, and
[email protected]73852b8f2010-05-14 00:38:1299// posts the task of reading the file
100// (CloudPrintDataSender::ReadPrintDataFile) to the file thread. That
101// routine reads in the file, and then hops over to the IO thread to
102// send that data to the dialog contents.
103
104// When the dialog contents are finished (by either being cancelled or
105// hitting the print button), the delegate is notified, and responds
106// that the dialog should be closed, at which point things are torn
107// down and released.
108
109// TODO(scottbyer):
110// http://code.google.com/p/chromium/issues/detail?id=44093 The
[email protected]a984bdf2011-03-15 20:17:16111// high-level flow (where the data is generated before even
[email protected]73852b8f2010-05-14 00:38:12112// bringing up the dialog) isn't what we want.
113
[email protected]631bb742011-11-02 11:29:39114using content::BrowserThread;
[email protected]c5eed492012-01-04 17:07:50115using content::NavigationController;
[email protected]10f417c52011-12-28 21:04:23116using content::NavigationEntry;
[email protected]a81343d232011-12-27 07:39:20117using content::WebContents;
[email protected]26e2632a2011-12-31 04:02:55118using content::WebUIMessageHandler;
[email protected]631bb742011-11-02 11:29:39119
[email protected]73852b8f2010-05-14 00:38:12120namespace internal_cloud_print_helpers {
121
[email protected]73852b8f2010-05-14 00:38:12122// From the JSON parsed value, get the entries for the page setup
123// parameters.
124bool GetPageSetupParameters(const std::string& json,
[email protected]1375e3ab2011-03-24 17:07:22125 PrintMsg_Print_Params& parameters) {
[email protected]73852b8f2010-05-14 00:38:12126 scoped_ptr<Value> parsed_value(base::JSONReader::Read(json, false));
127 DLOG_IF(ERROR, (!parsed_value.get() ||
128 !parsed_value->IsType(Value::TYPE_DICTIONARY)))
129 << "PageSetup call didn't have expected contents";
130 if (!parsed_value.get() || !parsed_value->IsType(Value::TYPE_DICTIONARY))
131 return false;
132
133 bool result = true;
134 DictionaryValue* params = static_cast<DictionaryValue*>(parsed_value.get());
[email protected]05c7da62011-05-05 17:23:56135 result &= params->GetDouble("dpi", &parameters.dpi);
136 result &= params->GetDouble("min_shrink", &parameters.min_shrink);
137 result &= params->GetDouble("max_shrink", &parameters.max_shrink);
[email protected]a65175d2010-08-17 04:00:57138 result &= params->GetBoolean("selection_only", &parameters.selection_only);
[email protected]73852b8f2010-05-14 00:38:12139 return result;
140}
141
[email protected]e8368e92011-08-20 04:05:56142string16 GetSwitchValueString16(const CommandLine& command_line,
143 const char* switchName) {
144#ifdef OS_WIN
145 CommandLine::StringType native_switch_val;
146 native_switch_val = command_line.GetSwitchValueNative(switchName);
147 return string16(native_switch_val);
148#elif defined(OS_POSIX)
149 // POSIX Command line string types are different.
150 CommandLine::StringType native_switch_val;
151 native_switch_val = command_line.GetSwitchValueASCII(switchName);
152 // Convert the ASCII string to UTF16 to prepare to pass.
153 return string16(ASCIIToUTF16(native_switch_val));
154#endif
155}
156
[email protected]73852b8f2010-05-14 00:38:12157void CloudPrintDataSenderHelper::CallJavascriptFunction(
158 const std::wstring& function_name) {
[email protected]adcf8492011-03-09 22:41:39159 web_ui_->CallJavascriptFunction(WideToASCII(function_name));
[email protected]73852b8f2010-05-14 00:38:12160}
161
162void CloudPrintDataSenderHelper::CallJavascriptFunction(
163 const std::wstring& function_name, const Value& arg) {
[email protected]adcf8492011-03-09 22:41:39164 web_ui_->CallJavascriptFunction(WideToASCII(function_name), arg);
[email protected]73852b8f2010-05-14 00:38:12165}
166
167void CloudPrintDataSenderHelper::CallJavascriptFunction(
168 const std::wstring& function_name, const Value& arg1, const Value& arg2) {
[email protected]adcf8492011-03-09 22:41:39169 web_ui_->CallJavascriptFunction(WideToASCII(function_name), arg1, arg2);
[email protected]73852b8f2010-05-14 00:38:12170}
171
[email protected]e8368e92011-08-20 04:05:56172void CloudPrintDataSenderHelper::CallJavascriptFunction(
173 const std::wstring& function_name,
174 const Value& arg1,
175 const Value& arg2,
176 const Value& arg3) {
[email protected]46adf7ff2011-12-30 00:53:09177 web_ui_->CallJavascriptFunction(
178 WideToASCII(function_name), arg1, arg2, arg3);
[email protected]e8368e92011-08-20 04:05:56179}
180
[email protected]73852b8f2010-05-14 00:38:12181// Clears out the pointer we're using to communicate. Either routine is
182// potentially expensive enough that stopping whatever is in progress
183// is worth it.
184void CloudPrintDataSender::CancelPrintDataFile() {
[email protected]20305ec2011-01-21 04:55:52185 base::AutoLock lock(lock_);
[email protected]73852b8f2010-05-14 00:38:12186 // We don't own helper, it was passed in to us, so no need to
187 // delete, just let it go.
188 helper_ = NULL;
189}
190
[email protected]38e08982010-10-22 17:28:43191CloudPrintDataSender::CloudPrintDataSender(CloudPrintDataSenderHelper* helper,
[email protected]a984bdf2011-03-15 20:17:16192 const string16& print_job_title,
[email protected]e8368e92011-08-20 04:05:56193 const string16& print_ticket,
[email protected]a984bdf2011-03-15 20:17:16194 const std::string& file_type)
[email protected]38e08982010-10-22 17:28:43195 : helper_(helper),
[email protected]a984bdf2011-03-15 20:17:16196 print_job_title_(print_job_title),
[email protected]e8368e92011-08-20 04:05:56197 print_ticket_(print_ticket),
[email protected]a984bdf2011-03-15 20:17:16198 file_type_(file_type) {
[email protected]38e08982010-10-22 17:28:43199}
200
201CloudPrintDataSender::~CloudPrintDataSender() {}
202
[email protected]a984bdf2011-03-15 20:17:16203// Grab the raw file contents and massage them into shape for
[email protected]73852b8f2010-05-14 00:38:12204// sending to the dialog contents (and up to the cloud print server)
205// by encoding it and prefixing it with the appropriate mime type.
206// Once that is done, kick off the next part of the task on the IO
207// thread.
[email protected]a984bdf2011-03-15 20:17:16208void CloudPrintDataSender::ReadPrintDataFile(const FilePath& path_to_file) {
[email protected]ba4f1132010-10-09 02:02:35209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]73852b8f2010-05-14 00:38:12210 int64 file_size = 0;
[email protected]a984bdf2011-03-15 20:17:16211 if (file_util::GetFileSize(path_to_file, &file_size) && file_size != 0) {
[email protected]73852b8f2010-05-14 00:38:12212 std::string file_data;
213 if (file_size < kuint32max) {
214 file_data.reserve(static_cast<unsigned int>(file_size));
215 } else {
216 DLOG(WARNING) << " print data file too large to reserve space";
217 }
[email protected]a984bdf2011-03-15 20:17:16218 if (helper_ && file_util::ReadFileToString(path_to_file, &file_data)) {
[email protected]73852b8f2010-05-14 00:38:12219 std::string base64_data;
220 base::Base64Encode(file_data, &base64_data);
[email protected]a984bdf2011-03-15 20:17:16221 std::string header("data:");
222 header.append(file_type_);
223 header.append(";base64,");
[email protected]73852b8f2010-05-14 00:38:12224 base64_data.insert(0, header);
225 scoped_ptr<StringValue> new_data(new StringValue(base64_data));
226 print_data_.swap(new_data);
[email protected]3e2dd4fa2011-11-10 06:06:40227 BrowserThread::PostTask(
228 BrowserThread::IO, FROM_HERE,
229 base::Bind(&CloudPrintDataSender::SendPrintDataFile, this));
[email protected]73852b8f2010-05-14 00:38:12230 }
231 }
232}
233
234// We have the data in hand that needs to be pushed into the dialog
235// contents; do so from the IO thread.
236
237// TODO(scottbyer): If the print data ends up being larger than the
238// upload limit (currently 10MB), what we need to do is upload that
239// large data to google docs and set the URL in the printing
240// JavaScript to that location, and make sure it gets deleted when not
241// needed. - 4/1/2010
242void CloudPrintDataSender::SendPrintDataFile() {
[email protected]ba4f1132010-10-09 02:02:35243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]20305ec2011-01-21 04:55:52244 base::AutoLock lock(lock_);
[email protected]73852b8f2010-05-14 00:38:12245 if (helper_ && print_data_.get()) {
[email protected]9848c7e2010-06-03 16:06:56246 StringValue title(print_job_title_);
[email protected]e8368e92011-08-20 04:05:56247 StringValue ticket(print_ticket_);
248 // TODO(abodenha): Change Javascript call to pass in print ticket
249 // after server side support is added. Add test for it.
[email protected]73852b8f2010-05-14 00:38:12250
251 // Send the print data to the dialog contents. The JavaScript
252 // function is a preliminary API for prototyping purposes and is
253 // subject to change.
254 const_cast<CloudPrintDataSenderHelper*>(helper_)->CallJavascriptFunction(
255 L"printApp._printDataUrl", *print_data_, title);
256 }
257}
258
259
[email protected]a984bdf2011-03-15 20:17:16260CloudPrintFlowHandler::CloudPrintFlowHandler(const FilePath& path_to_file,
261 const string16& print_job_title,
[email protected]e8368e92011-08-20 04:05:56262 const string16& print_ticket,
[email protected]a984bdf2011-03-15 20:17:16263 const std::string& file_type)
[email protected]c7bf7452011-09-12 21:31:50264 : dialog_delegate_(NULL),
265 path_to_file_(path_to_file),
[email protected]a984bdf2011-03-15 20:17:16266 print_job_title_(print_job_title),
[email protected]e8368e92011-08-20 04:05:56267 print_ticket_(print_ticket),
[email protected]a984bdf2011-03-15 20:17:16268 file_type_(file_type) {
[email protected]38e08982010-10-22 17:28:43269}
270
271CloudPrintFlowHandler::~CloudPrintFlowHandler() {
272 // This will also cancel any task in flight.
273 CancelAnyRunningTask();
274}
275
276
[email protected]73852b8f2010-05-14 00:38:12277void CloudPrintFlowHandler::SetDialogDelegate(
278 CloudPrintHtmlDialogDelegate* delegate) {
[email protected]7b748982011-02-14 19:28:23279 // Even if setting a new WebUI, it means any previous task needs
[email protected]73852b8f2010-05-14 00:38:12280 // to be cancelled, it's now invalid.
[email protected]ba4f1132010-10-09 02:02:35281 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]73852b8f2010-05-14 00:38:12282 CancelAnyRunningTask();
283 dialog_delegate_ = delegate;
284}
285
286// Cancels any print data sender we have in flight and removes our
287// reference to it, so when the task that is calling it finishes and
288// removes it's reference, it goes away.
289void CloudPrintFlowHandler::CancelAnyRunningTask() {
[email protected]ba4f1132010-10-09 02:02:35290 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]73852b8f2010-05-14 00:38:12291 if (print_data_sender_.get()) {
292 print_data_sender_->CancelPrintDataFile();
293 print_data_sender_ = NULL;
294 }
295}
296
[email protected]73852b8f2010-05-14 00:38:12297void CloudPrintFlowHandler::RegisterMessages() {
[email protected]73852b8f2010-05-14 00:38:12298 // TODO(scottbyer) - This is where we will register messages for the
299 // UI JS to use. Needed: Call to update page setup parameters.
[email protected]46adf7ff2011-12-30 00:53:09300 web_ui()->RegisterMessageCallback("ShowDebugger",
[email protected]ba4fc242011-10-04 18:56:56301 base::Bind(&CloudPrintFlowHandler::HandleShowDebugger,
302 base::Unretained(this)));
[email protected]46adf7ff2011-12-30 00:53:09303 web_ui()->RegisterMessageCallback("SendPrintData",
[email protected]ba4fc242011-10-04 18:56:56304 base::Bind(&CloudPrintFlowHandler::HandleSendPrintData,
305 base::Unretained(this)));
[email protected]46adf7ff2011-12-30 00:53:09306 web_ui()->RegisterMessageCallback("SetPageParameters",
[email protected]ba4fc242011-10-04 18:56:56307 base::Bind(&CloudPrintFlowHandler::HandleSetPageParameters,
308 base::Unretained(this)));
[email protected]73852b8f2010-05-14 00:38:12309
[email protected]0eb25c42011-08-11 14:50:14310 // Register for appropriate notifications, and re-direct the URL
311 // to the real server URL, now that we've gotten an HTML dialog
312 // going.
[email protected]c5eed492012-01-04 17:07:50313 NavigationController* controller =
[email protected]01ec4ec2012-01-18 04:13:47314 &web_ui()->GetWebContents()->GetController();
[email protected]10f417c52011-12-28 21:04:23315 NavigationEntry* pending_entry = controller->GetPendingEntry();
[email protected]0eb25c42011-08-11 14:50:14316 if (pending_entry) {
[email protected]46adf7ff2011-12-30 00:53:09317 Profile* profile = Profile::FromWebUI(web_ui());
[email protected]ad23a092011-12-28 07:02:04318 pending_entry->SetURL(
[email protected]0eb25c42011-08-11 14:50:14319 CloudPrintURL(profile).GetCloudPrintServiceDialogURL());
[email protected]73852b8f2010-05-14 00:38:12320 }
[email protected]0eb25c42011-08-11 14:50:14321 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP,
[email protected]c5eed492012-01-04 17:07:50322 content::Source<NavigationController>(controller));
[email protected]73852b8f2010-05-14 00:38:12323}
324
[email protected]6c2381d2011-10-19 02:52:53325void CloudPrintFlowHandler::Observe(
326 int type,
327 const content::NotificationSource& source,
328 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:27329 if (type == content::NOTIFICATION_LOAD_STOP) {
[email protected]20c52d22011-06-20 22:42:42330 // Take the opportunity to set some (minimal) additional
331 // script permissions required for the web UI.
[email protected]01ec4ec2012-01-18 04:13:47332 GURL url = web_ui()->GetWebContents()->GetURL();
[email protected]20c52d22011-06-20 22:42:42333 GURL dialog_url = CloudPrintURL(
[email protected]46adf7ff2011-12-30 00:53:09334 Profile::FromWebUI(web_ui())).GetCloudPrintServiceDialogURL();
[email protected]20c52d22011-06-20 22:42:42335 if (url.host() == dialog_url.host() &&
336 url.path() == dialog_url.path() &&
337 url.scheme() == dialog_url.scheme()) {
[email protected]01ec4ec2012-01-18 04:13:47338 RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost();
[email protected]20c52d22011-06-20 22:42:42339 if (rvh && rvh->delegate()) {
340 WebPreferences webkit_prefs = rvh->delegate()->GetWebkitPrefs();
341 webkit_prefs.allow_scripts_to_close_windows = true;
[email protected]9abd51f2011-09-21 19:11:35342 rvh->UpdateWebkitPreferences(webkit_prefs);
[email protected]20c52d22011-06-20 22:42:42343 } else {
344 DCHECK(false);
345 }
346 }
347
[email protected]73852b8f2010-05-14 00:38:12348 // Choose one or the other. If you need to debug, bring up the
349 // debugger. You can then use the various chrome.send()
350 // registrations above to kick of the various function calls,
351 // including chrome.send("SendPrintData") in the javaScript
352 // console and watch things happen with:
353 // HandleShowDebugger(NULL);
354 HandleSendPrintData(NULL);
355 }
356}
357
[email protected]88942a22010-08-19 20:34:43358void CloudPrintFlowHandler::HandleShowDebugger(const ListValue* args) {
[email protected]73852b8f2010-05-14 00:38:12359 ShowDebugger();
360}
361
362void CloudPrintFlowHandler::ShowDebugger() {
[email protected]46adf7ff2011-12-30 00:53:09363 if (web_ui()) {
[email protected]01ec4ec2012-01-18 04:13:47364 RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost();
[email protected]73852b8f2010-05-14 00:38:12365 if (rvh)
[email protected]aebdd072011-07-07 12:36:59366 DevToolsWindow::OpenDevToolsWindow(rvh);
[email protected]73852b8f2010-05-14 00:38:12367 }
368}
369
370scoped_refptr<CloudPrintDataSender>
371CloudPrintFlowHandler::CreateCloudPrintDataSender() {
[email protected]46adf7ff2011-12-30 00:53:09372 DCHECK(web_ui());
373 print_data_helper_.reset(new CloudPrintDataSenderHelper(web_ui()));
[email protected]a984bdf2011-03-15 20:17:16374 return new CloudPrintDataSender(print_data_helper_.get(),
375 print_job_title_,
[email protected]e8368e92011-08-20 04:05:56376 print_ticket_,
[email protected]a984bdf2011-03-15 20:17:16377 file_type_);
[email protected]73852b8f2010-05-14 00:38:12378}
379
[email protected]88942a22010-08-19 20:34:43380void CloudPrintFlowHandler::HandleSendPrintData(const ListValue* args) {
[email protected]ba4f1132010-10-09 02:02:35381 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]73852b8f2010-05-14 00:38:12382 // This will cancel any ReadPrintDataFile() or SendPrintDataFile()
383 // requests in flight (this is anticipation of when setting page
384 // setup parameters becomes asynchronous and may be set while some
385 // data is in flight). Then we can clear out the print data.
386 CancelAnyRunningTask();
[email protected]46adf7ff2011-12-30 00:53:09387 if (web_ui()) {
[email protected]73852b8f2010-05-14 00:38:12388 print_data_sender_ = CreateCloudPrintDataSender();
[email protected]3e2dd4fa2011-11-10 06:06:40389 BrowserThread::PostTask(
390 BrowserThread::FILE, FROM_HERE,
391 base::Bind(&CloudPrintDataSender::ReadPrintDataFile,
392 print_data_sender_.get(), path_to_file_));
[email protected]73852b8f2010-05-14 00:38:12393 }
394}
395
[email protected]88942a22010-08-19 20:34:43396void CloudPrintFlowHandler::HandleSetPageParameters(const ListValue* args) {
[email protected]036056a32011-03-03 21:05:01397 std::string json;
[email protected]e675f7b2011-06-22 17:32:12398 bool ret = args->GetString(0, &json);
399 if (!ret || json.empty()) {
[email protected]036056a32011-03-03 21:05:01400 NOTREACHED() << "Empty json string";
[email protected]73852b8f2010-05-14 00:38:12401 return;
[email protected]036056a32011-03-03 21:05:01402 }
[email protected]73852b8f2010-05-14 00:38:12403
404 // These are backstop default values - 72 dpi to match the screen,
405 // 8.5x11 inch paper with margins subtracted (1/4 inch top, left,
406 // right and 0.56 bottom), and the min page shrink and max page
407 // shrink values appear all over the place with no explanation.
408
409 // TODO(scottbyer): Get a Linux/ChromeOS edge for PrintSettings
410 // working so that we can get the default values from there. Fix up
411 // PrintWebViewHelper to do the same.
412 const int kDPI = 72;
413 const int kWidth = static_cast<int>((8.5-0.25-0.25)*kDPI);
414 const int kHeight = static_cast<int>((11-0.25-0.56)*kDPI);
415 const double kMinPageShrink = 1.25;
416 const double kMaxPageShrink = 2.0;
417
[email protected]1375e3ab2011-03-24 17:07:22418 PrintMsg_Print_Params default_settings;
[email protected]10980442011-12-04 22:33:05419 default_settings.content_size = gfx::Size(kWidth, kHeight);
[email protected]732b8132012-01-10 23:17:32420 default_settings.printable_area = gfx::Rect(0, 0, kWidth, kHeight);
[email protected]73852b8f2010-05-14 00:38:12421 default_settings.dpi = kDPI;
422 default_settings.min_shrink = kMinPageShrink;
423 default_settings.max_shrink = kMaxPageShrink;
424 default_settings.desired_dpi = kDPI;
425 default_settings.document_cookie = 0;
426 default_settings.selection_only = false;
[email protected]718af822011-08-12 22:11:33427 default_settings.preview_request_id = 0;
428 default_settings.is_first_request = true;
[email protected]732b8132012-01-10 23:17:32429 default_settings.print_to_pdf = false;
[email protected]73852b8f2010-05-14 00:38:12430
431 if (!GetPageSetupParameters(json, default_settings)) {
432 NOTREACHED();
433 return;
434 }
435
436 // TODO(scottbyer) - Here is where we would kick the originating
437 // renderer thread with these new parameters in order to get it to
[email protected]a984bdf2011-03-15 20:17:16438 // re-generate the PDF data and hand it back to us. window.print() is
[email protected]73852b8f2010-05-14 00:38:12439 // currently synchronous, so there's a lot of work to do to get to
440 // that point.
441}
442
[email protected]ea161da2010-11-02 21:57:35443void CloudPrintFlowHandler::StoreDialogClientSize() const {
[email protected]01ec4ec2012-01-18 04:13:47444 if (web_ui() && web_ui()->GetWebContents() &&
445 web_ui()->GetWebContents()->GetView()) {
446 gfx::Size size = web_ui()->GetWebContents()->GetView()->GetContainerSize();
[email protected]46adf7ff2011-12-30 00:53:09447 Profile* profile = Profile::FromWebUI(web_ui());
[email protected]0eb25c42011-08-11 14:50:14448 profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogWidth,
449 size.width());
450 profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogHeight,
451 size.height());
[email protected]ea161da2010-11-02 21:57:35452 }
453}
454
[email protected]73852b8f2010-05-14 00:38:12455CloudPrintHtmlDialogDelegate::CloudPrintHtmlDialogDelegate(
[email protected]a984bdf2011-03-15 20:17:16456 const FilePath& path_to_file,
[email protected]73852b8f2010-05-14 00:38:12457 int width, int height,
[email protected]9848c7e2010-06-03 16:06:56458 const std::string& json_arguments,
[email protected]e39027a2011-01-24 21:41:54459 const string16& print_job_title,
[email protected]e8368e92011-08-20 04:05:56460 const string16& print_ticket,
[email protected]a984bdf2011-03-15 20:17:16461 const std::string& file_type,
[email protected]d955fc92011-09-19 20:49:03462 bool modal,
463 bool delete_on_close)
464 : delete_on_close_(delete_on_close),
465 flow_handler_(new CloudPrintFlowHandler(path_to_file,
[email protected]a984bdf2011-03-15 20:17:16466 print_job_title,
[email protected]e8368e92011-08-20 04:05:56467 print_ticket,
[email protected]a984bdf2011-03-15 20:17:16468 file_type)),
[email protected]e39027a2011-01-24 21:41:54469 modal_(modal),
[email protected]6ddda232011-04-22 15:41:47470 owns_flow_handler_(true),
471 path_to_file_(path_to_file) {
[email protected]73852b8f2010-05-14 00:38:12472 Init(width, height, json_arguments);
473}
474
[email protected]05acb55472011-02-03 00:11:07475// For unit testing.
[email protected]73852b8f2010-05-14 00:38:12476CloudPrintHtmlDialogDelegate::CloudPrintHtmlDialogDelegate(
477 CloudPrintFlowHandler* flow_handler,
478 int width, int height,
[email protected]e39027a2011-01-24 21:41:54479 const std::string& json_arguments,
[email protected]d955fc92011-09-19 20:49:03480 bool modal,
481 bool delete_on_close)
482 : delete_on_close_(delete_on_close),
483 flow_handler_(flow_handler),
[email protected]e39027a2011-01-24 21:41:54484 modal_(modal),
[email protected]18137e02010-05-25 21:10:35485 owns_flow_handler_(true) {
[email protected]73852b8f2010-05-14 00:38:12486 Init(width, height, json_arguments);
487}
488
[email protected]05acb55472011-02-03 00:11:07489void CloudPrintHtmlDialogDelegate::Init(int width, int height,
490 const std::string& json_arguments) {
[email protected]73852b8f2010-05-14 00:38:12491 // This information is needed to show the dialog HTML content.
[email protected]ba4f1132010-10-09 02:02:35492 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]89f550b2011-06-08 18:34:03493 params_.url = GURL(chrome::kChromeUICloudPrintResourcesURL);
[email protected]73852b8f2010-05-14 00:38:12494 params_.height = height;
495 params_.width = width;
496 params_.json_input = json_arguments;
497
498 flow_handler_->SetDialogDelegate(this);
[email protected]e39027a2011-01-24 21:41:54499 // If we're not modal we can show the dialog with no browser.
500 // We need this to keep Chrome alive while our dialog is up.
501 if (!modal_)
502 BrowserList::StartKeepAlive();
[email protected]73852b8f2010-05-14 00:38:12503}
504
505CloudPrintHtmlDialogDelegate::~CloudPrintHtmlDialogDelegate() {
506 // If the flow_handler_ is about to outlive us because we don't own
507 // it anymore, we need to have it remove it's reference to us.
[email protected]ba4f1132010-10-09 02:02:35508 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]73852b8f2010-05-14 00:38:12509 flow_handler_->SetDialogDelegate(NULL);
510 if (owns_flow_handler_) {
511 delete flow_handler_;
512 }
513}
514
[email protected]707d6be62012-01-12 03:56:15515ui::ModalType CloudPrintHtmlDialogDelegate::GetDialogModalType() const {
516 return modal_ ? ui::MODAL_TYPE_WINDOW : ui::MODAL_TYPE_NONE;
[email protected]73852b8f2010-05-14 00:38:12517}
518
[email protected]bdae5c12011-08-05 21:49:06519string16 CloudPrintHtmlDialogDelegate::GetDialogTitle() const {
520 return string16();
[email protected]73852b8f2010-05-14 00:38:12521}
522
523GURL CloudPrintHtmlDialogDelegate::GetDialogContentURL() const {
524 return params_.url;
525}
526
[email protected]36e12172011-02-08 23:46:02527void CloudPrintHtmlDialogDelegate::GetWebUIMessageHandlers(
528 std::vector<WebUIMessageHandler*>* handlers) const {
[email protected]73852b8f2010-05-14 00:38:12529 handlers->push_back(flow_handler_);
530 // We don't own flow_handler_ anymore, but it sticks around until at
531 // least right after OnDialogClosed() is called (and this object is
532 // destroyed).
533 owns_flow_handler_ = false;
534}
535
536void CloudPrintHtmlDialogDelegate::GetDialogSize(gfx::Size* size) const {
537 size->set_width(params_.width);
538 size->set_height(params_.height);
539}
540
541std::string CloudPrintHtmlDialogDelegate::GetDialogArgs() const {
542 return params_.json_input;
543}
544
545void CloudPrintHtmlDialogDelegate::OnDialogClosed(
546 const std::string& json_retval) {
[email protected]ea161da2010-11-02 21:57:35547 // Get the final dialog size and store it.
548 flow_handler_->StoreDialogClientSize();
[email protected]6ddda232011-04-22 15:41:47549
[email protected]d955fc92011-09-19 20:49:03550 if (delete_on_close_) {
[email protected]3e2dd4fa2011-11-10 06:06:40551 BrowserThread::PostTask(
552 BrowserThread::FILE, FROM_HERE,
553 base::Bind(&internal_cloud_print_helpers::Delete, path_to_file_));
[email protected]6ddda232011-04-22 15:41:47554 }
555
[email protected]e39027a2011-01-24 21:41:54556 // If we're modal we can show the dialog with no browser.
557 // End the keep-alive so that Chrome can exit.
558 if (!modal_)
559 BrowserList::EndKeepAlive();
[email protected]73852b8f2010-05-14 00:38:12560 delete this;
561}
562
[email protected]a81343d232011-12-27 07:39:20563void CloudPrintHtmlDialogDelegate::OnCloseContents(WebContents* source,
[email protected]18137e02010-05-25 21:10:35564 bool* out_close_dialog) {
565 if (out_close_dialog)
566 *out_close_dialog = true;
567}
568
[email protected]ea161da2010-11-02 21:57:35569bool CloudPrintHtmlDialogDelegate::ShouldShowDialogTitle() const {
570 return false;
571}
572
[email protected]34478212011-04-19 01:35:46573bool CloudPrintHtmlDialogDelegate::HandleContextMenu(
574 const ContextMenuParams& params) {
575 return true;
576}
577
[email protected]d955fc92011-09-19 20:49:03578void CreatePrintDialogForBytesImpl(scoped_refptr<RefCountedBytes> data,
579 const string16& print_job_title,
580 const string16& print_ticket,
581 const std::string& file_type,
582 bool modal) {
583 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
584 // TODO([email protected]) Writing the PDF to a file before printing
585 // is wasteful. Modify the dialog flow to pull PDF data from memory.
586 // See http://code.google.com/p/chromium/issues/detail?id=44093
587 FilePath path;
588 if (file_util::CreateTemporaryFile(&path)) {
589 file_util::WriteFile(path,
590 reinterpret_cast<const char*>(data->front()),
591 data->size());
592 }
593 print_dialog_cloud::CreatePrintDialogForFile(path,
594 print_job_title,
595 print_ticket,
596 file_type,
597 modal,
598 true);
599}
600
[email protected]6085c70d2011-03-22 22:51:07601// Called from the UI thread, starts up the dialog.
602void CreateDialogImpl(const FilePath& path_to_file,
603 const string16& print_job_title,
[email protected]e8368e92011-08-20 04:05:56604 const string16& print_ticket,
[email protected]6085c70d2011-03-22 22:51:07605 const std::string& file_type,
[email protected]d955fc92011-09-19 20:49:03606 bool modal,
607 bool delete_on_close) {
[email protected]ba4f1132010-10-09 02:02:35608 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]6085c70d2011-03-22 22:51:07609 Browser* browser = BrowserList::GetLastActive();
[email protected]73852b8f2010-05-14 00:38:12610
[email protected]c142d862012-01-11 20:11:03611 const int kDefaultWidth = 912;
612 const int kDefaultHeight = 633;
[email protected]05acb55472011-02-03 00:11:07613 string16 job_title = print_job_title;
[email protected]e39027a2011-01-24 21:41:54614 Profile* profile = NULL;
[email protected]05acb55472011-02-03 00:11:07615 if (modal) {
[email protected]0ec4898e2011-12-30 21:09:24616 if (job_title.empty()) {
[email protected]4ca15302012-01-03 05:53:20617 WebContents* web_contents = browser->GetSelectedWebContents();
[email protected]0ec4898e2011-12-30 21:09:24618 if (web_contents)
619 job_title = web_contents->GetTitle();
620 }
[email protected]6085c70d2011-03-22 22:51:07621 profile = browser->GetProfile();
[email protected]e39027a2011-01-24 21:41:54622 } else {
[email protected]0744cc72011-08-12 12:05:45623 std::vector<Profile*> loaded_profiles =
624 g_browser_process->profile_manager()->GetLoadedProfiles();
625 DCHECK_GT(loaded_profiles.size(), 0U);
626 profile = loaded_profiles[0];
[email protected]e39027a2011-01-24 21:41:54627 }
628 DCHECK(profile);
[email protected]05acb55472011-02-03 00:11:07629 PrefService* pref_service = profile->GetPrefs();
[email protected]ea161da2010-11-02 21:57:35630 DCHECK(pref_service);
631 if (!pref_service->FindPreference(prefs::kCloudPrintDialogWidth)) {
632 pref_service->RegisterIntegerPref(prefs::kCloudPrintDialogWidth,
[email protected]d36f941b2011-05-09 06:19:16633 kDefaultWidth,
634 PrefService::UNSYNCABLE_PREF);
[email protected]ea161da2010-11-02 21:57:35635 }
636 if (!pref_service->FindPreference(prefs::kCloudPrintDialogHeight)) {
637 pref_service->RegisterIntegerPref(prefs::kCloudPrintDialogHeight,
[email protected]d36f941b2011-05-09 06:19:16638 kDefaultHeight,
639 PrefService::UNSYNCABLE_PREF);
[email protected]ea161da2010-11-02 21:57:35640 }
641
642 int width = pref_service->GetInteger(prefs::kCloudPrintDialogWidth);
643 int height = pref_service->GetInteger(prefs::kCloudPrintDialogHeight);
[email protected]e39027a2011-01-24 21:41:54644
[email protected]73852b8f2010-05-14 00:38:12645 HtmlDialogUIDelegate* dialog_delegate =
646 new internal_cloud_print_helpers::CloudPrintHtmlDialogDelegate(
[email protected]e8368e92011-08-20 04:05:56647 path_to_file, width, height, std::string(), job_title, print_ticket,
[email protected]d955fc92011-09-19 20:49:03648 file_type, modal, delete_on_close);
[email protected]05acb55472011-02-03 00:11:07649 if (modal) {
[email protected]6085c70d2011-03-22 22:51:07650 DCHECK(browser);
[email protected]b73889c2012-01-11 20:13:23651#if defined(USE_AURA)
652 HtmlDialogView* html_view =
653 new HtmlDialogView(profile, dialog_delegate);
654 views::Widget::CreateWindowWithParent(html_view,
655 browser->window()->GetNativeHandle());
656 html_view->InitDialog();
657 html_view->GetWidget()->Show();
658#else
[email protected]22c830d2011-11-29 20:00:37659 browser->BrowserShowHtmlDialog(dialog_delegate, NULL, STYLE_GENERIC);
[email protected]b73889c2012-01-11 20:13:23660#endif
[email protected]e39027a2011-01-24 21:41:54661 } else {
[email protected]22c830d2011-11-29 20:00:37662 browser::ShowHtmlDialog(NULL, profile, dialog_delegate, STYLE_GENERIC);
[email protected]e39027a2011-01-24 21:41:54663 }
[email protected]73852b8f2010-05-14 00:38:12664}
[email protected]6085c70d2011-03-22 22:51:07665
[email protected]6ddda232011-04-22 15:41:47666// Provides a runnable function to delete a file.
667void Delete(const FilePath& file_path) {
668 file_util::Delete(file_path, false);
669}
670
[email protected]6085c70d2011-03-22 22:51:07671} // namespace internal_cloud_print_helpers
672
673namespace print_dialog_cloud {
674
675// Called on the FILE or UI thread. This is the main entry point into creating
676// the dialog.
677
678// TODO(scottbyer): The signature here will need to change as the
679// workflow through the printing code changes to allow for dynamically
680// changing page setup parameters while the dialog is active.
681void CreatePrintDialogForFile(const FilePath& path_to_file,
682 const string16& print_job_title,
[email protected]e8368e92011-08-20 04:05:56683 const string16& print_ticket,
[email protected]6085c70d2011-03-22 22:51:07684 const std::string& file_type,
[email protected]d955fc92011-09-19 20:49:03685 bool modal,
686 bool delete_on_close) {
[email protected]6085c70d2011-03-22 22:51:07687 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE) ||
688 BrowserThread::CurrentlyOn(BrowserThread::UI));
689
690 BrowserThread::PostTask(
691 BrowserThread::UI, FROM_HERE,
[email protected]3e2dd4fa2011-11-10 06:06:40692 base::Bind(&internal_cloud_print_helpers::CreateDialogImpl, path_to_file,
693 print_job_title, print_ticket, file_type, modal,
694 delete_on_close));
[email protected]d955fc92011-09-19 20:49:03695}
696
697void CreatePrintDialogForBytes(scoped_refptr<RefCountedBytes> data,
698 const string16& print_job_title,
699 const string16& print_ticket,
700 const std::string& file_type,
701 bool modal) {
702 // TODO([email protected]) Avoid cloning the PDF data. Make use of a
703 // shared memory object instead.
704 // http://code.google.com/p/chromium/issues/detail?id=44093
705 scoped_refptr<RefCountedBytes> cloned_data(new RefCountedBytes(data->data()));
706 BrowserThread::PostTask(
707 BrowserThread::FILE, FROM_HERE,
[email protected]3e2dd4fa2011-11-10 06:06:40708 base::Bind(&internal_cloud_print_helpers::CreatePrintDialogForBytesImpl,
709 cloned_data, print_job_title, print_ticket, file_type, modal));
[email protected]6085c70d2011-03-22 22:51:07710}
711
[email protected]65c9d89a2011-04-13 21:02:39712bool CreatePrintDialogFromCommandLine(const CommandLine& command_line) {
[email protected]87ab41e72012-01-04 18:45:11713 DCHECK(command_line.HasSwitch(switches::kCloudPrintFile));
[email protected]65c9d89a2011-04-13 21:02:39714 if (!command_line.GetSwitchValuePath(switches::kCloudPrintFile).empty()) {
715 FilePath cloud_print_file;
716 cloud_print_file =
717 command_line.GetSwitchValuePath(switches::kCloudPrintFile);
718 if (!cloud_print_file.empty()) {
719 string16 print_job_title;
[email protected]e8368e92011-08-20 04:05:56720 string16 print_job_print_ticket;
[email protected]65c9d89a2011-04-13 21:02:39721 if (command_line.HasSwitch(switches::kCloudPrintJobTitle)) {
[email protected]e8368e92011-08-20 04:05:56722 print_job_title =
723 internal_cloud_print_helpers::GetSwitchValueString16(
724 command_line, switches::kCloudPrintJobTitle);
725 }
726 if (command_line.HasSwitch(switches::kCloudPrintPrintTicket)) {
727 print_job_print_ticket =
728 internal_cloud_print_helpers::GetSwitchValueString16(
729 command_line, switches::kCloudPrintPrintTicket);
[email protected]65c9d89a2011-04-13 21:02:39730 }
731 std::string file_type = "application/pdf";
732 if (command_line.HasSwitch(switches::kCloudPrintFileType)) {
733 file_type = command_line.GetSwitchValueASCII(
734 switches::kCloudPrintFileType);
735 }
[email protected]e8368e92011-08-20 04:05:56736
[email protected]d955fc92011-09-19 20:49:03737 bool delete_on_close = CommandLine::ForCurrentProcess()->HasSwitch(
738 switches::kCloudPrintDeleteFile);
739
[email protected]65c9d89a2011-04-13 21:02:39740 print_dialog_cloud::CreatePrintDialogForFile(cloud_print_file,
741 print_job_title,
[email protected]e8368e92011-08-20 04:05:56742 print_job_print_ticket,
[email protected]65c9d89a2011-04-13 21:02:39743 file_type,
[email protected]d955fc92011-09-19 20:49:03744 false,
745 delete_on_close);
[email protected]65c9d89a2011-04-13 21:02:39746 return true;
747 }
748 }
749 return false;
750}
751
[email protected]6085c70d2011-03-22 22:51:07752} // end namespace