[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/printing/print_dialog_cloud.h" |
| 6 | #include "chrome/browser/printing/print_dialog_cloud_internal.h" |
| 7 | |
| 8 | #include "app/l10n_util.h" |
| 9 | #include "base/base64.h" |
| 10 | #include "base/file_util.h" |
| 11 | #include "base/json/json_reader.h" |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 12 | #include "base/values.h" |
| 13 | #include "chrome/browser/browser_list.h" |
[email protected] | 017a7a11 | 2010-10-12 16:38:27 | [diff] [blame] | 14 | #include "chrome/browser/browser_thread.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 15 | #include "chrome/browser/debugger/devtools_manager.h" |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 16 | #include "chrome/browser/dom_ui/dom_ui.h" |
| 17 | #include "chrome/browser/dom_ui/dom_ui_util.h" |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 18 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | 2283eead | 2010-09-29 23:17:30 | [diff] [blame] | 19 | #include "chrome/browser/printing/cloud_print/cloud_print_url.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 20 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 21 | #include "chrome/browser/renderer_host/render_view_host.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 22 | #include "chrome/browser/tab_contents/tab_contents.h" |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 23 | #include "chrome/browser/tab_contents/tab_contents_view.h" |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 24 | #include "chrome/common/notification_observer.h" |
| 25 | #include "chrome/common/notification_registrar.h" |
| 26 | #include "chrome/common/notification_source.h" |
[email protected] | 939856a | 2010-08-24 20:29:02 | [diff] [blame] | 27 | #include "chrome/common/notification_type.h" |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 28 | #include "chrome/common/pref_names.h" |
[email protected] | 939856a | 2010-08-24 20:29:02 | [diff] [blame] | 29 | #include "chrome/common/render_messages_params.h" |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 30 | #include "chrome/common/url_constants.h" |
[email protected] | 939856a | 2010-08-24 20:29:02 | [diff] [blame] | 31 | #include "webkit/glue/webpreferences.h" |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 32 | |
| 33 | #include "grit/generated_resources.h" |
| 34 | |
| 35 | // This module implements the UI support in Chrome for cloud printing. |
| 36 | // This means hosting a dialog containing HTML/JavaScript and using |
| 37 | // the published cloud print user interface integration APIs to get |
| 38 | // page setup settings from the dialog contents and provide the |
| 39 | // generated print PDF to the dialog contents for uploading to the |
| 40 | // cloud print service. |
| 41 | |
| 42 | // Currently, the flow between these classes is as follows: |
| 43 | |
| 44 | // PrintDialogCloud::CreatePrintDialogForPdf is called from |
| 45 | // resource_message_filter_gtk.cc once the renderer has informed the |
| 46 | // renderer host that PDF generation into the renderer host provided |
[email protected] | 032682b | 2011-01-12 22:05:02 | [diff] [blame] | 47 | // temp file has been completed. That call is on the FILE thread. |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 48 | // That, in turn, hops over to the UI thread to create an instance of |
| 49 | // PrintDialogCloud. |
| 50 | |
| 51 | // The constructor for PrintDialogCloud creates a |
| 52 | // CloudPrintHtmlDialogDelegate and asks the current active browser to |
| 53 | // show an HTML dialog using that class as the delegate. That class |
| 54 | // hands in the kCloudPrintResourcesURL as the URL to visit. That is |
| 55 | // recognized by the GetDOMUIFactoryFunction as a signal to create an |
| 56 | // ExternalHtmlDialogUI. |
| 57 | |
| 58 | // CloudPrintHtmlDialogDelegate also temporarily owns a |
| 59 | // CloudPrintFlowHandler, a class which is responsible for the actual |
| 60 | // interactions with the dialog contents, including handing in the PDF |
| 61 | // print data and getting any page setup parameters that the dialog |
| 62 | // contents provides. As part of bringing up the dialog, |
| 63 | // HtmlDialogUI::RenderViewCreated is called (an override of |
| 64 | // DOMUI::RenderViewCreated). That routine, in turn, calls the |
| 65 | // delegate's GetDOMMessageHandlers routine, at which point the |
| 66 | // ownership of the CloudPrintFlowHandler is handed over. A pointer |
| 67 | // to the flow handler is kept to facilitate communication back and |
| 68 | // forth between the two classes. |
| 69 | |
| 70 | // The DOMUI continues dialog bring-up, calling |
| 71 | // CloudPrintFlowHandler::RegisterMessages. This is where the |
| 72 | // additional object model capabilities are registered for the dialog |
| 73 | // contents to use. It is also at this time that capabilities for the |
| 74 | // dialog contents are adjusted to allow the dialog contents to close |
| 75 | // the window. In addition, the pending URL is redirected to the |
| 76 | // actual cloud print service URL. The flow controller also registers |
| 77 | // for notification of when the dialog contents finish loading, which |
| 78 | // is currently used to send the PDF data to the dialog contents. |
| 79 | |
| 80 | // In order to send the PDF data to the dialog contents, the flow |
| 81 | // handler uses a CloudPrintDataSender. It creates one, letting it |
| 82 | // know the name of the temporary file containing the PDF data, and |
| 83 | // posts the task of reading the file |
| 84 | // (CloudPrintDataSender::ReadPrintDataFile) to the file thread. That |
| 85 | // routine reads in the file, and then hops over to the IO thread to |
| 86 | // send that data to the dialog contents. |
| 87 | |
| 88 | // When the dialog contents are finished (by either being cancelled or |
| 89 | // hitting the print button), the delegate is notified, and responds |
| 90 | // that the dialog should be closed, at which point things are torn |
| 91 | // down and released. |
| 92 | |
| 93 | // TODO(scottbyer): |
| 94 | // http://code.google.com/p/chromium/issues/detail?id=44093 The |
| 95 | // high-level flow (where the PDF data is generated before even |
| 96 | // bringing up the dialog) isn't what we want. |
| 97 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 98 | namespace internal_cloud_print_helpers { |
| 99 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 100 | bool GetRealOrInt(const DictionaryValue& dictionary, |
[email protected] | a65175d | 2010-08-17 04:00:57 | [diff] [blame] | 101 | const std::string& path, |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 102 | double* out_value) { |
| 103 | if (!dictionary.GetReal(path, out_value)) { |
| 104 | int int_value = 0; |
| 105 | if (!dictionary.GetInteger(path, &int_value)) |
| 106 | return false; |
| 107 | *out_value = int_value; |
| 108 | } |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | // From the JSON parsed value, get the entries for the page setup |
| 113 | // parameters. |
| 114 | bool GetPageSetupParameters(const std::string& json, |
| 115 | ViewMsg_Print_Params& parameters) { |
| 116 | scoped_ptr<Value> parsed_value(base::JSONReader::Read(json, false)); |
| 117 | DLOG_IF(ERROR, (!parsed_value.get() || |
| 118 | !parsed_value->IsType(Value::TYPE_DICTIONARY))) |
| 119 | << "PageSetup call didn't have expected contents"; |
| 120 | if (!parsed_value.get() || !parsed_value->IsType(Value::TYPE_DICTIONARY)) |
| 121 | return false; |
| 122 | |
| 123 | bool result = true; |
| 124 | DictionaryValue* params = static_cast<DictionaryValue*>(parsed_value.get()); |
[email protected] | a65175d | 2010-08-17 04:00:57 | [diff] [blame] | 125 | result &= GetRealOrInt(*params, "dpi", ¶meters.dpi); |
| 126 | result &= GetRealOrInt(*params, "min_shrink", ¶meters.min_shrink); |
| 127 | result &= GetRealOrInt(*params, "max_shrink", ¶meters.max_shrink); |
| 128 | result &= params->GetBoolean("selection_only", ¶meters.selection_only); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 129 | return result; |
| 130 | } |
| 131 | |
| 132 | void CloudPrintDataSenderHelper::CallJavascriptFunction( |
| 133 | const std::wstring& function_name) { |
| 134 | dom_ui_->CallJavascriptFunction(function_name); |
| 135 | } |
| 136 | |
| 137 | void CloudPrintDataSenderHelper::CallJavascriptFunction( |
| 138 | const std::wstring& function_name, const Value& arg) { |
| 139 | dom_ui_->CallJavascriptFunction(function_name, arg); |
| 140 | } |
| 141 | |
| 142 | void CloudPrintDataSenderHelper::CallJavascriptFunction( |
| 143 | const std::wstring& function_name, const Value& arg1, const Value& arg2) { |
| 144 | dom_ui_->CallJavascriptFunction(function_name, arg1, arg2); |
| 145 | } |
| 146 | |
| 147 | // Clears out the pointer we're using to communicate. Either routine is |
| 148 | // potentially expensive enough that stopping whatever is in progress |
| 149 | // is worth it. |
| 150 | void CloudPrintDataSender::CancelPrintDataFile() { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame^] | 151 | base::AutoLock lock(lock_); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 152 | // We don't own helper, it was passed in to us, so no need to |
| 153 | // delete, just let it go. |
| 154 | helper_ = NULL; |
| 155 | } |
| 156 | |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 157 | CloudPrintDataSender::CloudPrintDataSender(CloudPrintDataSenderHelper* helper, |
| 158 | const string16& print_job_title) |
| 159 | : helper_(helper), |
| 160 | print_job_title_(print_job_title) { |
| 161 | } |
| 162 | |
| 163 | CloudPrintDataSender::~CloudPrintDataSender() {} |
| 164 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 165 | // Grab the raw PDF file contents and massage them into shape for |
| 166 | // sending to the dialog contents (and up to the cloud print server) |
| 167 | // by encoding it and prefixing it with the appropriate mime type. |
| 168 | // Once that is done, kick off the next part of the task on the IO |
| 169 | // thread. |
| 170 | void CloudPrintDataSender::ReadPrintDataFile(const FilePath& path_to_pdf) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 171 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 172 | int64 file_size = 0; |
| 173 | if (file_util::GetFileSize(path_to_pdf, &file_size) && file_size != 0) { |
| 174 | std::string file_data; |
| 175 | if (file_size < kuint32max) { |
| 176 | file_data.reserve(static_cast<unsigned int>(file_size)); |
| 177 | } else { |
| 178 | DLOG(WARNING) << " print data file too large to reserve space"; |
| 179 | } |
| 180 | if (helper_ && file_util::ReadFileToString(path_to_pdf, &file_data)) { |
| 181 | std::string base64_data; |
| 182 | base::Base64Encode(file_data, &base64_data); |
| 183 | std::string header("data:application/pdf;base64,"); |
| 184 | base64_data.insert(0, header); |
| 185 | scoped_ptr<StringValue> new_data(new StringValue(base64_data)); |
| 186 | print_data_.swap(new_data); |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 187 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 188 | NewRunnableMethod( |
| 189 | this, |
| 190 | &CloudPrintDataSender::SendPrintDataFile)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // We have the data in hand that needs to be pushed into the dialog |
| 196 | // contents; do so from the IO thread. |
| 197 | |
| 198 | // TODO(scottbyer): If the print data ends up being larger than the |
| 199 | // upload limit (currently 10MB), what we need to do is upload that |
| 200 | // large data to google docs and set the URL in the printing |
| 201 | // JavaScript to that location, and make sure it gets deleted when not |
| 202 | // needed. - 4/1/2010 |
| 203 | void CloudPrintDataSender::SendPrintDataFile() { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 204 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame^] | 205 | base::AutoLock lock(lock_); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 206 | if (helper_ && print_data_.get()) { |
[email protected] | 9848c7e | 2010-06-03 16:06:56 | [diff] [blame] | 207 | StringValue title(print_job_title_); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 208 | |
| 209 | // Send the print data to the dialog contents. The JavaScript |
| 210 | // function is a preliminary API for prototyping purposes and is |
| 211 | // subject to change. |
| 212 | const_cast<CloudPrintDataSenderHelper*>(helper_)->CallJavascriptFunction( |
| 213 | L"printApp._printDataUrl", *print_data_, title); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 218 | CloudPrintFlowHandler::CloudPrintFlowHandler(const FilePath& path_to_pdf, |
| 219 | const string16& print_job_title) |
| 220 | : path_to_pdf_(path_to_pdf), |
| 221 | print_job_title_(print_job_title) { |
| 222 | } |
| 223 | |
| 224 | CloudPrintFlowHandler::~CloudPrintFlowHandler() { |
| 225 | // This will also cancel any task in flight. |
| 226 | CancelAnyRunningTask(); |
| 227 | } |
| 228 | |
| 229 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 230 | void CloudPrintFlowHandler::SetDialogDelegate( |
| 231 | CloudPrintHtmlDialogDelegate* delegate) { |
| 232 | // Even if setting a new dom_ui, it means any previous task needs |
| 233 | // to be cancelled, it's now invalid. |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 234 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 235 | CancelAnyRunningTask(); |
| 236 | dialog_delegate_ = delegate; |
| 237 | } |
| 238 | |
| 239 | // Cancels any print data sender we have in flight and removes our |
| 240 | // reference to it, so when the task that is calling it finishes and |
| 241 | // removes it's reference, it goes away. |
| 242 | void CloudPrintFlowHandler::CancelAnyRunningTask() { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 243 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 244 | if (print_data_sender_.get()) { |
| 245 | print_data_sender_->CancelPrintDataFile(); |
| 246 | print_data_sender_ = NULL; |
| 247 | } |
| 248 | } |
| 249 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 250 | void CloudPrintFlowHandler::RegisterMessages() { |
| 251 | if (!dom_ui_) |
| 252 | return; |
| 253 | |
| 254 | // TODO(scottbyer) - This is where we will register messages for the |
| 255 | // UI JS to use. Needed: Call to update page setup parameters. |
| 256 | dom_ui_->RegisterMessageCallback( |
| 257 | "ShowDebugger", |
| 258 | NewCallback(this, &CloudPrintFlowHandler::HandleShowDebugger)); |
| 259 | dom_ui_->RegisterMessageCallback( |
| 260 | "SendPrintData", |
| 261 | NewCallback(this, &CloudPrintFlowHandler::HandleSendPrintData)); |
| 262 | dom_ui_->RegisterMessageCallback( |
| 263 | "SetPageParameters", |
| 264 | NewCallback(this, &CloudPrintFlowHandler::HandleSetPageParameters)); |
| 265 | |
| 266 | if (dom_ui_->tab_contents()) { |
| 267 | // Also, take the opportunity to set some (minimal) additional |
| 268 | // script permissions required for the web UI. |
| 269 | |
| 270 | // TODO(scottbyer): learn how to make sure we're talking to the |
| 271 | // right web site first. |
| 272 | RenderViewHost* rvh = dom_ui_->tab_contents()->render_view_host(); |
| 273 | if (rvh && rvh->delegate()) { |
| 274 | WebPreferences webkit_prefs = rvh->delegate()->GetWebkitPrefs(); |
| 275 | webkit_prefs.allow_scripts_to_close_windows = true; |
| 276 | rvh->UpdateWebPreferences(webkit_prefs); |
| 277 | } |
| 278 | |
| 279 | // Register for appropriate notifications, and re-direct the URL |
| 280 | // to the real server URL, now that we've gotten an HTML dialog |
| 281 | // going. |
| 282 | NavigationController* controller = &dom_ui_->tab_contents()->controller(); |
| 283 | NavigationEntry* pending_entry = controller->pending_entry(); |
| 284 | if (pending_entry) |
[email protected] | 2283eead | 2010-09-29 23:17:30 | [diff] [blame] | 285 | pending_entry->set_url(CloudPrintURL( |
[email protected] | 4baf1c4 | 2010-05-18 18:45:25 | [diff] [blame] | 286 | dom_ui_->GetProfile()).GetCloudPrintServiceDialogURL()); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 287 | registrar_.Add(this, NotificationType::LOAD_STOP, |
| 288 | Source<NavigationController>(controller)); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | void CloudPrintFlowHandler::Observe(NotificationType type, |
| 293 | const NotificationSource& source, |
| 294 | const NotificationDetails& details) { |
| 295 | if (type == NotificationType::LOAD_STOP) { |
| 296 | // Choose one or the other. If you need to debug, bring up the |
| 297 | // debugger. You can then use the various chrome.send() |
| 298 | // registrations above to kick of the various function calls, |
| 299 | // including chrome.send("SendPrintData") in the javaScript |
| 300 | // console and watch things happen with: |
| 301 | // HandleShowDebugger(NULL); |
| 302 | HandleSendPrintData(NULL); |
| 303 | } |
| 304 | } |
| 305 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 306 | void CloudPrintFlowHandler::HandleShowDebugger(const ListValue* args) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 307 | ShowDebugger(); |
| 308 | } |
| 309 | |
| 310 | void CloudPrintFlowHandler::ShowDebugger() { |
| 311 | if (dom_ui_) { |
| 312 | RenderViewHost* rvh = dom_ui_->tab_contents()->render_view_host(); |
| 313 | if (rvh) |
| 314 | DevToolsManager::GetInstance()->OpenDevToolsWindow(rvh); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | scoped_refptr<CloudPrintDataSender> |
| 319 | CloudPrintFlowHandler::CreateCloudPrintDataSender() { |
| 320 | DCHECK(dom_ui_); |
| 321 | print_data_helper_.reset(new CloudPrintDataSenderHelper(dom_ui_)); |
[email protected] | 9848c7e | 2010-06-03 16:06:56 | [diff] [blame] | 322 | return new CloudPrintDataSender(print_data_helper_.get(), print_job_title_); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 323 | } |
| 324 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 325 | void CloudPrintFlowHandler::HandleSendPrintData(const ListValue* args) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 326 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 327 | // This will cancel any ReadPrintDataFile() or SendPrintDataFile() |
| 328 | // requests in flight (this is anticipation of when setting page |
| 329 | // setup parameters becomes asynchronous and may be set while some |
| 330 | // data is in flight). Then we can clear out the print data. |
| 331 | CancelAnyRunningTask(); |
| 332 | if (dom_ui_) { |
| 333 | print_data_sender_ = CreateCloudPrintDataSender(); |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 334 | BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 335 | NewRunnableMethod( |
| 336 | print_data_sender_.get(), |
| 337 | &CloudPrintDataSender::ReadPrintDataFile, |
| 338 | path_to_pdf_)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 342 | void CloudPrintFlowHandler::HandleSetPageParameters(const ListValue* args) { |
| 343 | std::string json(dom_ui_util::GetJsonResponseFromFirstArgumentInList(args)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 344 | if (json.empty()) |
| 345 | return; |
| 346 | |
| 347 | // These are backstop default values - 72 dpi to match the screen, |
| 348 | // 8.5x11 inch paper with margins subtracted (1/4 inch top, left, |
| 349 | // right and 0.56 bottom), and the min page shrink and max page |
| 350 | // shrink values appear all over the place with no explanation. |
| 351 | |
| 352 | // TODO(scottbyer): Get a Linux/ChromeOS edge for PrintSettings |
| 353 | // working so that we can get the default values from there. Fix up |
| 354 | // PrintWebViewHelper to do the same. |
| 355 | const int kDPI = 72; |
| 356 | const int kWidth = static_cast<int>((8.5-0.25-0.25)*kDPI); |
| 357 | const int kHeight = static_cast<int>((11-0.25-0.56)*kDPI); |
| 358 | const double kMinPageShrink = 1.25; |
| 359 | const double kMaxPageShrink = 2.0; |
| 360 | |
| 361 | ViewMsg_Print_Params default_settings; |
| 362 | default_settings.printable_size = gfx::Size(kWidth, kHeight); |
| 363 | default_settings.dpi = kDPI; |
| 364 | default_settings.min_shrink = kMinPageShrink; |
| 365 | default_settings.max_shrink = kMaxPageShrink; |
| 366 | default_settings.desired_dpi = kDPI; |
| 367 | default_settings.document_cookie = 0; |
| 368 | default_settings.selection_only = false; |
| 369 | |
| 370 | if (!GetPageSetupParameters(json, default_settings)) { |
| 371 | NOTREACHED(); |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | // TODO(scottbyer) - Here is where we would kick the originating |
| 376 | // renderer thread with these new parameters in order to get it to |
| 377 | // re-generate the PDF and hand it back to us. window.print() is |
| 378 | // currently synchronous, so there's a lot of work to do to get to |
| 379 | // that point. |
| 380 | } |
| 381 | |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 382 | void CloudPrintFlowHandler::StoreDialogClientSize() const { |
| 383 | if (dom_ui_ && dom_ui_->tab_contents() && dom_ui_->tab_contents()->view()) { |
| 384 | gfx::Size size = dom_ui_->tab_contents()->view()->GetContainerSize(); |
| 385 | dom_ui_->GetProfile()->GetPrefs()->SetInteger( |
| 386 | prefs::kCloudPrintDialogWidth, size.width()); |
| 387 | dom_ui_->GetProfile()->GetPrefs()->SetInteger( |
| 388 | prefs::kCloudPrintDialogHeight, size.height()); |
| 389 | } |
| 390 | } |
| 391 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 392 | CloudPrintHtmlDialogDelegate::CloudPrintHtmlDialogDelegate( |
| 393 | const FilePath& path_to_pdf, |
| 394 | int width, int height, |
[email protected] | 9848c7e | 2010-06-03 16:06:56 | [diff] [blame] | 395 | const std::string& json_arguments, |
| 396 | const string16& print_job_title) |
| 397 | : flow_handler_(new CloudPrintFlowHandler(path_to_pdf, print_job_title)), |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 398 | owns_flow_handler_(true) { |
| 399 | Init(width, height, json_arguments); |
| 400 | } |
| 401 | |
| 402 | CloudPrintHtmlDialogDelegate::CloudPrintHtmlDialogDelegate( |
| 403 | CloudPrintFlowHandler* flow_handler, |
| 404 | int width, int height, |
| 405 | const std::string& json_arguments) |
| 406 | : flow_handler_(flow_handler), |
[email protected] | 18137e0 | 2010-05-25 21:10:35 | [diff] [blame] | 407 | owns_flow_handler_(true) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 408 | Init(width, height, json_arguments); |
| 409 | } |
| 410 | |
| 411 | void CloudPrintHtmlDialogDelegate::Init( |
[email protected] | 9848c7e | 2010-06-03 16:06:56 | [diff] [blame] | 412 | int width, int height, const std::string& json_arguments) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 413 | // This information is needed to show the dialog HTML content. |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 414 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 415 | std::string cloud_print_url(chrome::kCloudPrintResourcesURL); |
| 416 | params_.url = GURL(cloud_print_url); |
| 417 | params_.height = height; |
| 418 | params_.width = width; |
| 419 | params_.json_input = json_arguments; |
| 420 | |
| 421 | flow_handler_->SetDialogDelegate(this); |
| 422 | } |
| 423 | |
| 424 | CloudPrintHtmlDialogDelegate::~CloudPrintHtmlDialogDelegate() { |
| 425 | // If the flow_handler_ is about to outlive us because we don't own |
| 426 | // it anymore, we need to have it remove it's reference to us. |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 427 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 428 | flow_handler_->SetDialogDelegate(NULL); |
| 429 | if (owns_flow_handler_) { |
| 430 | delete flow_handler_; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | bool CloudPrintHtmlDialogDelegate::IsDialogModal() const { |
| 435 | return true; |
| 436 | } |
| 437 | |
| 438 | std::wstring CloudPrintHtmlDialogDelegate::GetDialogTitle() const { |
[email protected] | be559e44 | 2010-11-02 20:37:32 | [diff] [blame] | 439 | return std::wstring(); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | GURL CloudPrintHtmlDialogDelegate::GetDialogContentURL() const { |
| 443 | return params_.url; |
| 444 | } |
| 445 | |
| 446 | void CloudPrintHtmlDialogDelegate::GetDOMMessageHandlers( |
| 447 | std::vector<DOMMessageHandler*>* handlers) const { |
| 448 | handlers->push_back(flow_handler_); |
| 449 | // We don't own flow_handler_ anymore, but it sticks around until at |
| 450 | // least right after OnDialogClosed() is called (and this object is |
| 451 | // destroyed). |
| 452 | owns_flow_handler_ = false; |
| 453 | } |
| 454 | |
| 455 | void CloudPrintHtmlDialogDelegate::GetDialogSize(gfx::Size* size) const { |
| 456 | size->set_width(params_.width); |
| 457 | size->set_height(params_.height); |
| 458 | } |
| 459 | |
| 460 | std::string CloudPrintHtmlDialogDelegate::GetDialogArgs() const { |
| 461 | return params_.json_input; |
| 462 | } |
| 463 | |
| 464 | void CloudPrintHtmlDialogDelegate::OnDialogClosed( |
| 465 | const std::string& json_retval) { |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 466 | // Get the final dialog size and store it. |
| 467 | flow_handler_->StoreDialogClientSize(); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 468 | delete this; |
| 469 | } |
| 470 | |
[email protected] | 18137e0 | 2010-05-25 21:10:35 | [diff] [blame] | 471 | void CloudPrintHtmlDialogDelegate::OnCloseContents(TabContents* source, |
| 472 | bool* out_close_dialog) { |
| 473 | if (out_close_dialog) |
| 474 | *out_close_dialog = true; |
| 475 | } |
| 476 | |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 477 | bool CloudPrintHtmlDialogDelegate::ShouldShowDialogTitle() const { |
| 478 | return false; |
| 479 | } |
| 480 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 481 | } // end of namespace internal_cloud_print_helpers |
| 482 | |
| 483 | // static, called on the IO thread. This is the main entry point into |
| 484 | // creating the dialog. |
| 485 | |
| 486 | // TODO(scottbyer): The signature here will need to change as the |
| 487 | // workflow through the printing code changes to allow for dynamically |
| 488 | // changing page setup parameters while the dialog is active. |
| 489 | void PrintDialogCloud::CreatePrintDialogForPdf(const FilePath& path_to_pdf) { |
[email protected] | 032682b | 2011-01-12 22:05:02 | [diff] [blame] | 490 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 491 | |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 492 | BrowserThread::PostTask( |
| 493 | BrowserThread::UI, FROM_HERE, |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 494 | NewRunnableFunction(&PrintDialogCloud::CreateDialogImpl, path_to_pdf)); |
| 495 | } |
| 496 | |
| 497 | // static, called from the UI thread. |
| 498 | void PrintDialogCloud::CreateDialogImpl(const FilePath& path_to_pdf) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 499 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 500 | new PrintDialogCloud(path_to_pdf); |
| 501 | } |
| 502 | |
| 503 | // Initialize the print dialog. Called on the UI thread. |
| 504 | PrintDialogCloud::PrintDialogCloud(const FilePath& path_to_pdf) |
| 505 | : browser_(BrowserList::GetLastActive()) { |
| 506 | |
| 507 | // TODO(scottbyer): Verify GAIA login valid, execute GAIA login if not (should |
| 508 | // be distilled out of bookmark sync.) |
[email protected] | 9848c7e | 2010-06-03 16:06:56 | [diff] [blame] | 509 | string16 print_job_title; |
| 510 | if (browser_ && browser_->GetSelectedTabContents()) |
| 511 | print_job_title = browser_->GetSelectedTabContents()->GetTitle(); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 512 | |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 513 | const int kDefaultWidth = 497; |
| 514 | const int kDefaultHeight = 332; |
| 515 | |
| 516 | PrefService* pref_service = browser_->GetProfile()->GetPrefs(); |
| 517 | DCHECK(pref_service); |
| 518 | if (!pref_service->FindPreference(prefs::kCloudPrintDialogWidth)) { |
| 519 | pref_service->RegisterIntegerPref(prefs::kCloudPrintDialogWidth, |
| 520 | kDefaultWidth); |
| 521 | } |
| 522 | if (!pref_service->FindPreference(prefs::kCloudPrintDialogHeight)) { |
| 523 | pref_service->RegisterIntegerPref(prefs::kCloudPrintDialogHeight, |
| 524 | kDefaultHeight); |
| 525 | } |
| 526 | |
| 527 | int width = pref_service->GetInteger(prefs::kCloudPrintDialogWidth); |
| 528 | int height = pref_service->GetInteger(prefs::kCloudPrintDialogHeight); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 529 | HtmlDialogUIDelegate* dialog_delegate = |
| 530 | new internal_cloud_print_helpers::CloudPrintHtmlDialogDelegate( |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 531 | path_to_pdf, width, height, std::string(), print_job_title); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 532 | browser_->BrowserShowHtmlDialog(dialog_delegate, NULL); |
| 533 | } |
| 534 | |
| 535 | PrintDialogCloud::~PrintDialogCloud() { |
| 536 | } |