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