[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] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 14 | #include "base/prefs/pref_service.h" |
[email protected] | 4b8852a | 2011-06-10 17:24:46 | [diff] [blame] | 15 | #include "base/utf_string_conversions.h" |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 16 | #include "base/values.h" |
[email protected] | 7001915 | 2012-12-19 11:44:19 | [diff] [blame] | 17 | #include "chrome/browser/devtools/devtools_window.h" |
[email protected] | 2e6389f | 2012-05-18 19:41:25 | [diff] [blame] | 18 | #include "chrome/browser/lifetime/application_lifetime.h" |
[email protected] | c753f14 | 2013-02-10 13:14:04 | [diff] [blame] | 19 | #include "chrome/browser/prefs/pref_registry_syncable.h" |
[email protected] | 2283eead | 2010-09-29 23:17:30 | [diff] [blame] | 20 | #include "chrome/browser/printing/cloud_print/cloud_print_url.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 21 | #include "chrome/browser/profiles/profile.h" |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 22 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | 508326df | 2012-05-23 16:01:19 | [diff] [blame] | 23 | #include "chrome/browser/ui/browser_dialogs.h" |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 24 | #include "chrome/common/chrome_switches.h" |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 25 | #include "chrome/common/pref_names.h" |
[email protected] | 1375e3ab | 2011-03-24 17:07:22 | [diff] [blame] | 26 | #include "chrome/common/print_messages.h" |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 27 | #include "chrome/common/url_constants.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 28 | #include "content/public/browser/browser_thread.h" |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 29 | #include "content/public/browser/navigation_controller.h" |
[email protected] | 022af74 | 2011-12-28 18:37:25 | [diff] [blame] | 30 | #include "content/public/browser/navigation_entry.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 31 | #include "content/public/browser/notification_registrar.h" |
| 32 | #include "content/public/browser/notification_source.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 33 | #include "content/public/browser/notification_types.h" |
[email protected] | 9c1662b | 2012-03-06 15:44:33 | [diff] [blame] | 34 | #include "content/public/browser/render_view_host.h" |
[email protected] | 0ec4898e | 2011-12-30 21:09:24 | [diff] [blame] | 35 | #include "content/public/browser/web_contents.h" |
[email protected] | 8643e6d | 2012-01-18 20:26:10 | [diff] [blame] | 36 | #include "content/public/browser/web_contents_view.h" |
[email protected] | 01ec4ec | 2012-01-18 04:13:47 | [diff] [blame] | 37 | #include "content/public/browser/web_ui.h" |
[email protected] | fca6b9b | 2012-05-22 17:11:05 | [diff] [blame] | 38 | #include "grit/generated_resources.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 39 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | fca6b9b | 2012-05-22 17:11:05 | [diff] [blame] | 40 | #include "webkit/glue/webpreferences.h" |
[email protected] | 520c202 | 2012-03-15 00:13:15 | [diff] [blame] | 41 | |
[email protected] | 88bfd25b | 2012-06-22 06:28:33 | [diff] [blame] | 42 | #if defined(USE_AURA) |
| 43 | #include "ui/aura/root_window.h" |
| 44 | #include "ui/aura/window.h" |
| 45 | #endif |
| 46 | |
[email protected] | 520c202 | 2012-03-15 00:13:15 | [diff] [blame] | 47 | #if defined(OS_WIN) |
| 48 | #include "ui/base/win/foreground_helper.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 |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 68 | // CloudPrintWebDialogDelegate and asks the current active browser to |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 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] | 02b5ccc | 2012-04-30 23:58:31 | [diff] [blame] | 72 | // ExternalWebDialogUI. |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 73 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 74 | // CloudPrintWebDialogDelegate also temporarily owns a |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 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, |
[email protected] | 02b5ccc | 2012-04-30 23:58:31 | [diff] [blame] | 79 | // WebDialogUI::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 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 109 | using content::BrowserThread; |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 110 | using content::NavigationController; |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 111 | using content::NavigationEntry; |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 112 | using content::RenderViewHost; |
[email protected] | a81343d23 | 2011-12-27 07:39:20 | [diff] [blame] | 113 | using content::WebContents; |
[email protected] | 26e2632a | 2011-12-31 04:02:55 | [diff] [blame] | 114 | using content::WebUIMessageHandler; |
[email protected] | 20c07f8e | 2012-05-31 08:43:14 | [diff] [blame] | 115 | using ui::WebDialogDelegate; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 116 | |
[email protected] | 681958c | 2013-02-21 13:48:14 | [diff] [blame] | 117 | const int kDefaultWidth = 912; |
| 118 | const int kDefaultHeight = 633; |
| 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] | cd578575 | 2012-04-11 00:15:41 | [diff] [blame] | 126 | scoped_ptr<Value> parsed_value(base::JSONReader::Read(json)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 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] | 47e870b | 2013-02-24 21:14:53 | [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] | 47e870b | 2013-02-24 21:14:53 | [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] | 47e870b | 2013-02-24 21:14:53 | [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( |
[email protected] | 47e870b | 2013-02-24 21:14:53 | [diff] [blame] | 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] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 194 | const std::string& file_type, |
| 195 | const base::RefCountedMemory* data) |
[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] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 199 | file_type_(file_type), |
| 200 | data_(data) { |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | CloudPrintDataSender::~CloudPrintDataSender() {} |
| 204 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 205 | // We have the data in hand that needs to be pushed into the dialog |
| 206 | // contents; do so from the IO thread. |
| 207 | |
| 208 | // TODO(scottbyer): If the print data ends up being larger than the |
| 209 | // upload limit (currently 10MB), what we need to do is upload that |
| 210 | // large data to google docs and set the URL in the printing |
| 211 | // JavaScript to that location, and make sure it gets deleted when not |
| 212 | // needed. - 4/1/2010 |
[email protected] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 213 | void CloudPrintDataSender::SendPrintData() { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 214 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 215 | if (!data_ || !data_->size()) |
| 216 | return; |
| 217 | |
| 218 | std::string base64_data; |
| 219 | base::Base64Encode( |
| 220 | base::StringPiece(reinterpret_cast<const char*>(data_->front()), |
| 221 | data_->size()), |
| 222 | &base64_data); |
| 223 | data_ = NULL; |
| 224 | std::string header("data:"); |
| 225 | header.append(file_type_); |
| 226 | header.append(";base64,"); |
| 227 | base64_data.insert(0, header); |
| 228 | |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 229 | base::AutoLock lock(lock_); |
[email protected] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 230 | if (helper_) { |
[email protected] | 9848c7e | 2010-06-03 16:06:56 | [diff] [blame] | 231 | StringValue title(print_job_title_); |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 232 | StringValue ticket(print_ticket_); |
| 233 | // TODO(abodenha): Change Javascript call to pass in print ticket |
| 234 | // after server side support is added. Add test for it. |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 235 | |
| 236 | // Send the print data to the dialog contents. The JavaScript |
| 237 | // function is a preliminary API for prototyping purposes and is |
| 238 | // subject to change. |
| 239 | const_cast<CloudPrintDataSenderHelper*>(helper_)->CallJavascriptFunction( |
[email protected] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 240 | L"printApp._printDataUrl", StringValue(base64_data), title); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | |
[email protected] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 245 | CloudPrintFlowHandler::CloudPrintFlowHandler(const base::RefCountedMemory* data, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 246 | const string16& print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 247 | const string16& print_ticket, |
[email protected] | 4cd4902 | 2012-01-19 20:37:37 | [diff] [blame] | 248 | const std::string& file_type, |
| 249 | bool close_after_signin, |
| 250 | const base::Closure& callback) |
[email protected] | c7bf745 | 2011-09-12 21:31:50 | [diff] [blame] | 251 | : dialog_delegate_(NULL), |
[email protected] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 252 | data_(data), |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 253 | print_job_title_(print_job_title), |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 254 | print_ticket_(print_ticket), |
[email protected] | 4cd4902 | 2012-01-19 20:37:37 | [diff] [blame] | 255 | file_type_(file_type), |
| 256 | close_after_signin_(close_after_signin), |
| 257 | callback_(callback) { |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | CloudPrintFlowHandler::~CloudPrintFlowHandler() { |
| 261 | // This will also cancel any task in flight. |
| 262 | CancelAnyRunningTask(); |
| 263 | } |
| 264 | |
| 265 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 266 | void CloudPrintFlowHandler::SetDialogDelegate( |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 267 | CloudPrintWebDialogDelegate* delegate) { |
[email protected] | 7b74898 | 2011-02-14 19:28:23 | [diff] [blame] | 268 | // Even if setting a new WebUI, it means any previous task needs |
[email protected] | a2c92a1c | 2012-04-03 12:32:14 | [diff] [blame] | 269 | // to be canceled, its now invalid. |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 270 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 271 | CancelAnyRunningTask(); |
| 272 | dialog_delegate_ = delegate; |
| 273 | } |
| 274 | |
| 275 | // Cancels any print data sender we have in flight and removes our |
| 276 | // reference to it, so when the task that is calling it finishes and |
[email protected] | a2c92a1c | 2012-04-03 12:32:14 | [diff] [blame] | 277 | // removes its reference, it goes away. |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 278 | void CloudPrintFlowHandler::CancelAnyRunningTask() { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 279 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 280 | if (print_data_sender_.get()) { |
| 281 | print_data_sender_->CancelPrintDataFile(); |
| 282 | print_data_sender_ = NULL; |
| 283 | } |
| 284 | } |
| 285 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 286 | void CloudPrintFlowHandler::RegisterMessages() { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 287 | // TODO(scottbyer) - This is where we will register messages for the |
| 288 | // UI JS to use. Needed: Call to update page setup parameters. |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 289 | web_ui()->RegisterMessageCallback("ShowDebugger", |
[email protected] | ba4fc24 | 2011-10-04 18:56:56 | [diff] [blame] | 290 | base::Bind(&CloudPrintFlowHandler::HandleShowDebugger, |
| 291 | base::Unretained(this))); |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 292 | web_ui()->RegisterMessageCallback("SendPrintData", |
[email protected] | ba4fc24 | 2011-10-04 18:56:56 | [diff] [blame] | 293 | base::Bind(&CloudPrintFlowHandler::HandleSendPrintData, |
| 294 | base::Unretained(this))); |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 295 | web_ui()->RegisterMessageCallback("SetPageParameters", |
[email protected] | ba4fc24 | 2011-10-04 18:56:56 | [diff] [blame] | 296 | base::Bind(&CloudPrintFlowHandler::HandleSetPageParameters, |
| 297 | base::Unretained(this))); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 298 | |
[email protected] | 0eb25c4 | 2011-08-11 14:50:14 | [diff] [blame] | 299 | // Register for appropriate notifications, and re-direct the URL |
| 300 | // to the real server URL, now that we've gotten an HTML dialog |
| 301 | // going. |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 302 | NavigationController* controller = |
[email protected] | 01ec4ec | 2012-01-18 04:13:47 | [diff] [blame] | 303 | &web_ui()->GetWebContents()->GetController(); |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 304 | NavigationEntry* pending_entry = controller->GetPendingEntry(); |
[email protected] | 0eb25c4 | 2011-08-11 14:50:14 | [diff] [blame] | 305 | if (pending_entry) { |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 306 | Profile* profile = Profile::FromWebUI(web_ui()); |
[email protected] | 4cd4902 | 2012-01-19 20:37:37 | [diff] [blame] | 307 | if (close_after_signin_) { |
| 308 | pending_entry->SetURL( |
| 309 | CloudPrintURL(profile).GetCloudPrintSigninURL()); |
| 310 | } else { |
| 311 | pending_entry->SetURL( |
| 312 | CloudPrintURL(profile).GetCloudPrintServiceDialogURL()); |
| 313 | } |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 314 | } |
[email protected] | 0eb25c4 | 2011-08-11 14:50:14 | [diff] [blame] | 315 | registrar_.Add(this, content::NOTIFICATION_LOAD_STOP, |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 316 | content::Source<NavigationController>(controller)); |
[email protected] | 57c8cf2 | 2013-03-02 16:50:00 | [diff] [blame] | 317 | registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 318 | content::Source<NavigationController>(controller)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 319 | } |
| 320 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 321 | void CloudPrintFlowHandler::Observe( |
| 322 | int type, |
| 323 | const content::NotificationSource& source, |
| 324 | const content::NotificationDetails& details) { |
[email protected] | 57c8cf2 | 2013-03-02 16:50:00 | [diff] [blame] | 325 | switch (type) { |
| 326 | case content::NOTIFICATION_NAV_ENTRY_COMMITTED: { |
| 327 | NavigationEntry* entry = |
| 328 | web_ui()->GetWebContents()->GetController().GetActiveEntry(); |
| 329 | if (entry) |
| 330 | NavigationToURLDidCloseDialog(entry->GetURL()); |
| 331 | break; |
[email protected] | 20c52d2 | 2011-06-20 22:42:42 | [diff] [blame] | 332 | } |
[email protected] | 57c8cf2 | 2013-03-02 16:50:00 | [diff] [blame] | 333 | case content::NOTIFICATION_LOAD_STOP: { |
| 334 | // Take the opportunity to set some (minimal) additional |
| 335 | // script permissions required for the web UI. |
| 336 | GURL url = web_ui()->GetWebContents()->GetURL(); |
| 337 | GURL dialog_url = CloudPrintURL( |
| 338 | Profile::FromWebUI(web_ui())).GetCloudPrintServiceDialogURL(); |
| 339 | if (url.host() == dialog_url.host() && |
| 340 | url.path() == dialog_url.path() && |
| 341 | url.scheme() == dialog_url.scheme()) { |
| 342 | RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost(); |
| 343 | if (rvh) { |
| 344 | webkit_glue::WebPreferences webkit_prefs = |
| 345 | rvh->GetWebkitPreferences(); |
| 346 | webkit_prefs.allow_scripts_to_close_windows = true; |
| 347 | rvh->UpdateWebkitPreferences(webkit_prefs); |
| 348 | } else { |
| 349 | NOTREACHED(); |
| 350 | } |
| 351 | } |
[email protected] | 20c52d2 | 2011-06-20 22:42:42 | [diff] [blame] | 352 | |
[email protected] | 57c8cf2 | 2013-03-02 16:50:00 | [diff] [blame] | 353 | // Choose one or the other. If you need to debug, bring up the |
| 354 | // debugger. You can then use the various chrome.send() |
| 355 | // registrations above to kick of the various function calls, |
| 356 | // including chrome.send("SendPrintData") in the javaScript |
| 357 | // console and watch things happen with: |
| 358 | // HandleShowDebugger(NULL); |
| 359 | HandleSendPrintData(NULL); |
| 360 | break; |
| 361 | } |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 365 | void CloudPrintFlowHandler::HandleShowDebugger(const ListValue* args) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 366 | ShowDebugger(); |
| 367 | } |
| 368 | |
| 369 | void CloudPrintFlowHandler::ShowDebugger() { |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 370 | if (web_ui()) { |
[email protected] | 01ec4ec | 2012-01-18 04:13:47 | [diff] [blame] | 371 | RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost(); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 372 | if (rvh) |
[email protected] | aebdd07 | 2011-07-07 12:36:59 | [diff] [blame] | 373 | DevToolsWindow::OpenDevToolsWindow(rvh); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
| 377 | scoped_refptr<CloudPrintDataSender> |
| 378 | CloudPrintFlowHandler::CreateCloudPrintDataSender() { |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 379 | DCHECK(web_ui()); |
| 380 | print_data_helper_.reset(new CloudPrintDataSenderHelper(web_ui())); |
[email protected] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 381 | scoped_refptr<CloudPrintDataSender> sender( |
| 382 | new CloudPrintDataSender(print_data_helper_.get(), print_job_title_, |
| 383 | print_ticket_, file_type_, data_)); |
| 384 | data_ = NULL; |
| 385 | return sender; |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 386 | } |
| 387 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 388 | void CloudPrintFlowHandler::HandleSendPrintData(const ListValue* args) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 389 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 390 | // This will cancel any ReadPrintDataFile() or SendPrintDataFile() |
| 391 | // requests in flight (this is anticipation of when setting page |
| 392 | // setup parameters becomes asynchronous and may be set while some |
| 393 | // data is in flight). Then we can clear out the print data. |
| 394 | CancelAnyRunningTask(); |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 395 | if (web_ui()) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 396 | print_data_sender_ = CreateCloudPrintDataSender(); |
[email protected] | 3e2dd4fa | 2011-11-10 06:06:40 | [diff] [blame] | 397 | BrowserThread::PostTask( |
[email protected] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 398 | BrowserThread::IO, FROM_HERE, |
| 399 | base::Bind(&CloudPrintDataSender::SendPrintData, print_data_sender_)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 403 | void CloudPrintFlowHandler::HandleSetPageParameters(const ListValue* args) { |
[email protected] | 036056a3 | 2011-03-03 21:05:01 | [diff] [blame] | 404 | std::string json; |
[email protected] | e675f7b | 2011-06-22 17:32:12 | [diff] [blame] | 405 | bool ret = args->GetString(0, &json); |
| 406 | if (!ret || json.empty()) { |
[email protected] | 036056a3 | 2011-03-03 21:05:01 | [diff] [blame] | 407 | NOTREACHED() << "Empty json string"; |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 408 | return; |
[email protected] | 036056a3 | 2011-03-03 21:05:01 | [diff] [blame] | 409 | } |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 410 | |
| 411 | // These are backstop default values - 72 dpi to match the screen, |
| 412 | // 8.5x11 inch paper with margins subtracted (1/4 inch top, left, |
| 413 | // right and 0.56 bottom), and the min page shrink and max page |
| 414 | // shrink values appear all over the place with no explanation. |
| 415 | |
| 416 | // TODO(scottbyer): Get a Linux/ChromeOS edge for PrintSettings |
| 417 | // working so that we can get the default values from there. Fix up |
| 418 | // PrintWebViewHelper to do the same. |
| 419 | const int kDPI = 72; |
| 420 | const int kWidth = static_cast<int>((8.5-0.25-0.25)*kDPI); |
| 421 | const int kHeight = static_cast<int>((11-0.25-0.56)*kDPI); |
| 422 | const double kMinPageShrink = 1.25; |
| 423 | const double kMaxPageShrink = 2.0; |
| 424 | |
[email protected] | 1375e3ab | 2011-03-24 17:07:22 | [diff] [blame] | 425 | PrintMsg_Print_Params default_settings; |
[email protected] | 1098044 | 2011-12-04 22:33:05 | [diff] [blame] | 426 | default_settings.content_size = gfx::Size(kWidth, kHeight); |
[email protected] | 732b813 | 2012-01-10 23:17:32 | [diff] [blame] | 427 | default_settings.printable_area = gfx::Rect(0, 0, kWidth, kHeight); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 428 | default_settings.dpi = kDPI; |
| 429 | default_settings.min_shrink = kMinPageShrink; |
| 430 | default_settings.max_shrink = kMaxPageShrink; |
| 431 | default_settings.desired_dpi = kDPI; |
| 432 | default_settings.document_cookie = 0; |
| 433 | default_settings.selection_only = false; |
[email protected] | 718af82 | 2011-08-12 22:11:33 | [diff] [blame] | 434 | default_settings.preview_request_id = 0; |
| 435 | default_settings.is_first_request = true; |
[email protected] | 732b813 | 2012-01-10 23:17:32 | [diff] [blame] | 436 | default_settings.print_to_pdf = false; |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 437 | |
| 438 | if (!GetPageSetupParameters(json, default_settings)) { |
| 439 | NOTREACHED(); |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | // TODO(scottbyer) - Here is where we would kick the originating |
| 444 | // renderer thread with these new parameters in order to get it to |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 445 | // 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] | 446 | // currently synchronous, so there's a lot of work to do to get to |
| 447 | // that point. |
| 448 | } |
| 449 | |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 450 | void CloudPrintFlowHandler::StoreDialogClientSize() const { |
[email protected] | 01ec4ec | 2012-01-18 04:13:47 | [diff] [blame] | 451 | if (web_ui() && web_ui()->GetWebContents() && |
| 452 | web_ui()->GetWebContents()->GetView()) { |
| 453 | gfx::Size size = web_ui()->GetWebContents()->GetView()->GetContainerSize(); |
[email protected] | 46adf7ff | 2011-12-30 00:53:09 | [diff] [blame] | 454 | Profile* profile = Profile::FromWebUI(web_ui()); |
[email protected] | 0eb25c4 | 2011-08-11 14:50:14 | [diff] [blame] | 455 | profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogWidth, |
| 456 | size.width()); |
| 457 | profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogHeight, |
| 458 | size.height()); |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
[email protected] | a911c4e | 2012-10-12 00:37:57 | [diff] [blame] | 462 | bool CloudPrintFlowHandler::NavigationToURLDidCloseDialog(const GURL& url) { |
| 463 | if (close_after_signin_) { |
| 464 | GURL dialog_url = CloudPrintURL( |
| 465 | Profile::FromWebUI(web_ui())).GetCloudPrintServiceURL(); |
| 466 | |
| 467 | if (url.host() == dialog_url.host() && |
[email protected] | 22f45049 | 2013-02-20 22:02:51 | [diff] [blame] | 468 | StartsWithASCII(url.path(), dialog_url.path(), false) && |
[email protected] | a911c4e | 2012-10-12 00:37:57 | [diff] [blame] | 469 | url.scheme() == dialog_url.scheme()) { |
| 470 | StoreDialogClientSize(); |
| 471 | web_ui()->GetWebContents()->GetRenderViewHost()->ClosePage(); |
| 472 | callback_.Run(); |
| 473 | return true; |
| 474 | } |
| 475 | } |
| 476 | return false; |
| 477 | } |
| 478 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 479 | CloudPrintWebDialogDelegate::CloudPrintWebDialogDelegate( |
[email protected] | b5b79d7 | 2012-05-24 19:42:28 | [diff] [blame] | 480 | content::BrowserContext* browser_context, |
| 481 | gfx::NativeWindow modal_parent, |
[email protected] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 482 | const base::RefCountedMemory* data, |
[email protected] | 9848c7e | 2010-06-03 16:06:56 | [diff] [blame] | 483 | const std::string& json_arguments, |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 484 | const string16& print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 485 | const string16& print_ticket, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 486 | const std::string& file_type, |
[email protected] | 4cd4902 | 2012-01-19 20:37:37 | [diff] [blame] | 487 | bool close_after_signin, |
| 488 | const base::Closure& callback) |
[email protected] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 489 | : flow_handler_( |
| 490 | new CloudPrintFlowHandler(data, print_job_title, print_ticket, |
| 491 | file_type, close_after_signin, callback)), |
[email protected] | b5b79d7 | 2012-05-24 19:42:28 | [diff] [blame] | 492 | modal_parent_(modal_parent), |
[email protected] | 6ddda23 | 2011-04-22 15:41:47 | [diff] [blame] | 493 | owns_flow_handler_(true), |
[email protected] | b5b79d7 | 2012-05-24 19:42:28 | [diff] [blame] | 494 | keep_alive_when_non_modal_(true) { |
| 495 | Init(browser_context, json_arguments); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 496 | } |
| 497 | |
[email protected] | 05acb5547 | 2011-02-03 00:11:07 | [diff] [blame] | 498 | // For unit testing. |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 499 | CloudPrintWebDialogDelegate::CloudPrintWebDialogDelegate( |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 500 | CloudPrintFlowHandler* flow_handler, |
[email protected] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 501 | const std::string& json_arguments) |
| 502 | : flow_handler_(flow_handler), |
[email protected] | b5b79d7 | 2012-05-24 19:42:28 | [diff] [blame] | 503 | modal_parent_(NULL), |
| 504 | owns_flow_handler_(true), |
| 505 | keep_alive_when_non_modal_(false) { |
| 506 | Init(NULL, json_arguments); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 507 | } |
| 508 | |
[email protected] | b5b79d7 | 2012-05-24 19:42:28 | [diff] [blame] | 509 | // Returns the persisted width/height for the print dialog. |
| 510 | void GetDialogWidthAndHeightFromPrefs(content::BrowserContext* browser_context, |
| 511 | int* width, |
| 512 | int* height) { |
[email protected] | b5b79d7 | 2012-05-24 19:42:28 | [diff] [blame] | 513 | if (!browser_context) { |
| 514 | *width = kDefaultWidth; |
| 515 | *height = kDefaultHeight; |
| 516 | return; |
| 517 | } |
| 518 | |
[email protected] | c753f14 | 2013-02-10 13:14:04 | [diff] [blame] | 519 | PrefService* prefs = Profile::FromBrowserContext(browser_context)->GetPrefs(); |
[email protected] | c753f14 | 2013-02-10 13:14:04 | [diff] [blame] | 520 | *width = prefs->GetInteger(prefs::kCloudPrintDialogWidth); |
| 521 | *height = prefs->GetInteger(prefs::kCloudPrintDialogHeight); |
[email protected] | b5b79d7 | 2012-05-24 19:42:28 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | void CloudPrintWebDialogDelegate::Init(content::BrowserContext* browser_context, |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 525 | const std::string& json_arguments) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 526 | // This information is needed to show the dialog HTML content. |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 527 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | b5b79d7 | 2012-05-24 19:42:28 | [diff] [blame] | 528 | |
[email protected] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 529 | params_.url = GURL(chrome::kChromeUICloudPrintResourcesURL); |
[email protected] | b5b79d7 | 2012-05-24 19:42:28 | [diff] [blame] | 530 | GetDialogWidthAndHeightFromPrefs(browser_context, |
| 531 | ¶ms_.width, |
| 532 | ¶ms_.height); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 533 | params_.json_input = json_arguments; |
| 534 | |
| 535 | flow_handler_->SetDialogDelegate(this); |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 536 | // If we're not modal we can show the dialog with no browser. |
| 537 | // We need this to keep Chrome alive while our dialog is up. |
[email protected] | b5b79d7 | 2012-05-24 19:42:28 | [diff] [blame] | 538 | if (!modal_parent_ && keep_alive_when_non_modal_) |
[email protected] | 313fce1 | 2013-01-30 17:09:04 | [diff] [blame] | 539 | chrome::StartKeepAlive(); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 540 | } |
| 541 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 542 | CloudPrintWebDialogDelegate::~CloudPrintWebDialogDelegate() { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 543 | // 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] | 544 | // it anymore, we need to have it remove its reference to us. |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 545 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 546 | flow_handler_->SetDialogDelegate(NULL); |
| 547 | if (owns_flow_handler_) { |
| 548 | delete flow_handler_; |
| 549 | } |
| 550 | } |
| 551 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 552 | ui::ModalType CloudPrintWebDialogDelegate::GetDialogModalType() const { |
[email protected] | b5b79d7 | 2012-05-24 19:42:28 | [diff] [blame] | 553 | return modal_parent_ ? ui::MODAL_TYPE_WINDOW : ui::MODAL_TYPE_NONE; |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 554 | } |
| 555 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 556 | string16 CloudPrintWebDialogDelegate::GetDialogTitle() const { |
[email protected] | bdae5c1 | 2011-08-05 21:49:06 | [diff] [blame] | 557 | return string16(); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 558 | } |
| 559 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 560 | GURL CloudPrintWebDialogDelegate::GetDialogContentURL() const { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 561 | return params_.url; |
| 562 | } |
| 563 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 564 | void CloudPrintWebDialogDelegate::GetWebUIMessageHandlers( |
[email protected] | 36e1217 | 2011-02-08 23:46:02 | [diff] [blame] | 565 | std::vector<WebUIMessageHandler*>* handlers) const { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 566 | handlers->push_back(flow_handler_); |
| 567 | // We don't own flow_handler_ anymore, but it sticks around until at |
| 568 | // least right after OnDialogClosed() is called (and this object is |
| 569 | // destroyed). |
| 570 | owns_flow_handler_ = false; |
| 571 | } |
| 572 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 573 | void CloudPrintWebDialogDelegate::GetDialogSize(gfx::Size* size) const { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 574 | size->set_width(params_.width); |
| 575 | size->set_height(params_.height); |
| 576 | } |
| 577 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 578 | std::string CloudPrintWebDialogDelegate::GetDialogArgs() const { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 579 | return params_.json_input; |
| 580 | } |
| 581 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 582 | void CloudPrintWebDialogDelegate::OnDialogClosed( |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 583 | const std::string& json_retval) { |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 584 | // Get the final dialog size and store it. |
| 585 | flow_handler_->StoreDialogClientSize(); |
[email protected] | 6ddda23 | 2011-04-22 15:41:47 | [diff] [blame] | 586 | |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 587 | // If we're modal we can show the dialog with no browser. |
| 588 | // End the keep-alive so that Chrome can exit. |
[email protected] | b5b79d7 | 2012-05-24 19:42:28 | [diff] [blame] | 589 | if (!modal_parent_ && keep_alive_when_non_modal_) |
[email protected] | 313fce1 | 2013-01-30 17:09:04 | [diff] [blame] | 590 | chrome::EndKeepAlive(); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 591 | delete this; |
| 592 | } |
| 593 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 594 | void CloudPrintWebDialogDelegate::OnCloseContents(WebContents* source, |
| 595 | bool* out_close_dialog) { |
[email protected] | 18137e0 | 2010-05-25 21:10:35 | [diff] [blame] | 596 | if (out_close_dialog) |
| 597 | *out_close_dialog = true; |
| 598 | } |
| 599 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 600 | bool CloudPrintWebDialogDelegate::ShouldShowDialogTitle() const { |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 601 | return false; |
| 602 | } |
| 603 | |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 604 | bool CloudPrintWebDialogDelegate::HandleContextMenu( |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 605 | const content::ContextMenuParams& params) { |
[email protected] | 3447821 | 2011-04-19 01:35:46 | [diff] [blame] | 606 | return true; |
| 607 | } |
| 608 | |
[email protected] | a911c4e | 2012-10-12 00:37:57 | [diff] [blame] | 609 | bool CloudPrintWebDialogDelegate::HandleOpenURLFromTab( |
| 610 | content::WebContents* source, |
| 611 | const content::OpenURLParams& params, |
| 612 | content::WebContents** out_new_contents) { |
| 613 | return flow_handler_->NavigationToURLDidCloseDialog(params.url); |
| 614 | } |
| 615 | |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 616 | // Called from the UI thread, starts up the dialog. |
[email protected] | b5b79d7 | 2012-05-24 19:42:28 | [diff] [blame] | 617 | void CreateDialogImpl(content::BrowserContext* browser_context, |
| 618 | gfx::NativeWindow modal_parent, |
[email protected] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 619 | const base::RefCountedMemory* data, |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 620 | const string16& print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 621 | const string16& print_ticket, |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 622 | const std::string& file_type, |
[email protected] | 4cd4902 | 2012-01-19 20:37:37 | [diff] [blame] | 623 | bool close_after_signin, |
| 624 | const base::Closure& callback) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 625 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 5835871a | 2012-04-25 21:56:55 | [diff] [blame] | 626 | WebDialogDelegate* dialog_delegate = |
| 627 | new internal_cloud_print_helpers::CloudPrintWebDialogDelegate( |
[email protected] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 628 | browser_context, modal_parent, data, std::string(), print_job_title, |
| 629 | print_ticket, file_type, close_after_signin, callback); |
[email protected] | 88bfd25b | 2012-06-22 06:28:33 | [diff] [blame] | 630 | #if defined(OS_WIN) |
| 631 | gfx::NativeWindow window = |
| 632 | #endif |
[email protected] | 87586fb7 | 2012-07-02 13:15:02 | [diff] [blame] | 633 | chrome::ShowWebDialog(modal_parent, |
| 634 | Profile::FromBrowserContext(browser_context), |
| 635 | dialog_delegate); |
[email protected] | 88bfd25b | 2012-06-22 06:28:33 | [diff] [blame] | 636 | #if defined(OS_WIN) |
[email protected] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 637 | if (window) { |
[email protected] | 88bfd25b | 2012-06-22 06:28:33 | [diff] [blame] | 638 | HWND dialog_handle; |
| 639 | #if defined(USE_AURA) |
| 640 | dialog_handle = window->GetRootWindow()->GetAcceleratedWidget(); |
| 641 | #else |
| 642 | dialog_handle = window; |
| 643 | #endif |
| 644 | if (::GetForegroundWindow() != dialog_handle) { |
| 645 | ui::ForegroundHelper::SetForeground(dialog_handle); |
| 646 | } |
| 647 | } |
| 648 | #endif |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 649 | } |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 650 | |
[email protected] | b5b79d7 | 2012-05-24 19:42:28 | [diff] [blame] | 651 | void CreateDialogSigninImpl(content::BrowserContext* browser_context, |
| 652 | gfx::NativeWindow modal_parent, |
| 653 | const base::Closure& callback) { |
[email protected] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 654 | CreateDialogImpl(browser_context, modal_parent, NULL, string16(), |
| 655 | string16(), std::string(), true, callback); |
[email protected] | 4cd4902 | 2012-01-19 20:37:37 | [diff] [blame] | 656 | } |
| 657 | |
[email protected] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 658 | void CreateDialogForFileImpl(content::BrowserContext* browser_context, |
| 659 | gfx::NativeWindow modal_parent, |
| 660 | const base::FilePath& path_to_file, |
| 661 | const string16& print_job_title, |
| 662 | const string16& print_ticket, |
| 663 | const std::string& file_type, |
| 664 | bool delete_on_close) { |
| 665 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 666 | scoped_refptr<base::RefCountedMemory> data; |
| 667 | int64 file_size = 0; |
| 668 | if (file_util::GetFileSize(path_to_file, &file_size) && file_size != 0) { |
| 669 | std::string file_data; |
| 670 | if (file_size < kuint32max) { |
| 671 | file_data.reserve(static_cast<unsigned int>(file_size)); |
| 672 | } else { |
| 673 | DLOG(WARNING) << " print data file too large to reserve space"; |
| 674 | } |
| 675 | if (file_util::ReadFileToString(path_to_file, &file_data)) { |
| 676 | data = base::RefCountedString::TakeString(&file_data); |
| 677 | } |
| 678 | } |
| 679 | // Proceed even for empty data to simplify testing. |
| 680 | BrowserThread::PostTask( |
| 681 | BrowserThread::UI, FROM_HERE, |
| 682 | base::Bind(&print_dialog_cloud::CreatePrintDialogForBytes, |
| 683 | browser_context, modal_parent, data, print_job_title, |
| 684 | print_ticket, file_type)); |
| 685 | if (delete_on_close) |
| 686 | file_util::Delete(path_to_file, false); |
[email protected] | 6ddda23 | 2011-04-22 15:41:47 | [diff] [blame] | 687 | } |
| 688 | |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 689 | } // namespace internal_cloud_print_helpers |
| 690 | |
| 691 | namespace print_dialog_cloud { |
| 692 | |
[email protected] | 681958c | 2013-02-21 13:48:14 | [diff] [blame] | 693 | void RegisterUserPrefs(PrefRegistrySyncable* registry) { |
| 694 | registry->RegisterIntegerPref( |
| 695 | prefs::kCloudPrintDialogWidth, |
| 696 | kDefaultWidth, |
| 697 | PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 698 | registry->RegisterIntegerPref( |
| 699 | prefs::kCloudPrintDialogHeight, |
| 700 | kDefaultHeight, |
| 701 | PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 702 | } |
| 703 | |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 704 | // Called on the FILE or UI thread. This is the main entry point into creating |
| 705 | // the dialog. |
| 706 | |
[email protected] | b5b79d7 | 2012-05-24 19:42:28 | [diff] [blame] | 707 | void CreatePrintDialogForFile(content::BrowserContext* browser_context, |
| 708 | gfx::NativeWindow modal_parent, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 709 | const base::FilePath& path_to_file, |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 710 | const string16& print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 711 | const string16& print_ticket, |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 712 | const std::string& file_type, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 713 | bool delete_on_close) { |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 714 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE) || |
| 715 | BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 716 | BrowserThread::PostTask( |
[email protected] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 717 | BrowserThread::FILE, FROM_HERE, |
| 718 | base::Bind(&internal_cloud_print_helpers::CreateDialogForFileImpl, |
[email protected] | b5b79d7 | 2012-05-24 19:42:28 | [diff] [blame] | 719 | browser_context, modal_parent, path_to_file, print_job_title, |
| 720 | print_ticket, file_type, delete_on_close)); |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 721 | } |
| 722 | |
[email protected] | b5b79d7 | 2012-05-24 19:42:28 | [diff] [blame] | 723 | void CreateCloudPrintSigninDialog(content::BrowserContext* browser_context, |
| 724 | gfx::NativeWindow modal_parent, |
| 725 | const base::Closure& callback) { |
[email protected] | 4cd4902 | 2012-01-19 20:37:37 | [diff] [blame] | 726 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 727 | |
| 728 | BrowserThread::PostTask( |
| 729 | BrowserThread::UI, FROM_HERE, |
| 730 | base::Bind(&internal_cloud_print_helpers::CreateDialogSigninImpl, |
[email protected] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 731 | browser_context, modal_parent, callback)); |
[email protected] | 4cd4902 | 2012-01-19 20:37:37 | [diff] [blame] | 732 | } |
| 733 | |
[email protected] | b5b79d7 | 2012-05-24 19:42:28 | [diff] [blame] | 734 | void CreatePrintDialogForBytes(content::BrowserContext* browser_context, |
| 735 | gfx::NativeWindow modal_parent, |
[email protected] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 736 | const base::RefCountedMemory* data, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 737 | const string16& print_job_title, |
| 738 | const string16& print_ticket, |
[email protected] | b5b79d7 | 2012-05-24 19:42:28 | [diff] [blame] | 739 | const std::string& file_type) { |
[email protected] | a9723e1 | 2013-03-05 04:02:45 | [diff] [blame] | 740 | internal_cloud_print_helpers::CreateDialogImpl(browser_context, modal_parent, |
| 741 | data, print_job_title, |
| 742 | print_ticket, file_type, false, |
| 743 | base::Closure()); |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 744 | } |
| 745 | |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 746 | bool CreatePrintDialogFromCommandLine(const CommandLine& command_line) { |
[email protected] | 87ab41e7 | 2012-01-04 18:45:11 | [diff] [blame] | 747 | DCHECK(command_line.HasSwitch(switches::kCloudPrintFile)); |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 748 | if (!command_line.GetSwitchValuePath(switches::kCloudPrintFile).empty()) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 749 | base::FilePath cloud_print_file; |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 750 | cloud_print_file = |
| 751 | command_line.GetSwitchValuePath(switches::kCloudPrintFile); |
| 752 | if (!cloud_print_file.empty()) { |
| 753 | string16 print_job_title; |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 754 | string16 print_job_print_ticket; |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 755 | if (command_line.HasSwitch(switches::kCloudPrintJobTitle)) { |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 756 | print_job_title = |
| 757 | internal_cloud_print_helpers::GetSwitchValueString16( |
| 758 | command_line, switches::kCloudPrintJobTitle); |
| 759 | } |
| 760 | if (command_line.HasSwitch(switches::kCloudPrintPrintTicket)) { |
| 761 | print_job_print_ticket = |
| 762 | internal_cloud_print_helpers::GetSwitchValueString16( |
| 763 | command_line, switches::kCloudPrintPrintTicket); |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 764 | } |
| 765 | std::string file_type = "application/pdf"; |
| 766 | if (command_line.HasSwitch(switches::kCloudPrintFileType)) { |
| 767 | file_type = command_line.GetSwitchValueASCII( |
| 768 | switches::kCloudPrintFileType); |
| 769 | } |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 770 | |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 771 | bool delete_on_close = CommandLine::ForCurrentProcess()->HasSwitch( |
| 772 | switches::kCloudPrintDeleteFile); |
| 773 | |
[email protected] | b5b79d7 | 2012-05-24 19:42:28 | [diff] [blame] | 774 | print_dialog_cloud::CreatePrintDialogForFile( |
| 775 | ProfileManager::GetDefaultProfile(), |
| 776 | NULL, |
| 777 | cloud_print_file, |
| 778 | print_job_title, |
| 779 | print_job_print_ticket, |
| 780 | file_type, |
| 781 | delete_on_close); |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 782 | return true; |
| 783 | } |
| 784 | } |
| 785 | return false; |
| 786 | } |
| 787 | |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 788 | } // end namespace |