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