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