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