[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] | 22c830d | 2011-11-29 20:00:37 | [diff] [blame] | 24 | #include "chrome/browser/ui/dialog_style.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] | 5f945a0e | 2011-03-01 17:47:53 | [diff] [blame] | 30 | #include "content/browser/renderer_host/render_view_host.h" |
[email protected] | 5f945a0e | 2011-03-01 17:47:53 | [diff] [blame] | 31 | #include "content/browser/tab_contents/tab_contents_view.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 32 | #include "content/public/browser/browser_thread.h" |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 33 | #include "content/public/browser/navigation_controller.h" |
[email protected] | 022af74 | 2011-12-28 18:37:25 | [diff] [blame] | 34 | #include "content/public/browser/navigation_entry.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 35 | #include "content/public/browser/notification_registrar.h" |
| 36 | #include "content/public/browser/notification_source.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 37 | #include "content/public/browser/notification_types.h" |
[email protected] | 0ec4898e | 2011-12-30 21:09:24 | [diff] [blame] | 38 | #include "content/public/browser/web_contents.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] | 939856a | 2010-08-24 20:29:02 | [diff] [blame] | 41 | #include "webkit/glue/webpreferences.h" |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 42 | |
| 43 | #include "grit/generated_resources.h" |
| 44 | |
[email protected] | b73889c | 2012-01-11 20:13:23 | [diff] [blame] | 45 | #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] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 51 | // 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] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 55 | // generated print data to the dialog contents for uploading to the |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 56 | // cloud print service. |
| 57 | |
| 58 | // Currently, the flow between these classes is as follows: |
| 59 | |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 60 | // PrintDialogCloud::CreatePrintDialogForFile is called from |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 61 | // resource_message_filter_gtk.cc once the renderer has informed the |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 62 | // renderer host that print data generation into the renderer host provided |
[email protected] | 032682b | 2011-01-12 22:05:02 | [diff] [blame] | 63 | // temp file has been completed. That call is on the FILE thread. |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 64 | // 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] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 70 | // hands in the kChromeUICloudPrintResourcesURL as the URL to visit. That is |
[email protected] | 80a8fad | 2011-01-29 04:02:38 | [diff] [blame] | 71 | // recognized by the GetWebUIFactoryFunction as a signal to create an |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 72 | // ExternalHtmlDialogUI. |
| 73 | |
| 74 | // CloudPrintHtmlDialogDelegate also temporarily owns a |
| 75 | // CloudPrintFlowHandler, a class which is responsible for the actual |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 76 | // interactions with the dialog contents, including handing in the |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 77 | // 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] | c39f9bf | 2011-02-12 00:43:55 | [diff] [blame] | 80 | // WebUI::RenderViewCreated). That routine, in turn, calls the |
[email protected] | 36e1217 | 2011-02-08 23:46:02 | [diff] [blame] | 81 | // delegate's GetWebUIMessageHandlers routine, at which point the |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 82 | // 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] | c39f9bf | 2011-02-12 00:43:55 | [diff] [blame] | 86 | // The WebUI continues dialog bring-up, calling |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 87 | // 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] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 94 | // is currently used to send the data to the dialog contents. |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 95 | |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 96 | // In order to send the data to the dialog contents, the flow |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 97 | // handler uses a CloudPrintDataSender. It creates one, letting it |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 98 | // know the name of the temporary file containing the data, and |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 99 | // 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] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 111 | // high-level flow (where the data is generated before even |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 112 | // bringing up the dialog) isn't what we want. |
| 113 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 114 | using content::BrowserThread; |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 115 | using content::NavigationController; |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 116 | using content::NavigationEntry; |
[email protected] | a81343d23 | 2011-12-27 07:39:20 | [diff] [blame] | 117 | using content::WebContents; |
[email protected] | 26e2632a | 2011-12-31 04:02:55 | [diff] [blame] | 118 | using content::WebUIMessageHandler; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 119 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 120 | namespace internal_cloud_print_helpers { |
| 121 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 122 | // From the JSON parsed value, get the entries for the page setup |
| 123 | // parameters. |
| 124 | bool GetPageSetupParameters(const std::string& json, |
[email protected] | 1375e3ab | 2011-03-24 17:07:22 | [diff] [blame] | 125 | PrintMsg_Print_Params& parameters) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 126 | 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] | 05c7da6 | 2011-05-05 17:23:56 | [diff] [blame] | 135 | result &= params->GetDouble("dpi", ¶meters.dpi); |
| 136 | result &= params->GetDouble("min_shrink", ¶meters.min_shrink); |
| 137 | result &= params->GetDouble("max_shrink", ¶meters.max_shrink); |
[email protected] | a65175d | 2010-08-17 04:00:57 | [diff] [blame] | 138 | result &= params->GetBoolean("selection_only", ¶meters.selection_only); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 139 | return result; |
| 140 | } |
| 141 | |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 142 | string16 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] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 157 | void CloudPrintDataSenderHelper::CallJavascriptFunction( |
| 158 | const std::wstring& function_name) { |
[email protected] | adcf849 | 2011-03-09 22:41:39 | [diff] [blame] | 159 | web_ui_->CallJavascriptFunction(WideToASCII(function_name)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void CloudPrintDataSenderHelper::CallJavascriptFunction( |
| 163 | const std::wstring& function_name, const Value& arg) { |
[email protected] | adcf849 | 2011-03-09 22:41:39 | [diff] [blame] | 164 | web_ui_->CallJavascriptFunction(WideToASCII(function_name), arg); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void CloudPrintDataSenderHelper::CallJavascriptFunction( |
| 168 | const std::wstring& function_name, const Value& arg1, const Value& arg2) { |
[email protected] | adcf849 | 2011-03-09 22:41:39 | [diff] [blame] | 169 | web_ui_->CallJavascriptFunction(WideToASCII(function_name), arg1, arg2); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 170 | } |
| 171 | |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 172 | void CloudPrintDataSenderHelper::CallJavascriptFunction( |
| 173 | const std::wstring& function_name, |
| 174 | const Value& arg1, |
| 175 | const Value& arg2, |
| 176 | const Value& arg3) { |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 177 | web_ui_->CallJavascriptFunction( |
| 178 | WideToASCII(function_name), arg1, arg2, arg3); |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 179 | } |
| 180 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 181 | // 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. |
| 184 | void CloudPrintDataSender::CancelPrintDataFile() { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 185 | base::AutoLock lock(lock_); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 186 | // 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] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 191 | CloudPrintDataSender::CloudPrintDataSender(CloudPrintDataSenderHelper* helper, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 192 | const string16& print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 193 | const string16& print_ticket, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 194 | const std::string& file_type) |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 195 | : helper_(helper), |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 196 | print_job_title_(print_job_title), |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 197 | print_ticket_(print_ticket), |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 198 | file_type_(file_type) { |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | CloudPrintDataSender::~CloudPrintDataSender() {} |
| 202 | |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 203 | // Grab the raw file contents and massage them into shape for |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 204 | // 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] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 208 | void CloudPrintDataSender::ReadPrintDataFile(const FilePath& path_to_file) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 209 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 210 | int64 file_size = 0; |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 211 | if (file_util::GetFileSize(path_to_file, &file_size) && file_size != 0) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 212 | 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] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 218 | if (helper_ && file_util::ReadFileToString(path_to_file, &file_data)) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 219 | std::string base64_data; |
| 220 | base::Base64Encode(file_data, &base64_data); |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 221 | std::string header("data:"); |
| 222 | header.append(file_type_); |
| 223 | header.append(";base64,"); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 224 | base64_data.insert(0, header); |
| 225 | scoped_ptr<StringValue> new_data(new StringValue(base64_data)); |
| 226 | print_data_.swap(new_data); |
[email protected] | 3e2dd4fa | 2011-11-10 06:06:40 | [diff] [blame] | 227 | BrowserThread::PostTask( |
| 228 | BrowserThread::IO, FROM_HERE, |
| 229 | base::Bind(&CloudPrintDataSender::SendPrintDataFile, this)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 230 | } |
| 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 |
| 242 | void CloudPrintDataSender::SendPrintDataFile() { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 243 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 244 | base::AutoLock lock(lock_); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 245 | if (helper_ && print_data_.get()) { |
[email protected] | 9848c7e | 2010-06-03 16:06:56 | [diff] [blame] | 246 | StringValue title(print_job_title_); |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 247 | 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] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 250 | |
| 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] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 260 | CloudPrintFlowHandler::CloudPrintFlowHandler(const FilePath& path_to_file, |
| 261 | const string16& print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 262 | const string16& print_ticket, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 263 | const std::string& file_type) |
[email protected] | c7bf745 | 2011-09-12 21:31:50 | [diff] [blame] | 264 | : dialog_delegate_(NULL), |
| 265 | path_to_file_(path_to_file), |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 266 | print_job_title_(print_job_title), |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 267 | print_ticket_(print_ticket), |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 268 | file_type_(file_type) { |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | CloudPrintFlowHandler::~CloudPrintFlowHandler() { |
| 272 | // This will also cancel any task in flight. |
| 273 | CancelAnyRunningTask(); |
| 274 | } |
| 275 | |
| 276 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 277 | void CloudPrintFlowHandler::SetDialogDelegate( |
| 278 | CloudPrintHtmlDialogDelegate* delegate) { |
[email protected] | 7b74898 | 2011-02-14 19:28:23 | [diff] [blame] | 279 | // Even if setting a new WebUI, it means any previous task needs |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 280 | // to be cancelled, it's now invalid. |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 281 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 282 | 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. |
| 289 | void CloudPrintFlowHandler::CancelAnyRunningTask() { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 290 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 291 | if (print_data_sender_.get()) { |
| 292 | print_data_sender_->CancelPrintDataFile(); |
| 293 | print_data_sender_ = NULL; |
| 294 | } |
| 295 | } |
| 296 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 297 | void CloudPrintFlowHandler::RegisterMessages() { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 298 | // 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] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 300 | web_ui()->RegisterMessageCallback("ShowDebugger", |
[email protected] | ba4fc24 | 2011-10-04 18:56:56 | [diff] [blame] | 301 | base::Bind(&CloudPrintFlowHandler::HandleShowDebugger, |
| 302 | base::Unretained(this))); |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 303 | web_ui()->RegisterMessageCallback("SendPrintData", |
[email protected] | ba4fc24 | 2011-10-04 18:56:56 | [diff] [blame] | 304 | base::Bind(&CloudPrintFlowHandler::HandleSendPrintData, |
| 305 | base::Unretained(this))); |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 306 | web_ui()->RegisterMessageCallback("SetPageParameters", |
[email protected] | ba4fc24 | 2011-10-04 18:56:56 | [diff] [blame] | 307 | base::Bind(&CloudPrintFlowHandler::HandleSetPageParameters, |
| 308 | base::Unretained(this))); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 309 | |
[email protected] | 0eb25c4 | 2011-08-11 14:50:14 | [diff] [blame] | 310 | // 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] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 313 | NavigationController* controller = |
[email protected] | 01ec4ec | 2012-01-18 04:13:47 | [diff] [blame^] | 314 | &web_ui()->GetWebContents()->GetController(); |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 315 | NavigationEntry* pending_entry = controller->GetPendingEntry(); |
[email protected] | 0eb25c4 | 2011-08-11 14:50:14 | [diff] [blame] | 316 | if (pending_entry) { |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 317 | Profile* profile = Profile::FromWebUI(web_ui()); |
[email protected] | ad23a09 | 2011-12-28 07:02:04 | [diff] [blame] | 318 | pending_entry->SetURL( |
[email protected] | 0eb25c4 | 2011-08-11 14:50:14 | [diff] [blame] | 319 | CloudPrintURL(profile).GetCloudPrintServiceDialogURL()); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 320 | } |
[email protected] | 0eb25c4 | 2011-08-11 14:50:14 | [diff] [blame] | 321 | registrar_.Add(this, content::NOTIFICATION_LOAD_STOP, |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 322 | content::Source<NavigationController>(controller)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 323 | } |
| 324 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 325 | void CloudPrintFlowHandler::Observe( |
| 326 | int type, |
| 327 | const content::NotificationSource& source, |
| 328 | const content::NotificationDetails& details) { |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 329 | if (type == content::NOTIFICATION_LOAD_STOP) { |
[email protected] | 20c52d2 | 2011-06-20 22:42:42 | [diff] [blame] | 330 | // Take the opportunity to set some (minimal) additional |
| 331 | // script permissions required for the web UI. |
[email protected] | 01ec4ec | 2012-01-18 04:13:47 | [diff] [blame^] | 332 | GURL url = web_ui()->GetWebContents()->GetURL(); |
[email protected] | 20c52d2 | 2011-06-20 22:42:42 | [diff] [blame] | 333 | GURL dialog_url = CloudPrintURL( |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 334 | Profile::FromWebUI(web_ui())).GetCloudPrintServiceDialogURL(); |
[email protected] | 20c52d2 | 2011-06-20 22:42:42 | [diff] [blame] | 335 | if (url.host() == dialog_url.host() && |
| 336 | url.path() == dialog_url.path() && |
| 337 | url.scheme() == dialog_url.scheme()) { |
[email protected] | 01ec4ec | 2012-01-18 04:13:47 | [diff] [blame^] | 338 | RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost(); |
[email protected] | 20c52d2 | 2011-06-20 22:42:42 | [diff] [blame] | 339 | if (rvh && rvh->delegate()) { |
| 340 | WebPreferences webkit_prefs = rvh->delegate()->GetWebkitPrefs(); |
| 341 | webkit_prefs.allow_scripts_to_close_windows = true; |
[email protected] | 9abd51f | 2011-09-21 19:11:35 | [diff] [blame] | 342 | rvh->UpdateWebkitPreferences(webkit_prefs); |
[email protected] | 20c52d2 | 2011-06-20 22:42:42 | [diff] [blame] | 343 | } else { |
| 344 | DCHECK(false); |
| 345 | } |
| 346 | } |
| 347 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 348 | // 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] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 358 | void CloudPrintFlowHandler::HandleShowDebugger(const ListValue* args) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 359 | ShowDebugger(); |
| 360 | } |
| 361 | |
| 362 | void CloudPrintFlowHandler::ShowDebugger() { |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 363 | if (web_ui()) { |
[email protected] | 01ec4ec | 2012-01-18 04:13:47 | [diff] [blame^] | 364 | RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost(); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 365 | if (rvh) |
[email protected] | aebdd07 | 2011-07-07 12:36:59 | [diff] [blame] | 366 | DevToolsWindow::OpenDevToolsWindow(rvh); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | scoped_refptr<CloudPrintDataSender> |
| 371 | CloudPrintFlowHandler::CreateCloudPrintDataSender() { |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 372 | DCHECK(web_ui()); |
| 373 | print_data_helper_.reset(new CloudPrintDataSenderHelper(web_ui())); |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 374 | return new CloudPrintDataSender(print_data_helper_.get(), |
| 375 | print_job_title_, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 376 | print_ticket_, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 377 | file_type_); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 378 | } |
| 379 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 380 | void CloudPrintFlowHandler::HandleSendPrintData(const ListValue* args) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 381 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 382 | // 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] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 387 | if (web_ui()) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 388 | print_data_sender_ = CreateCloudPrintDataSender(); |
[email protected] | 3e2dd4fa | 2011-11-10 06:06:40 | [diff] [blame] | 389 | BrowserThread::PostTask( |
| 390 | BrowserThread::FILE, FROM_HERE, |
| 391 | base::Bind(&CloudPrintDataSender::ReadPrintDataFile, |
| 392 | print_data_sender_.get(), path_to_file_)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 396 | void CloudPrintFlowHandler::HandleSetPageParameters(const ListValue* args) { |
[email protected] | 036056a3 | 2011-03-03 21:05:01 | [diff] [blame] | 397 | std::string json; |
[email protected] | e675f7b | 2011-06-22 17:32:12 | [diff] [blame] | 398 | bool ret = args->GetString(0, &json); |
| 399 | if (!ret || json.empty()) { |
[email protected] | 036056a3 | 2011-03-03 21:05:01 | [diff] [blame] | 400 | NOTREACHED() << "Empty json string"; |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 401 | return; |
[email protected] | 036056a3 | 2011-03-03 21:05:01 | [diff] [blame] | 402 | } |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 403 | |
| 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] | 1375e3ab | 2011-03-24 17:07:22 | [diff] [blame] | 418 | PrintMsg_Print_Params default_settings; |
[email protected] | 1098044 | 2011-12-04 22:33:05 | [diff] [blame] | 419 | default_settings.content_size = gfx::Size(kWidth, kHeight); |
[email protected] | 732b813 | 2012-01-10 23:17:32 | [diff] [blame] | 420 | default_settings.printable_area = gfx::Rect(0, 0, kWidth, kHeight); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 421 | 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] | 718af82 | 2011-08-12 22:11:33 | [diff] [blame] | 427 | default_settings.preview_request_id = 0; |
| 428 | default_settings.is_first_request = true; |
[email protected] | 732b813 | 2012-01-10 23:17:32 | [diff] [blame] | 429 | default_settings.print_to_pdf = false; |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 430 | |
| 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] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 438 | // 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] | 439 | // currently synchronous, so there's a lot of work to do to get to |
| 440 | // that point. |
| 441 | } |
| 442 | |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 443 | void CloudPrintFlowHandler::StoreDialogClientSize() const { |
[email protected] | 01ec4ec | 2012-01-18 04:13:47 | [diff] [blame^] | 444 | if (web_ui() && web_ui()->GetWebContents() && |
| 445 | web_ui()->GetWebContents()->GetView()) { |
| 446 | gfx::Size size = web_ui()->GetWebContents()->GetView()->GetContainerSize(); |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 447 | Profile* profile = Profile::FromWebUI(web_ui()); |
[email protected] | 0eb25c4 | 2011-08-11 14:50:14 | [diff] [blame] | 448 | profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogWidth, |
| 449 | size.width()); |
| 450 | profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogHeight, |
| 451 | size.height()); |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 455 | CloudPrintHtmlDialogDelegate::CloudPrintHtmlDialogDelegate( |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 456 | const FilePath& path_to_file, |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 457 | int width, int height, |
[email protected] | 9848c7e | 2010-06-03 16:06:56 | [diff] [blame] | 458 | const std::string& json_arguments, |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 459 | const string16& print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 460 | const string16& print_ticket, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 461 | const std::string& file_type, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 462 | bool modal, |
| 463 | bool delete_on_close) |
| 464 | : delete_on_close_(delete_on_close), |
| 465 | flow_handler_(new CloudPrintFlowHandler(path_to_file, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 466 | print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 467 | print_ticket, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 468 | file_type)), |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 469 | modal_(modal), |
[email protected] | 6ddda23 | 2011-04-22 15:41:47 | [diff] [blame] | 470 | owns_flow_handler_(true), |
| 471 | path_to_file_(path_to_file) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 472 | Init(width, height, json_arguments); |
| 473 | } |
| 474 | |
[email protected] | 05acb5547 | 2011-02-03 00:11:07 | [diff] [blame] | 475 | // For unit testing. |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 476 | CloudPrintHtmlDialogDelegate::CloudPrintHtmlDialogDelegate( |
| 477 | CloudPrintFlowHandler* flow_handler, |
| 478 | int width, int height, |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 479 | const std::string& json_arguments, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 480 | bool modal, |
| 481 | bool delete_on_close) |
| 482 | : delete_on_close_(delete_on_close), |
| 483 | flow_handler_(flow_handler), |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 484 | modal_(modal), |
[email protected] | 18137e0 | 2010-05-25 21:10:35 | [diff] [blame] | 485 | owns_flow_handler_(true) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 486 | Init(width, height, json_arguments); |
| 487 | } |
| 488 | |
[email protected] | 05acb5547 | 2011-02-03 00:11:07 | [diff] [blame] | 489 | void CloudPrintHtmlDialogDelegate::Init(int width, int height, |
| 490 | const std::string& json_arguments) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 491 | // This information is needed to show the dialog HTML content. |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 492 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 493 | params_.url = GURL(chrome::kChromeUICloudPrintResourcesURL); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 494 | params_.height = height; |
| 495 | params_.width = width; |
| 496 | params_.json_input = json_arguments; |
| 497 | |
| 498 | flow_handler_->SetDialogDelegate(this); |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 499 | // 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] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | CloudPrintHtmlDialogDelegate::~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] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 508 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 509 | flow_handler_->SetDialogDelegate(NULL); |
| 510 | if (owns_flow_handler_) { |
| 511 | delete flow_handler_; |
| 512 | } |
| 513 | } |
| 514 | |
[email protected] | 707d6be6 | 2012-01-12 03:56:15 | [diff] [blame] | 515 | ui::ModalType CloudPrintHtmlDialogDelegate::GetDialogModalType() const { |
| 516 | return modal_ ? ui::MODAL_TYPE_WINDOW : ui::MODAL_TYPE_NONE; |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 517 | } |
| 518 | |
[email protected] | bdae5c1 | 2011-08-05 21:49:06 | [diff] [blame] | 519 | string16 CloudPrintHtmlDialogDelegate::GetDialogTitle() const { |
| 520 | return string16(); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | GURL CloudPrintHtmlDialogDelegate::GetDialogContentURL() const { |
| 524 | return params_.url; |
| 525 | } |
| 526 | |
[email protected] | 36e1217 | 2011-02-08 23:46:02 | [diff] [blame] | 527 | void CloudPrintHtmlDialogDelegate::GetWebUIMessageHandlers( |
| 528 | std::vector<WebUIMessageHandler*>* handlers) const { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 529 | 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 | |
| 536 | void CloudPrintHtmlDialogDelegate::GetDialogSize(gfx::Size* size) const { |
| 537 | size->set_width(params_.width); |
| 538 | size->set_height(params_.height); |
| 539 | } |
| 540 | |
| 541 | std::string CloudPrintHtmlDialogDelegate::GetDialogArgs() const { |
| 542 | return params_.json_input; |
| 543 | } |
| 544 | |
| 545 | void CloudPrintHtmlDialogDelegate::OnDialogClosed( |
| 546 | const std::string& json_retval) { |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 547 | // Get the final dialog size and store it. |
| 548 | flow_handler_->StoreDialogClientSize(); |
[email protected] | 6ddda23 | 2011-04-22 15:41:47 | [diff] [blame] | 549 | |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 550 | if (delete_on_close_) { |
[email protected] | 3e2dd4fa | 2011-11-10 06:06:40 | [diff] [blame] | 551 | BrowserThread::PostTask( |
| 552 | BrowserThread::FILE, FROM_HERE, |
| 553 | base::Bind(&internal_cloud_print_helpers::Delete, path_to_file_)); |
[email protected] | 6ddda23 | 2011-04-22 15:41:47 | [diff] [blame] | 554 | } |
| 555 | |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 556 | // 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] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 560 | delete this; |
| 561 | } |
| 562 | |
[email protected] | a81343d23 | 2011-12-27 07:39:20 | [diff] [blame] | 563 | void CloudPrintHtmlDialogDelegate::OnCloseContents(WebContents* source, |
[email protected] | 18137e0 | 2010-05-25 21:10:35 | [diff] [blame] | 564 | bool* out_close_dialog) { |
| 565 | if (out_close_dialog) |
| 566 | *out_close_dialog = true; |
| 567 | } |
| 568 | |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 569 | bool CloudPrintHtmlDialogDelegate::ShouldShowDialogTitle() const { |
| 570 | return false; |
| 571 | } |
| 572 | |
[email protected] | 3447821 | 2011-04-19 01:35:46 | [diff] [blame] | 573 | bool CloudPrintHtmlDialogDelegate::HandleContextMenu( |
| 574 | const ContextMenuParams& params) { |
| 575 | return true; |
| 576 | } |
| 577 | |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 578 | void 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] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 601 | // Called from the UI thread, starts up the dialog. |
| 602 | void CreateDialogImpl(const FilePath& path_to_file, |
| 603 | const string16& print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 604 | const string16& print_ticket, |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 605 | const std::string& file_type, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 606 | bool modal, |
| 607 | bool delete_on_close) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 608 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 609 | Browser* browser = BrowserList::GetLastActive(); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 610 | |
[email protected] | c142d86 | 2012-01-11 20:11:03 | [diff] [blame] | 611 | const int kDefaultWidth = 912; |
| 612 | const int kDefaultHeight = 633; |
[email protected] | 05acb5547 | 2011-02-03 00:11:07 | [diff] [blame] | 613 | string16 job_title = print_job_title; |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 614 | Profile* profile = NULL; |
[email protected] | 05acb5547 | 2011-02-03 00:11:07 | [diff] [blame] | 615 | if (modal) { |
[email protected] | 0ec4898e | 2011-12-30 21:09:24 | [diff] [blame] | 616 | if (job_title.empty()) { |
[email protected] | 4ca1530 | 2012-01-03 05:53:20 | [diff] [blame] | 617 | WebContents* web_contents = browser->GetSelectedWebContents(); |
[email protected] | 0ec4898e | 2011-12-30 21:09:24 | [diff] [blame] | 618 | if (web_contents) |
| 619 | job_title = web_contents->GetTitle(); |
| 620 | } |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 621 | profile = browser->GetProfile(); |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 622 | } else { |
[email protected] | 0744cc7 | 2011-08-12 12:05:45 | [diff] [blame] | 623 | 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] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 627 | } |
| 628 | DCHECK(profile); |
[email protected] | 05acb5547 | 2011-02-03 00:11:07 | [diff] [blame] | 629 | PrefService* pref_service = profile->GetPrefs(); |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 630 | DCHECK(pref_service); |
| 631 | if (!pref_service->FindPreference(prefs::kCloudPrintDialogWidth)) { |
| 632 | pref_service->RegisterIntegerPref(prefs::kCloudPrintDialogWidth, |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 633 | kDefaultWidth, |
| 634 | PrefService::UNSYNCABLE_PREF); |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 635 | } |
| 636 | if (!pref_service->FindPreference(prefs::kCloudPrintDialogHeight)) { |
| 637 | pref_service->RegisterIntegerPref(prefs::kCloudPrintDialogHeight, |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 638 | kDefaultHeight, |
| 639 | PrefService::UNSYNCABLE_PREF); |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | int width = pref_service->GetInteger(prefs::kCloudPrintDialogWidth); |
| 643 | int height = pref_service->GetInteger(prefs::kCloudPrintDialogHeight); |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 644 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 645 | HtmlDialogUIDelegate* dialog_delegate = |
| 646 | new internal_cloud_print_helpers::CloudPrintHtmlDialogDelegate( |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 647 | path_to_file, width, height, std::string(), job_title, print_ticket, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 648 | file_type, modal, delete_on_close); |
[email protected] | 05acb5547 | 2011-02-03 00:11:07 | [diff] [blame] | 649 | if (modal) { |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 650 | DCHECK(browser); |
[email protected] | b73889c | 2012-01-11 20:13:23 | [diff] [blame] | 651 | #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] | 22c830d | 2011-11-29 20:00:37 | [diff] [blame] | 659 | browser->BrowserShowHtmlDialog(dialog_delegate, NULL, STYLE_GENERIC); |
[email protected] | b73889c | 2012-01-11 20:13:23 | [diff] [blame] | 660 | #endif |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 661 | } else { |
[email protected] | 22c830d | 2011-11-29 20:00:37 | [diff] [blame] | 662 | browser::ShowHtmlDialog(NULL, profile, dialog_delegate, STYLE_GENERIC); |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 663 | } |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 664 | } |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 665 | |
[email protected] | 6ddda23 | 2011-04-22 15:41:47 | [diff] [blame] | 666 | // Provides a runnable function to delete a file. |
| 667 | void Delete(const FilePath& file_path) { |
| 668 | file_util::Delete(file_path, false); |
| 669 | } |
| 670 | |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 671 | } // namespace internal_cloud_print_helpers |
| 672 | |
| 673 | namespace 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. |
| 681 | void CreatePrintDialogForFile(const FilePath& path_to_file, |
| 682 | const string16& print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 683 | const string16& print_ticket, |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 684 | const std::string& file_type, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 685 | bool modal, |
| 686 | bool delete_on_close) { |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 687 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE) || |
| 688 | BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 689 | |
| 690 | BrowserThread::PostTask( |
| 691 | BrowserThread::UI, FROM_HERE, |
[email protected] | 3e2dd4fa | 2011-11-10 06:06:40 | [diff] [blame] | 692 | 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] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | void 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] | 3e2dd4fa | 2011-11-10 06:06:40 | [diff] [blame] | 708 | base::Bind(&internal_cloud_print_helpers::CreatePrintDialogForBytesImpl, |
| 709 | cloned_data, print_job_title, print_ticket, file_type, modal)); |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 710 | } |
| 711 | |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 712 | bool CreatePrintDialogFromCommandLine(const CommandLine& command_line) { |
[email protected] | 87ab41e7 | 2012-01-04 18:45:11 | [diff] [blame] | 713 | DCHECK(command_line.HasSwitch(switches::kCloudPrintFile)); |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 714 | 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] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 720 | string16 print_job_print_ticket; |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 721 | if (command_line.HasSwitch(switches::kCloudPrintJobTitle)) { |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 722 | 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] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 730 | } |
| 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] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 736 | |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 737 | bool delete_on_close = CommandLine::ForCurrentProcess()->HasSwitch( |
| 738 | switches::kCloudPrintDeleteFile); |
| 739 | |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 740 | print_dialog_cloud::CreatePrintDialogForFile(cloud_print_file, |
| 741 | print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 742 | print_job_print_ticket, |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 743 | file_type, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 744 | false, |
| 745 | delete_on_close); |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 746 | return true; |
| 747 | } |
| 748 | } |
| 749 | return false; |
| 750 | } |
| 751 | |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 752 | } // end namespace |