[email protected] | 87ab41e7 | 2012-01-04 18:45:11 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 2 | // 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] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 8 | #include "base/base64.h" |
[email protected] | ba4fc24 | 2011-10-04 18:56:56 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/bind_helpers.h" |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 11 | #include "base/command_line.h" |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 12 | #include "base/file_util.h" |
| 13 | #include "base/json/json_reader.h" |
[email protected] | 4b8852a | 2011-06-10 17:24:46 | [diff] [blame] | 14 | #include "base/utf_string_conversions.h" |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 15 | #include "base/values.h" |
[email protected] | 0744cc7 | 2011-08-12 12:05:45 | [diff] [blame] | 16 | #include "chrome/browser/browser_process_impl.h" |
[email protected] | dce50276 | 2011-07-20 08:53:49 | [diff] [blame] | 17 | #include "chrome/browser/debugger/devtools_window.h" |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 18 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | 2283eead | 2010-09-29 23:17:30 | [diff] [blame] | 19 | #include "chrome/browser/printing/cloud_print/cloud_print_url.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 20 | #include "chrome/browser/profiles/profile.h" |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 21 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | eb2d790 | 2011-02-02 18:19:56 | [diff] [blame] | 22 | #include "chrome/browser/ui/browser_dialogs.h" |
[email protected] | 6768ac0 | 2011-04-06 17:41:04 | [diff] [blame] | 23 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | 3b7fe7a4 | 2012-04-24 02:49:31 | [diff] [blame] | 24 | #include "chrome/browser/ui/browser_window.h" |
[email protected] | 0ec4898e | 2011-12-30 21:09:24 | [diff] [blame] | 25 | #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 26 | #include "chrome/common/chrome_switches.h" |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 27 | #include "chrome/common/pref_names.h" |
[email protected] | 1375e3ab | 2011-03-24 17:07:22 | [diff] [blame] | 28 | #include "chrome/common/print_messages.h" |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 29 | #include "chrome/common/url_constants.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 30 | #include "content/public/browser/browser_thread.h" |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 31 | #include "content/public/browser/navigation_controller.h" |
[email protected] | 022af74 | 2011-12-28 18:37:25 | [diff] [blame] | 32 | #include "content/public/browser/navigation_entry.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 33 | #include "content/public/browser/notification_registrar.h" |
| 34 | #include "content/public/browser/notification_source.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 35 | #include "content/public/browser/notification_types.h" |
[email protected] | 9c1662b | 2012-03-06 15:44:33 | [diff] [blame] | 36 | #include "content/public/browser/render_view_host.h" |
[email protected] | 0ec4898e | 2011-12-30 21:09:24 | [diff] [blame] | 37 | #include "content/public/browser/web_contents.h" |
[email protected] | 8643e6d | 2012-01-18 20:26:10 | [diff] [blame] | 38 | #include "content/public/browser/web_contents_view.h" |
[email protected] | 01ec4ec | 2012-01-18 04:13:47 | [diff] [blame] | 39 | #include "content/public/browser/web_ui.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 40 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 520c202 | 2012-03-15 00:13:15 | [diff] [blame] | 41 | |
| 42 | #if defined(OS_WIN) |
| 43 | #include "ui/base/win/foreground_helper.h" |
| 44 | #endif |
| 45 | |
[email protected] | 939856a | 2010-08-24 20:29:02 | [diff] [blame] | 46 | #include "webkit/glue/webpreferences.h" |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 47 | |
| 48 | #include "grit/generated_resources.h" |
| 49 | |
[email protected] | b73889c | 2012-01-11 20:13:23 | [diff] [blame] | 50 | #if defined(USE_AURA) |
| 51 | #include "chrome/browser/ui/browser_window.h" |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 52 | #include "chrome/browser/ui/views/web_dialog_view.h" |
[email protected] | 520c202 | 2012-03-15 00:13:15 | [diff] [blame] | 53 | #include "ui/aura/root_window.h" |
[email protected] | b73889c | 2012-01-11 20:13:23 | [diff] [blame] | 54 | #include "ui/views/widget/widget.h" |
| 55 | #endif |
| 56 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 57 | // This module implements the UI support in Chrome for cloud printing. |
| 58 | // This means hosting a dialog containing HTML/JavaScript and using |
| 59 | // the published cloud print user interface integration APIs to get |
| 60 | // page setup settings from the dialog contents and provide the |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 61 | // generated print data to the dialog contents for uploading to the |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 62 | // cloud print service. |
| 63 | |
| 64 | // Currently, the flow between these classes is as follows: |
| 65 | |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 66 | // PrintDialogCloud::CreatePrintDialogForFile is called from |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 67 | // resource_message_filter_gtk.cc once the renderer has informed the |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 68 | // renderer host that print data generation into the renderer host provided |
[email protected] | 032682b | 2011-01-12 22:05:02 | [diff] [blame] | 69 | // temp file has been completed. That call is on the FILE thread. |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 70 | // That, in turn, hops over to the UI thread to create an instance of |
| 71 | // PrintDialogCloud. |
| 72 | |
| 73 | // The constructor for PrintDialogCloud creates a |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 74 | // CloudPrintWebDialogDelegate and asks the current active browser to |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 75 | // show an HTML dialog using that class as the delegate. That class |
[email protected] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 76 | // hands in the kChromeUICloudPrintResourcesURL as the URL to visit. That is |
[email protected] | 80a8fad | 2011-01-29 04:02:38 | [diff] [blame] | 77 | // recognized by the GetWebUIFactoryFunction as a signal to create an |
[email protected] | 02b5ccc | 2012-04-30 23:58:31 | [diff] [blame^] | 78 | // ExternalWebDialogUI. |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 79 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 80 | // CloudPrintWebDialogDelegate also temporarily owns a |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 81 | // CloudPrintFlowHandler, a class which is responsible for the actual |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 82 | // interactions with the dialog contents, including handing in the |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 83 | // print data and getting any page setup parameters that the dialog |
| 84 | // contents provides. As part of bringing up the dialog, |
[email protected] | 02b5ccc | 2012-04-30 23:58:31 | [diff] [blame^] | 85 | // WebDialogUI::RenderViewCreated is called (an override of |
[email protected] | c39f9bf | 2011-02-12 00:43:55 | [diff] [blame] | 86 | // WebUI::RenderViewCreated). That routine, in turn, calls the |
[email protected] | 36e1217 | 2011-02-08 23:46:02 | [diff] [blame] | 87 | // delegate's GetWebUIMessageHandlers routine, at which point the |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 88 | // ownership of the CloudPrintFlowHandler is handed over. A pointer |
| 89 | // to the flow handler is kept to facilitate communication back and |
| 90 | // forth between the two classes. |
| 91 | |
[email protected] | c39f9bf | 2011-02-12 00:43:55 | [diff] [blame] | 92 | // The WebUI continues dialog bring-up, calling |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 93 | // CloudPrintFlowHandler::RegisterMessages. This is where the |
| 94 | // additional object model capabilities are registered for the dialog |
| 95 | // contents to use. It is also at this time that capabilities for the |
| 96 | // dialog contents are adjusted to allow the dialog contents to close |
| 97 | // the window. In addition, the pending URL is redirected to the |
| 98 | // actual cloud print service URL. The flow controller also registers |
| 99 | // for notification of when the dialog contents finish loading, which |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 100 | // is currently used to send the data to the dialog contents. |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 101 | |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 102 | // In order to send the data to the dialog contents, the flow |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 103 | // handler uses a CloudPrintDataSender. It creates one, letting it |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 104 | // know the name of the temporary file containing the data, and |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 105 | // posts the task of reading the file |
| 106 | // (CloudPrintDataSender::ReadPrintDataFile) to the file thread. That |
| 107 | // routine reads in the file, and then hops over to the IO thread to |
| 108 | // send that data to the dialog contents. |
| 109 | |
| 110 | // When the dialog contents are finished (by either being cancelled or |
| 111 | // hitting the print button), the delegate is notified, and responds |
| 112 | // that the dialog should be closed, at which point things are torn |
| 113 | // down and released. |
| 114 | |
| 115 | // TODO(scottbyer): |
| 116 | // http://code.google.com/p/chromium/issues/detail?id=44093 The |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 117 | // high-level flow (where the data is generated before even |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 118 | // bringing up the dialog) isn't what we want. |
| 119 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 120 | using content::BrowserThread; |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 121 | using content::NavigationController; |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 122 | using content::NavigationEntry; |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 123 | using content::RenderViewHost; |
[email protected] | a81343d23 | 2011-12-27 07:39:20 | [diff] [blame] | 124 | using content::WebContents; |
[email protected] | 26e2632a | 2011-12-31 04:02:55 | [diff] [blame] | 125 | using content::WebUIMessageHandler; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 126 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 127 | namespace internal_cloud_print_helpers { |
| 128 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 129 | // From the JSON parsed value, get the entries for the page setup |
| 130 | // parameters. |
| 131 | bool GetPageSetupParameters(const std::string& json, |
[email protected] | 1375e3ab | 2011-03-24 17:07:22 | [diff] [blame] | 132 | PrintMsg_Print_Params& parameters) { |
[email protected] | cd578575 | 2012-04-11 00:15:41 | [diff] [blame] | 133 | scoped_ptr<Value> parsed_value(base::JSONReader::Read(json)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 134 | DLOG_IF(ERROR, (!parsed_value.get() || |
| 135 | !parsed_value->IsType(Value::TYPE_DICTIONARY))) |
| 136 | << "PageSetup call didn't have expected contents"; |
| 137 | if (!parsed_value.get() || !parsed_value->IsType(Value::TYPE_DICTIONARY)) |
| 138 | return false; |
| 139 | |
| 140 | bool result = true; |
| 141 | DictionaryValue* params = static_cast<DictionaryValue*>(parsed_value.get()); |
[email protected] | 05c7da6 | 2011-05-05 17:23:56 | [diff] [blame] | 142 | result &= params->GetDouble("dpi", ¶meters.dpi); |
| 143 | result &= params->GetDouble("min_shrink", ¶meters.min_shrink); |
| 144 | result &= params->GetDouble("max_shrink", ¶meters.max_shrink); |
[email protected] | a65175d | 2010-08-17 04:00:57 | [diff] [blame] | 145 | result &= params->GetBoolean("selection_only", ¶meters.selection_only); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 146 | return result; |
| 147 | } |
| 148 | |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 149 | string16 GetSwitchValueString16(const CommandLine& command_line, |
| 150 | const char* switchName) { |
| 151 | #ifdef OS_WIN |
| 152 | CommandLine::StringType native_switch_val; |
| 153 | native_switch_val = command_line.GetSwitchValueNative(switchName); |
| 154 | return string16(native_switch_val); |
| 155 | #elif defined(OS_POSIX) |
| 156 | // POSIX Command line string types are different. |
| 157 | CommandLine::StringType native_switch_val; |
| 158 | native_switch_val = command_line.GetSwitchValueASCII(switchName); |
| 159 | // Convert the ASCII string to UTF16 to prepare to pass. |
| 160 | return string16(ASCIIToUTF16(native_switch_val)); |
| 161 | #endif |
| 162 | } |
| 163 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 164 | void CloudPrintDataSenderHelper::CallJavascriptFunction( |
| 165 | const std::wstring& function_name) { |
[email protected] | adcf849 | 2011-03-09 22:41:39 | [diff] [blame] | 166 | web_ui_->CallJavascriptFunction(WideToASCII(function_name)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void CloudPrintDataSenderHelper::CallJavascriptFunction( |
| 170 | const std::wstring& function_name, const Value& arg) { |
[email protected] | adcf849 | 2011-03-09 22:41:39 | [diff] [blame] | 171 | web_ui_->CallJavascriptFunction(WideToASCII(function_name), arg); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void CloudPrintDataSenderHelper::CallJavascriptFunction( |
| 175 | const std::wstring& function_name, const Value& arg1, const Value& arg2) { |
[email protected] | adcf849 | 2011-03-09 22:41:39 | [diff] [blame] | 176 | web_ui_->CallJavascriptFunction(WideToASCII(function_name), arg1, arg2); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 177 | } |
| 178 | |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 179 | void CloudPrintDataSenderHelper::CallJavascriptFunction( |
| 180 | const std::wstring& function_name, |
| 181 | const Value& arg1, |
| 182 | const Value& arg2, |
| 183 | const Value& arg3) { |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 184 | web_ui_->CallJavascriptFunction( |
| 185 | WideToASCII(function_name), arg1, arg2, arg3); |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 186 | } |
| 187 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 188 | // Clears out the pointer we're using to communicate. Either routine is |
| 189 | // potentially expensive enough that stopping whatever is in progress |
| 190 | // is worth it. |
| 191 | void CloudPrintDataSender::CancelPrintDataFile() { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 192 | base::AutoLock lock(lock_); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 193 | // We don't own helper, it was passed in to us, so no need to |
| 194 | // delete, just let it go. |
| 195 | helper_ = NULL; |
| 196 | } |
| 197 | |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 198 | CloudPrintDataSender::CloudPrintDataSender(CloudPrintDataSenderHelper* helper, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 199 | const string16& print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 200 | const string16& print_ticket, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 201 | const std::string& file_type) |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 202 | : helper_(helper), |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 203 | print_job_title_(print_job_title), |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 204 | print_ticket_(print_ticket), |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 205 | file_type_(file_type) { |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | CloudPrintDataSender::~CloudPrintDataSender() {} |
| 209 | |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 210 | // Grab the raw file contents and massage them into shape for |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 211 | // sending to the dialog contents (and up to the cloud print server) |
| 212 | // by encoding it and prefixing it with the appropriate mime type. |
| 213 | // Once that is done, kick off the next part of the task on the IO |
| 214 | // thread. |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 215 | void CloudPrintDataSender::ReadPrintDataFile(const FilePath& path_to_file) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 216 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 217 | int64 file_size = 0; |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 218 | if (file_util::GetFileSize(path_to_file, &file_size) && file_size != 0) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 219 | std::string file_data; |
| 220 | if (file_size < kuint32max) { |
| 221 | file_data.reserve(static_cast<unsigned int>(file_size)); |
| 222 | } else { |
| 223 | DLOG(WARNING) << " print data file too large to reserve space"; |
| 224 | } |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 225 | if (helper_ && file_util::ReadFileToString(path_to_file, &file_data)) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 226 | std::string base64_data; |
| 227 | base::Base64Encode(file_data, &base64_data); |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 228 | std::string header("data:"); |
| 229 | header.append(file_type_); |
| 230 | header.append(";base64,"); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 231 | base64_data.insert(0, header); |
| 232 | scoped_ptr<StringValue> new_data(new StringValue(base64_data)); |
| 233 | print_data_.swap(new_data); |
[email protected] | 3e2dd4fa | 2011-11-10 06:06:40 | [diff] [blame] | 234 | BrowserThread::PostTask( |
| 235 | BrowserThread::IO, FROM_HERE, |
| 236 | base::Bind(&CloudPrintDataSender::SendPrintDataFile, this)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // We have the data in hand that needs to be pushed into the dialog |
| 242 | // contents; do so from the IO thread. |
| 243 | |
| 244 | // TODO(scottbyer): If the print data ends up being larger than the |
| 245 | // upload limit (currently 10MB), what we need to do is upload that |
| 246 | // large data to google docs and set the URL in the printing |
| 247 | // JavaScript to that location, and make sure it gets deleted when not |
| 248 | // needed. - 4/1/2010 |
| 249 | void CloudPrintDataSender::SendPrintDataFile() { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 250 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 251 | base::AutoLock lock(lock_); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 252 | if (helper_ && print_data_.get()) { |
[email protected] | 9848c7e | 2010-06-03 16:06:56 | [diff] [blame] | 253 | StringValue title(print_job_title_); |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 254 | StringValue ticket(print_ticket_); |
| 255 | // TODO(abodenha): Change Javascript call to pass in print ticket |
| 256 | // after server side support is added. Add test for it. |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 257 | |
| 258 | // Send the print data to the dialog contents. The JavaScript |
| 259 | // function is a preliminary API for prototyping purposes and is |
| 260 | // subject to change. |
| 261 | const_cast<CloudPrintDataSenderHelper*>(helper_)->CallJavascriptFunction( |
| 262 | L"printApp._printDataUrl", *print_data_, title); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 267 | CloudPrintFlowHandler::CloudPrintFlowHandler(const FilePath& path_to_file, |
| 268 | const string16& print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 269 | const string16& print_ticket, |
[email protected] | 4cd4902 | 2012-01-19 20:37:37 | [diff] [blame] | 270 | const std::string& file_type, |
| 271 | bool close_after_signin, |
| 272 | const base::Closure& callback) |
[email protected] | c7bf745 | 2011-09-12 21:31:50 | [diff] [blame] | 273 | : dialog_delegate_(NULL), |
| 274 | path_to_file_(path_to_file), |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 275 | print_job_title_(print_job_title), |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 276 | print_ticket_(print_ticket), |
[email protected] | 4cd4902 | 2012-01-19 20:37:37 | [diff] [blame] | 277 | file_type_(file_type), |
| 278 | close_after_signin_(close_after_signin), |
| 279 | callback_(callback) { |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | CloudPrintFlowHandler::~CloudPrintFlowHandler() { |
| 283 | // This will also cancel any task in flight. |
| 284 | CancelAnyRunningTask(); |
| 285 | } |
| 286 | |
| 287 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 288 | void CloudPrintFlowHandler::SetDialogDelegate( |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 289 | CloudPrintWebDialogDelegate* delegate) { |
[email protected] | 7b74898 | 2011-02-14 19:28:23 | [diff] [blame] | 290 | // Even if setting a new WebUI, it means any previous task needs |
[email protected] | a2c92a1c | 2012-04-03 12:32:14 | [diff] [blame] | 291 | // to be canceled, its now invalid. |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 292 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 293 | CancelAnyRunningTask(); |
| 294 | dialog_delegate_ = delegate; |
| 295 | } |
| 296 | |
| 297 | // Cancels any print data sender we have in flight and removes our |
| 298 | // reference to it, so when the task that is calling it finishes and |
[email protected] | a2c92a1c | 2012-04-03 12:32:14 | [diff] [blame] | 299 | // removes its reference, it goes away. |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 300 | void CloudPrintFlowHandler::CancelAnyRunningTask() { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 301 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 302 | if (print_data_sender_.get()) { |
| 303 | print_data_sender_->CancelPrintDataFile(); |
| 304 | print_data_sender_ = NULL; |
| 305 | } |
| 306 | } |
| 307 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 308 | void CloudPrintFlowHandler::RegisterMessages() { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 309 | // TODO(scottbyer) - This is where we will register messages for the |
| 310 | // UI JS to use. Needed: Call to update page setup parameters. |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 311 | web_ui()->RegisterMessageCallback("ShowDebugger", |
[email protected] | ba4fc24 | 2011-10-04 18:56:56 | [diff] [blame] | 312 | base::Bind(&CloudPrintFlowHandler::HandleShowDebugger, |
| 313 | base::Unretained(this))); |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 314 | web_ui()->RegisterMessageCallback("SendPrintData", |
[email protected] | ba4fc24 | 2011-10-04 18:56:56 | [diff] [blame] | 315 | base::Bind(&CloudPrintFlowHandler::HandleSendPrintData, |
| 316 | base::Unretained(this))); |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 317 | web_ui()->RegisterMessageCallback("SetPageParameters", |
[email protected] | ba4fc24 | 2011-10-04 18:56:56 | [diff] [blame] | 318 | base::Bind(&CloudPrintFlowHandler::HandleSetPageParameters, |
| 319 | base::Unretained(this))); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 320 | |
[email protected] | 0eb25c4 | 2011-08-11 14:50:14 | [diff] [blame] | 321 | // Register for appropriate notifications, and re-direct the URL |
| 322 | // to the real server URL, now that we've gotten an HTML dialog |
| 323 | // going. |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 324 | NavigationController* controller = |
[email protected] | 01ec4ec | 2012-01-18 04:13:47 | [diff] [blame] | 325 | &web_ui()->GetWebContents()->GetController(); |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 326 | NavigationEntry* pending_entry = controller->GetPendingEntry(); |
[email protected] | 0eb25c4 | 2011-08-11 14:50:14 | [diff] [blame] | 327 | if (pending_entry) { |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 328 | Profile* profile = Profile::FromWebUI(web_ui()); |
[email protected] | 4cd4902 | 2012-01-19 20:37:37 | [diff] [blame] | 329 | if (close_after_signin_) { |
| 330 | pending_entry->SetURL( |
| 331 | CloudPrintURL(profile).GetCloudPrintSigninURL()); |
| 332 | } else { |
| 333 | pending_entry->SetURL( |
| 334 | CloudPrintURL(profile).GetCloudPrintServiceDialogURL()); |
| 335 | } |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 336 | } |
[email protected] | 0eb25c4 | 2011-08-11 14:50:14 | [diff] [blame] | 337 | registrar_.Add(this, content::NOTIFICATION_LOAD_STOP, |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 338 | content::Source<NavigationController>(controller)); |
[email protected] | 4cd4902 | 2012-01-19 20:37:37 | [diff] [blame] | 339 | if (close_after_signin_) { |
| 340 | registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 341 | content::Source<NavigationController>(controller)); |
| 342 | } |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 343 | } |
| 344 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 345 | void CloudPrintFlowHandler::Observe( |
| 346 | int type, |
| 347 | const content::NotificationSource& source, |
| 348 | const content::NotificationDetails& details) { |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 349 | if (type == content::NOTIFICATION_LOAD_STOP) { |
[email protected] | 20c52d2 | 2011-06-20 22:42:42 | [diff] [blame] | 350 | // Take the opportunity to set some (minimal) additional |
| 351 | // script permissions required for the web UI. |
[email protected] | 01ec4ec | 2012-01-18 04:13:47 | [diff] [blame] | 352 | GURL url = web_ui()->GetWebContents()->GetURL(); |
[email protected] | 20c52d2 | 2011-06-20 22:42:42 | [diff] [blame] | 353 | GURL dialog_url = CloudPrintURL( |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 354 | Profile::FromWebUI(web_ui())).GetCloudPrintServiceDialogURL(); |
[email protected] | 20c52d2 | 2011-06-20 22:42:42 | [diff] [blame] | 355 | if (url.host() == dialog_url.host() && |
| 356 | url.path() == dialog_url.path() && |
| 357 | url.scheme() == dialog_url.scheme()) { |
[email protected] | 01ec4ec | 2012-01-18 04:13:47 | [diff] [blame] | 358 | RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost(); |
[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 359 | if (rvh && rvh->GetDelegate()) { |
| 360 | WebPreferences webkit_prefs = rvh->GetDelegate()->GetWebkitPrefs(); |
[email protected] | 20c52d2 | 2011-06-20 22:42:42 | [diff] [blame] | 361 | webkit_prefs.allow_scripts_to_close_windows = true; |
[email protected] | 9abd51f | 2011-09-21 19:11:35 | [diff] [blame] | 362 | rvh->UpdateWebkitPreferences(webkit_prefs); |
[email protected] | 20c52d2 | 2011-06-20 22:42:42 | [diff] [blame] | 363 | } else { |
| 364 | DCHECK(false); |
| 365 | } |
| 366 | } |
| 367 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 368 | // Choose one or the other. If you need to debug, bring up the |
| 369 | // debugger. You can then use the various chrome.send() |
| 370 | // registrations above to kick of the various function calls, |
| 371 | // including chrome.send("SendPrintData") in the javaScript |
| 372 | // console and watch things happen with: |
| 373 | // HandleShowDebugger(NULL); |
| 374 | HandleSendPrintData(NULL); |
| 375 | } |
[email protected] | 4cd4902 | 2012-01-19 20:37:37 | [diff] [blame] | 376 | if (close_after_signin_ && |
| 377 | type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) { |
| 378 | GURL url = web_ui()->GetWebContents()->GetURL(); |
| 379 | GURL dialog_url = CloudPrintURL( |
| 380 | Profile::FromWebUI(web_ui())).GetCloudPrintServiceURL(); |
| 381 | |
| 382 | if (url.host() == dialog_url.host() && |
| 383 | url.path() == dialog_url.path() && |
| 384 | url.scheme() == dialog_url.scheme()) { |
| 385 | StoreDialogClientSize(); |
| 386 | web_ui()->GetWebContents()->GetRenderViewHost()->ClosePage(); |
| 387 | callback_.Run(); |
| 388 | } |
| 389 | } |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 390 | } |
| 391 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 392 | void CloudPrintFlowHandler::HandleShowDebugger(const ListValue* args) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 393 | ShowDebugger(); |
| 394 | } |
| 395 | |
| 396 | void CloudPrintFlowHandler::ShowDebugger() { |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 397 | if (web_ui()) { |
[email protected] | 01ec4ec | 2012-01-18 04:13:47 | [diff] [blame] | 398 | RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost(); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 399 | if (rvh) |
[email protected] | aebdd07 | 2011-07-07 12:36:59 | [diff] [blame] | 400 | DevToolsWindow::OpenDevToolsWindow(rvh); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
| 404 | scoped_refptr<CloudPrintDataSender> |
| 405 | CloudPrintFlowHandler::CreateCloudPrintDataSender() { |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 406 | DCHECK(web_ui()); |
| 407 | print_data_helper_.reset(new CloudPrintDataSenderHelper(web_ui())); |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 408 | return new CloudPrintDataSender(print_data_helper_.get(), |
| 409 | print_job_title_, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 410 | print_ticket_, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 411 | file_type_); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 412 | } |
| 413 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 414 | void CloudPrintFlowHandler::HandleSendPrintData(const ListValue* args) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 415 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 416 | // This will cancel any ReadPrintDataFile() or SendPrintDataFile() |
| 417 | // requests in flight (this is anticipation of when setting page |
| 418 | // setup parameters becomes asynchronous and may be set while some |
| 419 | // data is in flight). Then we can clear out the print data. |
| 420 | CancelAnyRunningTask(); |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 421 | if (web_ui()) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 422 | print_data_sender_ = CreateCloudPrintDataSender(); |
[email protected] | 3e2dd4fa | 2011-11-10 06:06:40 | [diff] [blame] | 423 | BrowserThread::PostTask( |
| 424 | BrowserThread::FILE, FROM_HERE, |
| 425 | base::Bind(&CloudPrintDataSender::ReadPrintDataFile, |
| 426 | print_data_sender_.get(), path_to_file_)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 430 | void CloudPrintFlowHandler::HandleSetPageParameters(const ListValue* args) { |
[email protected] | 036056a3 | 2011-03-03 21:05:01 | [diff] [blame] | 431 | std::string json; |
[email protected] | e675f7b | 2011-06-22 17:32:12 | [diff] [blame] | 432 | bool ret = args->GetString(0, &json); |
| 433 | if (!ret || json.empty()) { |
[email protected] | 036056a3 | 2011-03-03 21:05:01 | [diff] [blame] | 434 | NOTREACHED() << "Empty json string"; |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 435 | return; |
[email protected] | 036056a3 | 2011-03-03 21:05:01 | [diff] [blame] | 436 | } |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 437 | |
| 438 | // These are backstop default values - 72 dpi to match the screen, |
| 439 | // 8.5x11 inch paper with margins subtracted (1/4 inch top, left, |
| 440 | // right and 0.56 bottom), and the min page shrink and max page |
| 441 | // shrink values appear all over the place with no explanation. |
| 442 | |
| 443 | // TODO(scottbyer): Get a Linux/ChromeOS edge for PrintSettings |
| 444 | // working so that we can get the default values from there. Fix up |
| 445 | // PrintWebViewHelper to do the same. |
| 446 | const int kDPI = 72; |
| 447 | const int kWidth = static_cast<int>((8.5-0.25-0.25)*kDPI); |
| 448 | const int kHeight = static_cast<int>((11-0.25-0.56)*kDPI); |
| 449 | const double kMinPageShrink = 1.25; |
| 450 | const double kMaxPageShrink = 2.0; |
| 451 | |
[email protected] | 1375e3ab | 2011-03-24 17:07:22 | [diff] [blame] | 452 | PrintMsg_Print_Params default_settings; |
[email protected] | 1098044 | 2011-12-04 22:33:05 | [diff] [blame] | 453 | default_settings.content_size = gfx::Size(kWidth, kHeight); |
[email protected] | 732b813 | 2012-01-10 23:17:32 | [diff] [blame] | 454 | default_settings.printable_area = gfx::Rect(0, 0, kWidth, kHeight); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 455 | default_settings.dpi = kDPI; |
| 456 | default_settings.min_shrink = kMinPageShrink; |
| 457 | default_settings.max_shrink = kMaxPageShrink; |
| 458 | default_settings.desired_dpi = kDPI; |
| 459 | default_settings.document_cookie = 0; |
| 460 | default_settings.selection_only = false; |
[email protected] | 718af82 | 2011-08-12 22:11:33 | [diff] [blame] | 461 | default_settings.preview_request_id = 0; |
| 462 | default_settings.is_first_request = true; |
[email protected] | 732b813 | 2012-01-10 23:17:32 | [diff] [blame] | 463 | default_settings.print_to_pdf = false; |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 464 | |
| 465 | if (!GetPageSetupParameters(json, default_settings)) { |
| 466 | NOTREACHED(); |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | // TODO(scottbyer) - Here is where we would kick the originating |
| 471 | // renderer thread with these new parameters in order to get it to |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 472 | // re-generate the PDF data and hand it back to us. window.print() is |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 473 | // currently synchronous, so there's a lot of work to do to get to |
| 474 | // that point. |
| 475 | } |
| 476 | |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 477 | void CloudPrintFlowHandler::StoreDialogClientSize() const { |
[email protected] | 01ec4ec | 2012-01-18 04:13:47 | [diff] [blame] | 478 | if (web_ui() && web_ui()->GetWebContents() && |
| 479 | web_ui()->GetWebContents()->GetView()) { |
| 480 | gfx::Size size = web_ui()->GetWebContents()->GetView()->GetContainerSize(); |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 481 | Profile* profile = Profile::FromWebUI(web_ui()); |
[email protected] | 0eb25c4 | 2011-08-11 14:50:14 | [diff] [blame] | 482 | profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogWidth, |
| 483 | size.width()); |
| 484 | profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogHeight, |
| 485 | size.height()); |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 489 | CloudPrintWebDialogDelegate::CloudPrintWebDialogDelegate( |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 490 | const FilePath& path_to_file, |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 491 | int width, int height, |
[email protected] | 9848c7e | 2010-06-03 16:06:56 | [diff] [blame] | 492 | const std::string& json_arguments, |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 493 | const string16& print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 494 | const string16& print_ticket, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 495 | const std::string& file_type, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 496 | bool modal, |
[email protected] | 4cd4902 | 2012-01-19 20:37:37 | [diff] [blame] | 497 | bool delete_on_close, |
| 498 | bool close_after_signin, |
| 499 | const base::Closure& callback) |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 500 | : delete_on_close_(delete_on_close), |
| 501 | flow_handler_(new CloudPrintFlowHandler(path_to_file, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 502 | print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 503 | print_ticket, |
[email protected] | 4cd4902 | 2012-01-19 20:37:37 | [diff] [blame] | 504 | file_type, |
| 505 | close_after_signin, |
| 506 | callback)), |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 507 | modal_(modal), |
[email protected] | 6ddda23 | 2011-04-22 15:41:47 | [diff] [blame] | 508 | owns_flow_handler_(true), |
| 509 | path_to_file_(path_to_file) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 510 | Init(width, height, json_arguments); |
| 511 | } |
| 512 | |
[email protected] | 05acb5547 | 2011-02-03 00:11:07 | [diff] [blame] | 513 | // For unit testing. |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 514 | CloudPrintWebDialogDelegate::CloudPrintWebDialogDelegate( |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 515 | CloudPrintFlowHandler* flow_handler, |
| 516 | int width, int height, |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 517 | const std::string& json_arguments, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 518 | bool modal, |
| 519 | bool delete_on_close) |
| 520 | : delete_on_close_(delete_on_close), |
| 521 | flow_handler_(flow_handler), |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 522 | modal_(modal), |
[email protected] | 18137e0 | 2010-05-25 21:10:35 | [diff] [blame] | 523 | owns_flow_handler_(true) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 524 | Init(width, height, json_arguments); |
| 525 | } |
| 526 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 527 | void CloudPrintWebDialogDelegate::Init(int width, int height, |
| 528 | const std::string& json_arguments) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 529 | // This information is needed to show the dialog HTML content. |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 530 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 531 | params_.url = GURL(chrome::kChromeUICloudPrintResourcesURL); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 532 | params_.height = height; |
| 533 | params_.width = width; |
| 534 | params_.json_input = json_arguments; |
| 535 | |
| 536 | flow_handler_->SetDialogDelegate(this); |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 537 | // If we're not modal we can show the dialog with no browser. |
| 538 | // We need this to keep Chrome alive while our dialog is up. |
| 539 | if (!modal_) |
| 540 | BrowserList::StartKeepAlive(); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 541 | } |
| 542 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 543 | CloudPrintWebDialogDelegate::~CloudPrintWebDialogDelegate() { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 544 | // If the flow_handler_ is about to outlive us because we don't own |
[email protected] | a2c92a1c | 2012-04-03 12:32:14 | [diff] [blame] | 545 | // it anymore, we need to have it remove its reference to us. |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 546 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 547 | flow_handler_->SetDialogDelegate(NULL); |
| 548 | if (owns_flow_handler_) { |
| 549 | delete flow_handler_; |
| 550 | } |
| 551 | } |
| 552 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 553 | ui::ModalType CloudPrintWebDialogDelegate::GetDialogModalType() const { |
[email protected] | 707d6be6 | 2012-01-12 03:56:15 | [diff] [blame] | 554 | return modal_ ? ui::MODAL_TYPE_WINDOW : ui::MODAL_TYPE_NONE; |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 555 | } |
| 556 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 557 | string16 CloudPrintWebDialogDelegate::GetDialogTitle() const { |
[email protected] | bdae5c1 | 2011-08-05 21:49:06 | [diff] [blame] | 558 | return string16(); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 559 | } |
| 560 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 561 | GURL CloudPrintWebDialogDelegate::GetDialogContentURL() const { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 562 | return params_.url; |
| 563 | } |
| 564 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 565 | void CloudPrintWebDialogDelegate::GetWebUIMessageHandlers( |
[email protected] | 36e1217 | 2011-02-08 23:46:02 | [diff] [blame] | 566 | std::vector<WebUIMessageHandler*>* handlers) const { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 567 | handlers->push_back(flow_handler_); |
| 568 | // We don't own flow_handler_ anymore, but it sticks around until at |
| 569 | // least right after OnDialogClosed() is called (and this object is |
| 570 | // destroyed). |
| 571 | owns_flow_handler_ = false; |
| 572 | } |
| 573 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 574 | void CloudPrintWebDialogDelegate::GetDialogSize(gfx::Size* size) const { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 575 | size->set_width(params_.width); |
| 576 | size->set_height(params_.height); |
| 577 | } |
| 578 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 579 | std::string CloudPrintWebDialogDelegate::GetDialogArgs() const { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 580 | return params_.json_input; |
| 581 | } |
| 582 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 583 | void CloudPrintWebDialogDelegate::OnDialogClosed( |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 584 | const std::string& json_retval) { |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 585 | // Get the final dialog size and store it. |
| 586 | flow_handler_->StoreDialogClientSize(); |
[email protected] | 6ddda23 | 2011-04-22 15:41:47 | [diff] [blame] | 587 | |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 588 | if (delete_on_close_) { |
[email protected] | 3e2dd4fa | 2011-11-10 06:06:40 | [diff] [blame] | 589 | BrowserThread::PostTask( |
| 590 | BrowserThread::FILE, FROM_HERE, |
| 591 | base::Bind(&internal_cloud_print_helpers::Delete, path_to_file_)); |
[email protected] | 6ddda23 | 2011-04-22 15:41:47 | [diff] [blame] | 592 | } |
| 593 | |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 594 | // If we're modal we can show the dialog with no browser. |
| 595 | // End the keep-alive so that Chrome can exit. |
| 596 | if (!modal_) |
| 597 | BrowserList::EndKeepAlive(); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 598 | delete this; |
| 599 | } |
| 600 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 601 | void CloudPrintWebDialogDelegate::OnCloseContents(WebContents* source, |
| 602 | bool* out_close_dialog) { |
[email protected] | 18137e0 | 2010-05-25 21:10:35 | [diff] [blame] | 603 | if (out_close_dialog) |
| 604 | *out_close_dialog = true; |
| 605 | } |
| 606 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 607 | bool CloudPrintWebDialogDelegate::ShouldShowDialogTitle() const { |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 608 | return false; |
| 609 | } |
| 610 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 611 | bool CloudPrintWebDialogDelegate::HandleContextMenu( |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 612 | const content::ContextMenuParams& params) { |
[email protected] | 3447821 | 2011-04-19 01:35:46 | [diff] [blame] | 613 | return true; |
| 614 | } |
| 615 | |
[email protected] | ebbccb95 | 2012-04-20 09:51:31 | [diff] [blame] | 616 | void CreatePrintDialogForBytesImpl(scoped_refptr<base::RefCountedBytes> data, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 617 | const string16& print_job_title, |
| 618 | const string16& print_ticket, |
| 619 | const std::string& file_type, |
| 620 | bool modal) { |
| 621 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 622 | // TODO([email protected]) Writing the PDF to a file before printing |
| 623 | // is wasteful. Modify the dialog flow to pull PDF data from memory. |
| 624 | // See http://code.google.com/p/chromium/issues/detail?id=44093 |
| 625 | FilePath path; |
| 626 | if (file_util::CreateTemporaryFile(&path)) { |
| 627 | file_util::WriteFile(path, |
| 628 | reinterpret_cast<const char*>(data->front()), |
| 629 | data->size()); |
| 630 | } |
| 631 | print_dialog_cloud::CreatePrintDialogForFile(path, |
| 632 | print_job_title, |
| 633 | print_ticket, |
| 634 | file_type, |
| 635 | modal, |
| 636 | true); |
| 637 | } |
| 638 | |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 639 | // Called from the UI thread, starts up the dialog. |
| 640 | void CreateDialogImpl(const FilePath& path_to_file, |
| 641 | const string16& print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 642 | const string16& print_ticket, |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 643 | const std::string& file_type, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 644 | bool modal, |
[email protected] | 4cd4902 | 2012-01-19 20:37:37 | [diff] [blame] | 645 | bool delete_on_close, |
| 646 | bool close_after_signin, |
| 647 | const base::Closure& callback) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 648 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 649 | Browser* browser = BrowserList::GetLastActive(); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 650 | |
[email protected] | c142d86 | 2012-01-11 20:11:03 | [diff] [blame] | 651 | const int kDefaultWidth = 912; |
| 652 | const int kDefaultHeight = 633; |
[email protected] | 05acb5547 | 2011-02-03 00:11:07 | [diff] [blame] | 653 | string16 job_title = print_job_title; |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 654 | Profile* profile = NULL; |
[email protected] | 05acb5547 | 2011-02-03 00:11:07 | [diff] [blame] | 655 | if (modal) { |
[email protected] | 0ec4898e | 2011-12-30 21:09:24 | [diff] [blame] | 656 | if (job_title.empty()) { |
[email protected] | 4ca1530 | 2012-01-03 05:53:20 | [diff] [blame] | 657 | WebContents* web_contents = browser->GetSelectedWebContents(); |
[email protected] | 0ec4898e | 2011-12-30 21:09:24 | [diff] [blame] | 658 | if (web_contents) |
| 659 | job_title = web_contents->GetTitle(); |
| 660 | } |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 661 | profile = browser->GetProfile(); |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 662 | } else { |
[email protected] | 0744cc7 | 2011-08-12 12:05:45 | [diff] [blame] | 663 | std::vector<Profile*> loaded_profiles = |
| 664 | g_browser_process->profile_manager()->GetLoadedProfiles(); |
| 665 | DCHECK_GT(loaded_profiles.size(), 0U); |
| 666 | profile = loaded_profiles[0]; |
[email protected] | c5e22aee5 | 2012-01-19 16:10:51 | [diff] [blame] | 667 | browser = BrowserList::GetLastActiveWithProfile(profile); |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 668 | } |
| 669 | DCHECK(profile); |
[email protected] | 05acb5547 | 2011-02-03 00:11:07 | [diff] [blame] | 670 | PrefService* pref_service = profile->GetPrefs(); |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 671 | DCHECK(pref_service); |
| 672 | if (!pref_service->FindPreference(prefs::kCloudPrintDialogWidth)) { |
| 673 | pref_service->RegisterIntegerPref(prefs::kCloudPrintDialogWidth, |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 674 | kDefaultWidth, |
| 675 | PrefService::UNSYNCABLE_PREF); |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 676 | } |
| 677 | if (!pref_service->FindPreference(prefs::kCloudPrintDialogHeight)) { |
| 678 | pref_service->RegisterIntegerPref(prefs::kCloudPrintDialogHeight, |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 679 | kDefaultHeight, |
| 680 | PrefService::UNSYNCABLE_PREF); |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | int width = pref_service->GetInteger(prefs::kCloudPrintDialogWidth); |
| 684 | int height = pref_service->GetInteger(prefs::kCloudPrintDialogHeight); |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 685 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 686 | WebDialogDelegate* dialog_delegate = |
| 687 | new internal_cloud_print_helpers::CloudPrintWebDialogDelegate( |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 688 | path_to_file, width, height, std::string(), job_title, print_ticket, |
[email protected] | 4cd4902 | 2012-01-19 20:37:37 | [diff] [blame] | 689 | file_type, modal, delete_on_close, close_after_signin, |
| 690 | callback); |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 691 | browser::ShowWebDialog(modal ? browser->window()->GetNativeHandle() : NULL, |
[email protected] | 60fd60e | 2012-04-26 17:58:33 | [diff] [blame] | 692 | profile, browser, dialog_delegate); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 693 | } |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 694 | |
[email protected] | 4cd4902 | 2012-01-19 20:37:37 | [diff] [blame] | 695 | void CreateDialogSigninImpl(const base::Closure& callback) { |
| 696 | CreateDialogImpl(FilePath(), string16(), string16(), std::string(), |
| 697 | true, false, true, callback); |
| 698 | } |
| 699 | |
| 700 | void CreateDialogFullImpl(const FilePath& path_to_file, |
| 701 | const string16& print_job_title, |
| 702 | const string16& print_ticket, |
| 703 | const std::string& file_type, |
| 704 | bool modal, |
| 705 | bool delete_on_close) { |
| 706 | CreateDialogImpl(path_to_file, print_job_title, print_ticket, file_type, |
| 707 | modal, delete_on_close, false, base::Closure()); |
| 708 | } |
| 709 | |
| 710 | |
| 711 | |
[email protected] | 6ddda23 | 2011-04-22 15:41:47 | [diff] [blame] | 712 | // Provides a runnable function to delete a file. |
| 713 | void Delete(const FilePath& file_path) { |
| 714 | file_util::Delete(file_path, false); |
| 715 | } |
| 716 | |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 717 | } // namespace internal_cloud_print_helpers |
| 718 | |
| 719 | namespace print_dialog_cloud { |
| 720 | |
| 721 | // Called on the FILE or UI thread. This is the main entry point into creating |
| 722 | // the dialog. |
| 723 | |
| 724 | // TODO(scottbyer): The signature here will need to change as the |
| 725 | // workflow through the printing code changes to allow for dynamically |
| 726 | // changing page setup parameters while the dialog is active. |
| 727 | void CreatePrintDialogForFile(const FilePath& path_to_file, |
| 728 | const string16& print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 729 | const string16& print_ticket, |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 730 | const std::string& file_type, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 731 | bool modal, |
| 732 | bool delete_on_close) { |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 733 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE) || |
| 734 | BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 735 | |
| 736 | BrowserThread::PostTask( |
| 737 | BrowserThread::UI, FROM_HERE, |
[email protected] | 4cd4902 | 2012-01-19 20:37:37 | [diff] [blame] | 738 | base::Bind(&internal_cloud_print_helpers::CreateDialogFullImpl, |
| 739 | path_to_file, print_job_title, print_ticket, file_type, modal, |
[email protected] | 3e2dd4fa | 2011-11-10 06:06:40 | [diff] [blame] | 740 | delete_on_close)); |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 741 | } |
| 742 | |
[email protected] | 4cd4902 | 2012-01-19 20:37:37 | [diff] [blame] | 743 | void CreateCloudPrintSigninDialog(const base::Closure& callback) { |
| 744 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 745 | |
| 746 | BrowserThread::PostTask( |
| 747 | BrowserThread::UI, FROM_HERE, |
| 748 | base::Bind(&internal_cloud_print_helpers::CreateDialogSigninImpl, |
| 749 | callback)); |
| 750 | } |
| 751 | |
[email protected] | ebbccb95 | 2012-04-20 09:51:31 | [diff] [blame] | 752 | void CreatePrintDialogForBytes(scoped_refptr<base::RefCountedBytes> data, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 753 | const string16& print_job_title, |
| 754 | const string16& print_ticket, |
| 755 | const std::string& file_type, |
| 756 | bool modal) { |
| 757 | // TODO([email protected]) Avoid cloning the PDF data. Make use of a |
| 758 | // shared memory object instead. |
| 759 | // http://code.google.com/p/chromium/issues/detail?id=44093 |
[email protected] | ebbccb95 | 2012-04-20 09:51:31 | [diff] [blame] | 760 | scoped_refptr<base::RefCountedBytes> cloned_data = |
| 761 | new base::RefCountedBytes(data->data()); |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 762 | BrowserThread::PostTask( |
| 763 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 3e2dd4fa | 2011-11-10 06:06:40 | [diff] [blame] | 764 | base::Bind(&internal_cloud_print_helpers::CreatePrintDialogForBytesImpl, |
| 765 | cloned_data, print_job_title, print_ticket, file_type, modal)); |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 766 | } |
| 767 | |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 768 | bool CreatePrintDialogFromCommandLine(const CommandLine& command_line) { |
[email protected] | 87ab41e7 | 2012-01-04 18:45:11 | [diff] [blame] | 769 | DCHECK(command_line.HasSwitch(switches::kCloudPrintFile)); |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 770 | if (!command_line.GetSwitchValuePath(switches::kCloudPrintFile).empty()) { |
| 771 | FilePath cloud_print_file; |
| 772 | cloud_print_file = |
| 773 | command_line.GetSwitchValuePath(switches::kCloudPrintFile); |
| 774 | if (!cloud_print_file.empty()) { |
| 775 | string16 print_job_title; |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 776 | string16 print_job_print_ticket; |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 777 | if (command_line.HasSwitch(switches::kCloudPrintJobTitle)) { |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 778 | print_job_title = |
| 779 | internal_cloud_print_helpers::GetSwitchValueString16( |
| 780 | command_line, switches::kCloudPrintJobTitle); |
| 781 | } |
| 782 | if (command_line.HasSwitch(switches::kCloudPrintPrintTicket)) { |
| 783 | print_job_print_ticket = |
| 784 | internal_cloud_print_helpers::GetSwitchValueString16( |
| 785 | command_line, switches::kCloudPrintPrintTicket); |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 786 | } |
| 787 | std::string file_type = "application/pdf"; |
| 788 | if (command_line.HasSwitch(switches::kCloudPrintFileType)) { |
| 789 | file_type = command_line.GetSwitchValueASCII( |
| 790 | switches::kCloudPrintFileType); |
| 791 | } |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 792 | |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 793 | bool delete_on_close = CommandLine::ForCurrentProcess()->HasSwitch( |
| 794 | switches::kCloudPrintDeleteFile); |
| 795 | |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 796 | print_dialog_cloud::CreatePrintDialogForFile(cloud_print_file, |
| 797 | print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 798 | print_job_print_ticket, |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 799 | file_type, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 800 | false, |
| 801 | delete_on_close); |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 802 | return true; |
| 803 | } |
| 804 | } |
| 805 | return false; |
| 806 | } |
| 807 | |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 808 | } // end namespace |