[email protected] | 80a8fad | 2011-01-29 04:02:38 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/printing/print_dialog_cloud.h" |
| 6 | #include "chrome/browser/printing/print_dialog_cloud_internal.h" |
| 7 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 8 | #include "base/base64.h" |
[email protected] | ba4fc24 | 2011-10-04 18:56:56 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/bind_helpers.h" |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 11 | #include "base/command_line.h" |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 12 | #include "base/file_util.h" |
| 13 | #include "base/json/json_reader.h" |
[email protected] | 4b8852a | 2011-06-10 17:24:46 | [diff] [blame] | 14 | #include "base/utf_string_conversions.h" |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 15 | #include "base/values.h" |
[email protected] | 0744cc7 | 2011-08-12 12:05:45 | [diff] [blame] | 16 | #include "chrome/browser/browser_process_impl.h" |
[email protected] | dce50276 | 2011-07-20 08:53:49 | [diff] [blame] | 17 | #include "chrome/browser/debugger/devtools_window.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] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 21 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | eb2d790 | 2011-02-02 18:19:56 | [diff] [blame] | 22 | #include "chrome/browser/ui/browser_dialogs.h" |
[email protected] | 6768ac0 | 2011-04-06 17:41:04 | [diff] [blame] | 23 | #include "chrome/browser/ui/browser_list.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] | 5f945a0e | 2011-03-01 17:47:53 | [diff] [blame] | 28 | #include "content/browser/renderer_host/render_view_host.h" |
| 29 | #include "content/browser/tab_contents/tab_contents.h" |
| 30 | #include "content/browser/tab_contents/tab_contents_view.h" |
[email protected] | 67fc039 | 2011-02-25 02:56:57 | [diff] [blame] | 31 | #include "content/browser/webui/web_ui.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame^] | 32 | #include "content/public/browser/browser_thread.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 33 | #include "content/public/browser/notification_registrar.h" |
| 34 | #include "content/public/browser/notification_source.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 35 | #include "content/public/browser/notification_types.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 36 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 939856a | 2010-08-24 20:29:02 | [diff] [blame] | 37 | #include "webkit/glue/webpreferences.h" |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 38 | |
| 39 | #include "grit/generated_resources.h" |
| 40 | |
| 41 | // This module implements the UI support in Chrome for cloud printing. |
| 42 | // This means hosting a dialog containing HTML/JavaScript and using |
| 43 | // the published cloud print user interface integration APIs to get |
| 44 | // page setup settings from the dialog contents and provide the |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 45 | // generated print data to the dialog contents for uploading to the |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 46 | // cloud print service. |
| 47 | |
| 48 | // Currently, the flow between these classes is as follows: |
| 49 | |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 50 | // PrintDialogCloud::CreatePrintDialogForFile is called from |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 51 | // resource_message_filter_gtk.cc once the renderer has informed the |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 52 | // renderer host that print data generation into the renderer host provided |
[email protected] | 032682b | 2011-01-12 22:05:02 | [diff] [blame] | 53 | // temp file has been completed. That call is on the FILE thread. |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 54 | // That, in turn, hops over to the UI thread to create an instance of |
| 55 | // PrintDialogCloud. |
| 56 | |
| 57 | // The constructor for PrintDialogCloud creates a |
| 58 | // CloudPrintHtmlDialogDelegate and asks the current active browser to |
| 59 | // show an HTML dialog using that class as the delegate. That class |
[email protected] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 60 | // hands in the kChromeUICloudPrintResourcesURL as the URL to visit. That is |
[email protected] | 80a8fad | 2011-01-29 04:02:38 | [diff] [blame] | 61 | // recognized by the GetWebUIFactoryFunction as a signal to create an |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 62 | // ExternalHtmlDialogUI. |
| 63 | |
| 64 | // CloudPrintHtmlDialogDelegate also temporarily owns a |
| 65 | // CloudPrintFlowHandler, a class which is responsible for the actual |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 66 | // interactions with the dialog contents, including handing in the |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 67 | // print data and getting any page setup parameters that the dialog |
| 68 | // contents provides. As part of bringing up the dialog, |
| 69 | // HtmlDialogUI::RenderViewCreated is called (an override of |
[email protected] | c39f9bf | 2011-02-12 00:43:55 | [diff] [blame] | 70 | // WebUI::RenderViewCreated). That routine, in turn, calls the |
[email protected] | 36e1217 | 2011-02-08 23:46:02 | [diff] [blame] | 71 | // delegate's GetWebUIMessageHandlers routine, at which point the |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 72 | // ownership of the CloudPrintFlowHandler is handed over. A pointer |
| 73 | // to the flow handler is kept to facilitate communication back and |
| 74 | // forth between the two classes. |
| 75 | |
[email protected] | c39f9bf | 2011-02-12 00:43:55 | [diff] [blame] | 76 | // The WebUI continues dialog bring-up, calling |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 77 | // CloudPrintFlowHandler::RegisterMessages. This is where the |
| 78 | // additional object model capabilities are registered for the dialog |
| 79 | // contents to use. It is also at this time that capabilities for the |
| 80 | // dialog contents are adjusted to allow the dialog contents to close |
| 81 | // the window. In addition, the pending URL is redirected to the |
| 82 | // actual cloud print service URL. The flow controller also registers |
| 83 | // for notification of when the dialog contents finish loading, which |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 84 | // is currently used to send the data to the dialog contents. |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 85 | |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 86 | // In order to send the data to the dialog contents, the flow |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 87 | // handler uses a CloudPrintDataSender. It creates one, letting it |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 88 | // know the name of the temporary file containing the data, and |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 89 | // posts the task of reading the file |
| 90 | // (CloudPrintDataSender::ReadPrintDataFile) to the file thread. That |
| 91 | // routine reads in the file, and then hops over to the IO thread to |
| 92 | // send that data to the dialog contents. |
| 93 | |
| 94 | // When the dialog contents are finished (by either being cancelled or |
| 95 | // hitting the print button), the delegate is notified, and responds |
| 96 | // that the dialog should be closed, at which point things are torn |
| 97 | // down and released. |
| 98 | |
| 99 | // TODO(scottbyer): |
| 100 | // http://code.google.com/p/chromium/issues/detail?id=44093 The |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 101 | // high-level flow (where the data is generated before even |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 102 | // bringing up the dialog) isn't what we want. |
| 103 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 104 | namespace internal_cloud_print_helpers { |
| 105 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 106 | // From the JSON parsed value, get the entries for the page setup |
| 107 | // parameters. |
| 108 | bool GetPageSetupParameters(const std::string& json, |
[email protected] | 1375e3ab | 2011-03-24 17:07:22 | [diff] [blame] | 109 | PrintMsg_Print_Params& parameters) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 110 | scoped_ptr<Value> parsed_value(base::JSONReader::Read(json, false)); |
| 111 | DLOG_IF(ERROR, (!parsed_value.get() || |
| 112 | !parsed_value->IsType(Value::TYPE_DICTIONARY))) |
| 113 | << "PageSetup call didn't have expected contents"; |
| 114 | if (!parsed_value.get() || !parsed_value->IsType(Value::TYPE_DICTIONARY)) |
| 115 | return false; |
| 116 | |
| 117 | bool result = true; |
| 118 | DictionaryValue* params = static_cast<DictionaryValue*>(parsed_value.get()); |
[email protected] | 05c7da6 | 2011-05-05 17:23:56 | [diff] [blame] | 119 | result &= params->GetDouble("dpi", ¶meters.dpi); |
| 120 | result &= params->GetDouble("min_shrink", ¶meters.min_shrink); |
| 121 | result &= params->GetDouble("max_shrink", ¶meters.max_shrink); |
[email protected] | a65175d | 2010-08-17 04:00:57 | [diff] [blame] | 122 | result &= params->GetBoolean("selection_only", ¶meters.selection_only); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 123 | return result; |
| 124 | } |
| 125 | |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 126 | string16 GetSwitchValueString16(const CommandLine& command_line, |
| 127 | const char* switchName) { |
| 128 | #ifdef OS_WIN |
| 129 | CommandLine::StringType native_switch_val; |
| 130 | native_switch_val = command_line.GetSwitchValueNative(switchName); |
| 131 | return string16(native_switch_val); |
| 132 | #elif defined(OS_POSIX) |
| 133 | // POSIX Command line string types are different. |
| 134 | CommandLine::StringType native_switch_val; |
| 135 | native_switch_val = command_line.GetSwitchValueASCII(switchName); |
| 136 | // Convert the ASCII string to UTF16 to prepare to pass. |
| 137 | return string16(ASCIIToUTF16(native_switch_val)); |
| 138 | #endif |
| 139 | } |
| 140 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 141 | void CloudPrintDataSenderHelper::CallJavascriptFunction( |
| 142 | const std::wstring& function_name) { |
[email protected] | adcf849 | 2011-03-09 22:41:39 | [diff] [blame] | 143 | web_ui_->CallJavascriptFunction(WideToASCII(function_name)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void CloudPrintDataSenderHelper::CallJavascriptFunction( |
| 147 | const std::wstring& function_name, const Value& arg) { |
[email protected] | adcf849 | 2011-03-09 22:41:39 | [diff] [blame] | 148 | web_ui_->CallJavascriptFunction(WideToASCII(function_name), arg); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void CloudPrintDataSenderHelper::CallJavascriptFunction( |
| 152 | const std::wstring& function_name, const Value& arg1, const Value& arg2) { |
[email protected] | adcf849 | 2011-03-09 22:41:39 | [diff] [blame] | 153 | web_ui_->CallJavascriptFunction(WideToASCII(function_name), arg1, arg2); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 154 | } |
| 155 | |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 156 | void CloudPrintDataSenderHelper::CallJavascriptFunction( |
| 157 | const std::wstring& function_name, |
| 158 | const Value& arg1, |
| 159 | const Value& arg2, |
| 160 | const Value& arg3) { |
| 161 | web_ui_->CallJavascriptFunction(WideToASCII(function_name), arg1, arg2, arg3); |
| 162 | } |
| 163 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 164 | // Clears out the pointer we're using to communicate. Either routine is |
| 165 | // potentially expensive enough that stopping whatever is in progress |
| 166 | // is worth it. |
| 167 | void CloudPrintDataSender::CancelPrintDataFile() { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 168 | base::AutoLock lock(lock_); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 169 | // We don't own helper, it was passed in to us, so no need to |
| 170 | // delete, just let it go. |
| 171 | helper_ = NULL; |
| 172 | } |
| 173 | |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 174 | CloudPrintDataSender::CloudPrintDataSender(CloudPrintDataSenderHelper* helper, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 175 | const string16& print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 176 | const string16& print_ticket, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 177 | const std::string& file_type) |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 178 | : helper_(helper), |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 179 | print_job_title_(print_job_title), |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 180 | print_ticket_(print_ticket), |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 181 | file_type_(file_type) { |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | CloudPrintDataSender::~CloudPrintDataSender() {} |
| 185 | |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 186 | // Grab the raw file contents and massage them into shape for |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 187 | // sending to the dialog contents (and up to the cloud print server) |
| 188 | // by encoding it and prefixing it with the appropriate mime type. |
| 189 | // Once that is done, kick off the next part of the task on the IO |
| 190 | // thread. |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 191 | void CloudPrintDataSender::ReadPrintDataFile(const FilePath& path_to_file) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 192 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 193 | int64 file_size = 0; |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 194 | if (file_util::GetFileSize(path_to_file, &file_size) && file_size != 0) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 195 | std::string file_data; |
| 196 | if (file_size < kuint32max) { |
| 197 | file_data.reserve(static_cast<unsigned int>(file_size)); |
| 198 | } else { |
| 199 | DLOG(WARNING) << " print data file too large to reserve space"; |
| 200 | } |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 201 | if (helper_ && file_util::ReadFileToString(path_to_file, &file_data)) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 202 | std::string base64_data; |
| 203 | base::Base64Encode(file_data, &base64_data); |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 204 | std::string header("data:"); |
| 205 | header.append(file_type_); |
| 206 | header.append(";base64,"); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 207 | base64_data.insert(0, header); |
| 208 | scoped_ptr<StringValue> new_data(new StringValue(base64_data)); |
| 209 | print_data_.swap(new_data); |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 210 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 211 | NewRunnableMethod( |
| 212 | this, |
| 213 | &CloudPrintDataSender::SendPrintDataFile)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // We have the data in hand that needs to be pushed into the dialog |
| 219 | // contents; do so from the IO thread. |
| 220 | |
| 221 | // TODO(scottbyer): If the print data ends up being larger than the |
| 222 | // upload limit (currently 10MB), what we need to do is upload that |
| 223 | // large data to google docs and set the URL in the printing |
| 224 | // JavaScript to that location, and make sure it gets deleted when not |
| 225 | // needed. - 4/1/2010 |
| 226 | void CloudPrintDataSender::SendPrintDataFile() { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 227 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 228 | base::AutoLock lock(lock_); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 229 | if (helper_ && print_data_.get()) { |
[email protected] | 9848c7e | 2010-06-03 16:06:56 | [diff] [blame] | 230 | StringValue title(print_job_title_); |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 231 | StringValue ticket(print_ticket_); |
| 232 | // TODO(abodenha): Change Javascript call to pass in print ticket |
| 233 | // after server side support is added. Add test for it. |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 234 | |
| 235 | // Send the print data to the dialog contents. The JavaScript |
| 236 | // function is a preliminary API for prototyping purposes and is |
| 237 | // subject to change. |
| 238 | const_cast<CloudPrintDataSenderHelper*>(helper_)->CallJavascriptFunction( |
| 239 | L"printApp._printDataUrl", *print_data_, title); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 244 | CloudPrintFlowHandler::CloudPrintFlowHandler(const FilePath& path_to_file, |
| 245 | const string16& print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 246 | const string16& print_ticket, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 247 | const std::string& file_type) |
[email protected] | c7bf745 | 2011-09-12 21:31:50 | [diff] [blame] | 248 | : dialog_delegate_(NULL), |
| 249 | path_to_file_(path_to_file), |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 250 | print_job_title_(print_job_title), |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 251 | print_ticket_(print_ticket), |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 252 | file_type_(file_type) { |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | CloudPrintFlowHandler::~CloudPrintFlowHandler() { |
| 256 | // This will also cancel any task in flight. |
| 257 | CancelAnyRunningTask(); |
| 258 | } |
| 259 | |
| 260 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 261 | void CloudPrintFlowHandler::SetDialogDelegate( |
| 262 | CloudPrintHtmlDialogDelegate* delegate) { |
[email protected] | 7b74898 | 2011-02-14 19:28:23 | [diff] [blame] | 263 | // Even if setting a new WebUI, it means any previous task needs |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 264 | // to be cancelled, it's now invalid. |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 265 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 266 | CancelAnyRunningTask(); |
| 267 | dialog_delegate_ = delegate; |
| 268 | } |
| 269 | |
| 270 | // Cancels any print data sender we have in flight and removes our |
| 271 | // reference to it, so when the task that is calling it finishes and |
| 272 | // removes it's reference, it goes away. |
| 273 | void CloudPrintFlowHandler::CancelAnyRunningTask() { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 274 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 275 | if (print_data_sender_.get()) { |
| 276 | print_data_sender_->CancelPrintDataFile(); |
| 277 | print_data_sender_ = NULL; |
| 278 | } |
| 279 | } |
| 280 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 281 | void CloudPrintFlowHandler::RegisterMessages() { |
[email protected] | 7b74898 | 2011-02-14 19:28:23 | [diff] [blame] | 282 | if (!web_ui_) |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 283 | return; |
| 284 | |
| 285 | // TODO(scottbyer) - This is where we will register messages for the |
| 286 | // UI JS to use. Needed: Call to update page setup parameters. |
[email protected] | ba4fc24 | 2011-10-04 18:56:56 | [diff] [blame] | 287 | web_ui_->RegisterMessageCallback("ShowDebugger", |
| 288 | base::Bind(&CloudPrintFlowHandler::HandleShowDebugger, |
| 289 | base::Unretained(this))); |
| 290 | web_ui_->RegisterMessageCallback("SendPrintData", |
| 291 | base::Bind(&CloudPrintFlowHandler::HandleSendPrintData, |
| 292 | base::Unretained(this))); |
| 293 | web_ui_->RegisterMessageCallback("SetPageParameters", |
| 294 | base::Bind(&CloudPrintFlowHandler::HandleSetPageParameters, |
| 295 | base::Unretained(this))); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 296 | |
[email protected] | 0eb25c4 | 2011-08-11 14:50:14 | [diff] [blame] | 297 | // Register for appropriate notifications, and re-direct the URL |
| 298 | // to the real server URL, now that we've gotten an HTML dialog |
| 299 | // going. |
| 300 | NavigationController* controller = &web_ui_->tab_contents()->controller(); |
| 301 | NavigationEntry* pending_entry = controller->pending_entry(); |
| 302 | if (pending_entry) { |
| 303 | Profile* profile = Profile::FromWebUI(web_ui_); |
| 304 | pending_entry->set_url( |
| 305 | CloudPrintURL(profile).GetCloudPrintServiceDialogURL()); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 306 | } |
[email protected] | 0eb25c4 | 2011-08-11 14:50:14 | [diff] [blame] | 307 | registrar_.Add(this, content::NOTIFICATION_LOAD_STOP, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 308 | content::Source<NavigationController>(controller)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 309 | } |
| 310 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 311 | void CloudPrintFlowHandler::Observe( |
| 312 | int type, |
| 313 | const content::NotificationSource& source, |
| 314 | const content::NotificationDetails& details) { |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 315 | if (type == content::NOTIFICATION_LOAD_STOP) { |
[email protected] | 20c52d2 | 2011-06-20 22:42:42 | [diff] [blame] | 316 | // Take the opportunity to set some (minimal) additional |
| 317 | // script permissions required for the web UI. |
| 318 | GURL url = web_ui_->tab_contents()->GetURL(); |
| 319 | GURL dialog_url = CloudPrintURL( |
[email protected] | 0eb25c4 | 2011-08-11 14:50:14 | [diff] [blame] | 320 | Profile::FromWebUI(web_ui_)).GetCloudPrintServiceDialogURL(); |
[email protected] | 20c52d2 | 2011-06-20 22:42:42 | [diff] [blame] | 321 | if (url.host() == dialog_url.host() && |
| 322 | url.path() == dialog_url.path() && |
| 323 | url.scheme() == dialog_url.scheme()) { |
| 324 | RenderViewHost* rvh = web_ui_->tab_contents()->render_view_host(); |
| 325 | if (rvh && rvh->delegate()) { |
| 326 | WebPreferences webkit_prefs = rvh->delegate()->GetWebkitPrefs(); |
| 327 | webkit_prefs.allow_scripts_to_close_windows = true; |
[email protected] | 9abd51f | 2011-09-21 19:11:35 | [diff] [blame] | 328 | rvh->UpdateWebkitPreferences(webkit_prefs); |
[email protected] | 20c52d2 | 2011-06-20 22:42:42 | [diff] [blame] | 329 | } else { |
| 330 | DCHECK(false); |
| 331 | } |
| 332 | } |
| 333 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 334 | // Choose one or the other. If you need to debug, bring up the |
| 335 | // debugger. You can then use the various chrome.send() |
| 336 | // registrations above to kick of the various function calls, |
| 337 | // including chrome.send("SendPrintData") in the javaScript |
| 338 | // console and watch things happen with: |
| 339 | // HandleShowDebugger(NULL); |
| 340 | HandleSendPrintData(NULL); |
| 341 | } |
| 342 | } |
| 343 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 344 | void CloudPrintFlowHandler::HandleShowDebugger(const ListValue* args) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 345 | ShowDebugger(); |
| 346 | } |
| 347 | |
| 348 | void CloudPrintFlowHandler::ShowDebugger() { |
[email protected] | 7b74898 | 2011-02-14 19:28:23 | [diff] [blame] | 349 | if (web_ui_) { |
| 350 | RenderViewHost* rvh = web_ui_->tab_contents()->render_view_host(); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 351 | if (rvh) |
[email protected] | aebdd07 | 2011-07-07 12:36:59 | [diff] [blame] | 352 | DevToolsWindow::OpenDevToolsWindow(rvh); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
| 356 | scoped_refptr<CloudPrintDataSender> |
| 357 | CloudPrintFlowHandler::CreateCloudPrintDataSender() { |
[email protected] | 7b74898 | 2011-02-14 19:28:23 | [diff] [blame] | 358 | DCHECK(web_ui_); |
| 359 | print_data_helper_.reset(new CloudPrintDataSenderHelper(web_ui_)); |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 360 | return new CloudPrintDataSender(print_data_helper_.get(), |
| 361 | print_job_title_, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 362 | print_ticket_, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 363 | file_type_); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 364 | } |
| 365 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 366 | void CloudPrintFlowHandler::HandleSendPrintData(const ListValue* args) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 367 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 368 | // This will cancel any ReadPrintDataFile() or SendPrintDataFile() |
| 369 | // requests in flight (this is anticipation of when setting page |
| 370 | // setup parameters becomes asynchronous and may be set while some |
| 371 | // data is in flight). Then we can clear out the print data. |
| 372 | CancelAnyRunningTask(); |
[email protected] | 7b74898 | 2011-02-14 19:28:23 | [diff] [blame] | 373 | if (web_ui_) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 374 | print_data_sender_ = CreateCloudPrintDataSender(); |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 375 | BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 376 | NewRunnableMethod( |
| 377 | print_data_sender_.get(), |
| 378 | &CloudPrintDataSender::ReadPrintDataFile, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 379 | path_to_file_)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
[email protected] | 88942a2 | 2010-08-19 20:34:43 | [diff] [blame] | 383 | void CloudPrintFlowHandler::HandleSetPageParameters(const ListValue* args) { |
[email protected] | 036056a3 | 2011-03-03 21:05:01 | [diff] [blame] | 384 | std::string json; |
[email protected] | e675f7b | 2011-06-22 17:32:12 | [diff] [blame] | 385 | bool ret = args->GetString(0, &json); |
| 386 | if (!ret || json.empty()) { |
[email protected] | 036056a3 | 2011-03-03 21:05:01 | [diff] [blame] | 387 | NOTREACHED() << "Empty json string"; |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 388 | return; |
[email protected] | 036056a3 | 2011-03-03 21:05:01 | [diff] [blame] | 389 | } |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 390 | |
| 391 | // These are backstop default values - 72 dpi to match the screen, |
| 392 | // 8.5x11 inch paper with margins subtracted (1/4 inch top, left, |
| 393 | // right and 0.56 bottom), and the min page shrink and max page |
| 394 | // shrink values appear all over the place with no explanation. |
| 395 | |
| 396 | // TODO(scottbyer): Get a Linux/ChromeOS edge for PrintSettings |
| 397 | // working so that we can get the default values from there. Fix up |
| 398 | // PrintWebViewHelper to do the same. |
| 399 | const int kDPI = 72; |
| 400 | const int kWidth = static_cast<int>((8.5-0.25-0.25)*kDPI); |
| 401 | const int kHeight = static_cast<int>((11-0.25-0.56)*kDPI); |
| 402 | const double kMinPageShrink = 1.25; |
| 403 | const double kMaxPageShrink = 2.0; |
| 404 | |
[email protected] | 1375e3ab | 2011-03-24 17:07:22 | [diff] [blame] | 405 | PrintMsg_Print_Params default_settings; |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 406 | default_settings.printable_size = gfx::Size(kWidth, kHeight); |
| 407 | default_settings.dpi = kDPI; |
| 408 | default_settings.min_shrink = kMinPageShrink; |
| 409 | default_settings.max_shrink = kMaxPageShrink; |
| 410 | default_settings.desired_dpi = kDPI; |
| 411 | default_settings.document_cookie = 0; |
| 412 | default_settings.selection_only = false; |
[email protected] | 718af82 | 2011-08-12 22:11:33 | [diff] [blame] | 413 | default_settings.preview_request_id = 0; |
| 414 | default_settings.is_first_request = true; |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 415 | |
| 416 | if (!GetPageSetupParameters(json, default_settings)) { |
| 417 | NOTREACHED(); |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | // TODO(scottbyer) - Here is where we would kick the originating |
| 422 | // renderer thread with these new parameters in order to get it to |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 423 | // 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] | 424 | // currently synchronous, so there's a lot of work to do to get to |
| 425 | // that point. |
| 426 | } |
| 427 | |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 428 | void CloudPrintFlowHandler::StoreDialogClientSize() const { |
[email protected] | 7b74898 | 2011-02-14 19:28:23 | [diff] [blame] | 429 | if (web_ui_ && web_ui_->tab_contents() && web_ui_->tab_contents()->view()) { |
| 430 | gfx::Size size = web_ui_->tab_contents()->view()->GetContainerSize(); |
[email protected] | 0eb25c4 | 2011-08-11 14:50:14 | [diff] [blame] | 431 | Profile* profile = Profile::FromWebUI(web_ui_); |
| 432 | profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogWidth, |
| 433 | size.width()); |
| 434 | profile->GetPrefs()->SetInteger(prefs::kCloudPrintDialogHeight, |
| 435 | size.height()); |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 439 | CloudPrintHtmlDialogDelegate::CloudPrintHtmlDialogDelegate( |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 440 | const FilePath& path_to_file, |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 441 | int width, int height, |
[email protected] | 9848c7e | 2010-06-03 16:06:56 | [diff] [blame] | 442 | const std::string& json_arguments, |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 443 | const string16& print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 444 | const string16& print_ticket, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 445 | const std::string& file_type, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 446 | bool modal, |
| 447 | bool delete_on_close) |
| 448 | : delete_on_close_(delete_on_close), |
| 449 | flow_handler_(new CloudPrintFlowHandler(path_to_file, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 450 | print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 451 | print_ticket, |
[email protected] | a984bdf | 2011-03-15 20:17:16 | [diff] [blame] | 452 | file_type)), |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 453 | modal_(modal), |
[email protected] | 6ddda23 | 2011-04-22 15:41:47 | [diff] [blame] | 454 | owns_flow_handler_(true), |
| 455 | path_to_file_(path_to_file) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 456 | Init(width, height, json_arguments); |
| 457 | } |
| 458 | |
[email protected] | 05acb5547 | 2011-02-03 00:11:07 | [diff] [blame] | 459 | // For unit testing. |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 460 | CloudPrintHtmlDialogDelegate::CloudPrintHtmlDialogDelegate( |
| 461 | CloudPrintFlowHandler* flow_handler, |
| 462 | int width, int height, |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 463 | const std::string& json_arguments, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 464 | bool modal, |
| 465 | bool delete_on_close) |
| 466 | : delete_on_close_(delete_on_close), |
| 467 | flow_handler_(flow_handler), |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 468 | modal_(modal), |
[email protected] | 18137e0 | 2010-05-25 21:10:35 | [diff] [blame] | 469 | owns_flow_handler_(true) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 470 | Init(width, height, json_arguments); |
| 471 | } |
| 472 | |
[email protected] | 05acb5547 | 2011-02-03 00:11:07 | [diff] [blame] | 473 | void CloudPrintHtmlDialogDelegate::Init(int width, int height, |
| 474 | const std::string& json_arguments) { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 475 | // This information is needed to show the dialog HTML content. |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 476 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 477 | params_.url = GURL(chrome::kChromeUICloudPrintResourcesURL); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 478 | params_.height = height; |
| 479 | params_.width = width; |
| 480 | params_.json_input = json_arguments; |
| 481 | |
| 482 | flow_handler_->SetDialogDelegate(this); |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 483 | // If we're not modal we can show the dialog with no browser. |
| 484 | // We need this to keep Chrome alive while our dialog is up. |
| 485 | if (!modal_) |
| 486 | BrowserList::StartKeepAlive(); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | CloudPrintHtmlDialogDelegate::~CloudPrintHtmlDialogDelegate() { |
| 490 | // If the flow_handler_ is about to outlive us because we don't own |
| 491 | // it anymore, we need to have it remove it's reference to us. |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 492 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 493 | flow_handler_->SetDialogDelegate(NULL); |
| 494 | if (owns_flow_handler_) { |
| 495 | delete flow_handler_; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | bool CloudPrintHtmlDialogDelegate::IsDialogModal() const { |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 500 | return modal_; |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 501 | } |
| 502 | |
[email protected] | bdae5c1 | 2011-08-05 21:49:06 | [diff] [blame] | 503 | string16 CloudPrintHtmlDialogDelegate::GetDialogTitle() const { |
| 504 | return string16(); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | GURL CloudPrintHtmlDialogDelegate::GetDialogContentURL() const { |
| 508 | return params_.url; |
| 509 | } |
| 510 | |
[email protected] | 36e1217 | 2011-02-08 23:46:02 | [diff] [blame] | 511 | void CloudPrintHtmlDialogDelegate::GetWebUIMessageHandlers( |
| 512 | std::vector<WebUIMessageHandler*>* handlers) const { |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 513 | handlers->push_back(flow_handler_); |
| 514 | // We don't own flow_handler_ anymore, but it sticks around until at |
| 515 | // least right after OnDialogClosed() is called (and this object is |
| 516 | // destroyed). |
| 517 | owns_flow_handler_ = false; |
| 518 | } |
| 519 | |
| 520 | void CloudPrintHtmlDialogDelegate::GetDialogSize(gfx::Size* size) const { |
| 521 | size->set_width(params_.width); |
| 522 | size->set_height(params_.height); |
| 523 | } |
| 524 | |
| 525 | std::string CloudPrintHtmlDialogDelegate::GetDialogArgs() const { |
| 526 | return params_.json_input; |
| 527 | } |
| 528 | |
| 529 | void CloudPrintHtmlDialogDelegate::OnDialogClosed( |
| 530 | const std::string& json_retval) { |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 531 | // Get the final dialog size and store it. |
| 532 | flow_handler_->StoreDialogClientSize(); |
[email protected] | 6ddda23 | 2011-04-22 15:41:47 | [diff] [blame] | 533 | |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 534 | if (delete_on_close_) { |
[email protected] | 6ddda23 | 2011-04-22 15:41:47 | [diff] [blame] | 535 | BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 536 | NewRunnableFunction(&internal_cloud_print_helpers::Delete, |
| 537 | path_to_file_)); |
| 538 | } |
| 539 | |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 540 | // If we're modal we can show the dialog with no browser. |
| 541 | // End the keep-alive so that Chrome can exit. |
| 542 | if (!modal_) |
| 543 | BrowserList::EndKeepAlive(); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 544 | delete this; |
| 545 | } |
| 546 | |
[email protected] | 18137e0 | 2010-05-25 21:10:35 | [diff] [blame] | 547 | void CloudPrintHtmlDialogDelegate::OnCloseContents(TabContents* source, |
| 548 | bool* out_close_dialog) { |
| 549 | if (out_close_dialog) |
| 550 | *out_close_dialog = true; |
| 551 | } |
| 552 | |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 553 | bool CloudPrintHtmlDialogDelegate::ShouldShowDialogTitle() const { |
| 554 | return false; |
| 555 | } |
| 556 | |
[email protected] | 3447821 | 2011-04-19 01:35:46 | [diff] [blame] | 557 | bool CloudPrintHtmlDialogDelegate::HandleContextMenu( |
| 558 | const ContextMenuParams& params) { |
| 559 | return true; |
| 560 | } |
| 561 | |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 562 | void CreatePrintDialogForBytesImpl(scoped_refptr<RefCountedBytes> data, |
| 563 | const string16& print_job_title, |
| 564 | const string16& print_ticket, |
| 565 | const std::string& file_type, |
| 566 | bool modal) { |
| 567 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 568 | // TODO([email protected]) Writing the PDF to a file before printing |
| 569 | // is wasteful. Modify the dialog flow to pull PDF data from memory. |
| 570 | // See http://code.google.com/p/chromium/issues/detail?id=44093 |
| 571 | FilePath path; |
| 572 | if (file_util::CreateTemporaryFile(&path)) { |
| 573 | file_util::WriteFile(path, |
| 574 | reinterpret_cast<const char*>(data->front()), |
| 575 | data->size()); |
| 576 | } |
| 577 | print_dialog_cloud::CreatePrintDialogForFile(path, |
| 578 | print_job_title, |
| 579 | print_ticket, |
| 580 | file_type, |
| 581 | modal, |
| 582 | true); |
| 583 | } |
| 584 | |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 585 | // Called from the UI thread, starts up the dialog. |
| 586 | void CreateDialogImpl(const FilePath& path_to_file, |
| 587 | const string16& print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 588 | const string16& print_ticket, |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 589 | const std::string& file_type, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 590 | bool modal, |
| 591 | bool delete_on_close) { |
[email protected] | ba4f113 | 2010-10-09 02:02:35 | [diff] [blame] | 592 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 593 | Browser* browser = BrowserList::GetLastActive(); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 594 | |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 595 | const int kDefaultWidth = 497; |
| 596 | const int kDefaultHeight = 332; |
[email protected] | 05acb5547 | 2011-02-03 00:11:07 | [diff] [blame] | 597 | string16 job_title = print_job_title; |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 598 | Profile* profile = NULL; |
[email protected] | 05acb5547 | 2011-02-03 00:11:07 | [diff] [blame] | 599 | if (modal) { |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 600 | DCHECK(browser); |
| 601 | if (job_title.empty() && browser->GetSelectedTabContents()) |
| 602 | job_title = browser->GetSelectedTabContents()->GetTitle(); |
| 603 | profile = browser->GetProfile(); |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 604 | } else { |
[email protected] | 0744cc7 | 2011-08-12 12:05:45 | [diff] [blame] | 605 | std::vector<Profile*> loaded_profiles = |
| 606 | g_browser_process->profile_manager()->GetLoadedProfiles(); |
| 607 | DCHECK_GT(loaded_profiles.size(), 0U); |
| 608 | profile = loaded_profiles[0]; |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 609 | } |
| 610 | DCHECK(profile); |
[email protected] | 05acb5547 | 2011-02-03 00:11:07 | [diff] [blame] | 611 | PrefService* pref_service = profile->GetPrefs(); |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 612 | DCHECK(pref_service); |
| 613 | if (!pref_service->FindPreference(prefs::kCloudPrintDialogWidth)) { |
| 614 | pref_service->RegisterIntegerPref(prefs::kCloudPrintDialogWidth, |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 615 | kDefaultWidth, |
| 616 | PrefService::UNSYNCABLE_PREF); |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 617 | } |
| 618 | if (!pref_service->FindPreference(prefs::kCloudPrintDialogHeight)) { |
| 619 | pref_service->RegisterIntegerPref(prefs::kCloudPrintDialogHeight, |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 620 | kDefaultHeight, |
| 621 | PrefService::UNSYNCABLE_PREF); |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | int width = pref_service->GetInteger(prefs::kCloudPrintDialogWidth); |
| 625 | int height = pref_service->GetInteger(prefs::kCloudPrintDialogHeight); |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 626 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 627 | HtmlDialogUIDelegate* dialog_delegate = |
| 628 | new internal_cloud_print_helpers::CloudPrintHtmlDialogDelegate( |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 629 | path_to_file, width, height, std::string(), job_title, print_ticket, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 630 | file_type, modal, delete_on_close); |
[email protected] | 05acb5547 | 2011-02-03 00:11:07 | [diff] [blame] | 631 | if (modal) { |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 632 | DCHECK(browser); |
| 633 | browser->BrowserShowHtmlDialog(dialog_delegate, NULL); |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 634 | } else { |
[email protected] | eb2d790 | 2011-02-02 18:19:56 | [diff] [blame] | 635 | browser::ShowHtmlDialog(NULL, profile, dialog_delegate); |
[email protected] | e39027a | 2011-01-24 21:41:54 | [diff] [blame] | 636 | } |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 637 | } |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 638 | |
[email protected] | 6ddda23 | 2011-04-22 15:41:47 | [diff] [blame] | 639 | // Provides a runnable function to delete a file. |
| 640 | void Delete(const FilePath& file_path) { |
| 641 | file_util::Delete(file_path, false); |
| 642 | } |
| 643 | |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 644 | } // namespace internal_cloud_print_helpers |
| 645 | |
| 646 | namespace print_dialog_cloud { |
| 647 | |
| 648 | // Called on the FILE or UI thread. This is the main entry point into creating |
| 649 | // the dialog. |
| 650 | |
| 651 | // TODO(scottbyer): The signature here will need to change as the |
| 652 | // workflow through the printing code changes to allow for dynamically |
| 653 | // changing page setup parameters while the dialog is active. |
| 654 | void CreatePrintDialogForFile(const FilePath& path_to_file, |
| 655 | const string16& print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 656 | const string16& print_ticket, |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 657 | const std::string& file_type, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 658 | bool modal, |
| 659 | bool delete_on_close) { |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 660 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE) || |
| 661 | BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 662 | |
| 663 | BrowserThread::PostTask( |
| 664 | BrowserThread::UI, FROM_HERE, |
| 665 | NewRunnableFunction(&internal_cloud_print_helpers::CreateDialogImpl, |
| 666 | path_to_file, |
| 667 | print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 668 | print_ticket, |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 669 | file_type, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 670 | modal, |
| 671 | delete_on_close)); |
| 672 | } |
| 673 | |
| 674 | void CreatePrintDialogForBytes(scoped_refptr<RefCountedBytes> data, |
| 675 | const string16& print_job_title, |
| 676 | const string16& print_ticket, |
| 677 | const std::string& file_type, |
| 678 | bool modal) { |
| 679 | // TODO([email protected]) Avoid cloning the PDF data. Make use of a |
| 680 | // shared memory object instead. |
| 681 | // http://code.google.com/p/chromium/issues/detail?id=44093 |
| 682 | scoped_refptr<RefCountedBytes> cloned_data(new RefCountedBytes(data->data())); |
| 683 | BrowserThread::PostTask( |
| 684 | BrowserThread::FILE, FROM_HERE, |
| 685 | NewRunnableFunction( |
| 686 | &internal_cloud_print_helpers::CreatePrintDialogForBytesImpl, |
| 687 | cloned_data, |
| 688 | print_job_title, |
| 689 | print_ticket, |
| 690 | file_type, |
| 691 | modal)); |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 692 | } |
| 693 | |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 694 | bool CreatePrintDialogFromCommandLine(const CommandLine& command_line) { |
| 695 | if (!command_line.GetSwitchValuePath(switches::kCloudPrintFile).empty()) { |
| 696 | FilePath cloud_print_file; |
| 697 | cloud_print_file = |
| 698 | command_line.GetSwitchValuePath(switches::kCloudPrintFile); |
| 699 | if (!cloud_print_file.empty()) { |
| 700 | string16 print_job_title; |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 701 | string16 print_job_print_ticket; |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 702 | if (command_line.HasSwitch(switches::kCloudPrintJobTitle)) { |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 703 | print_job_title = |
| 704 | internal_cloud_print_helpers::GetSwitchValueString16( |
| 705 | command_line, switches::kCloudPrintJobTitle); |
| 706 | } |
| 707 | if (command_line.HasSwitch(switches::kCloudPrintPrintTicket)) { |
| 708 | print_job_print_ticket = |
| 709 | internal_cloud_print_helpers::GetSwitchValueString16( |
| 710 | command_line, switches::kCloudPrintPrintTicket); |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 711 | } |
| 712 | std::string file_type = "application/pdf"; |
| 713 | if (command_line.HasSwitch(switches::kCloudPrintFileType)) { |
| 714 | file_type = command_line.GetSwitchValueASCII( |
| 715 | switches::kCloudPrintFileType); |
| 716 | } |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 717 | |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 718 | bool delete_on_close = CommandLine::ForCurrentProcess()->HasSwitch( |
| 719 | switches::kCloudPrintDeleteFile); |
| 720 | |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 721 | print_dialog_cloud::CreatePrintDialogForFile(cloud_print_file, |
| 722 | print_job_title, |
[email protected] | e8368e9 | 2011-08-20 04:05:56 | [diff] [blame] | 723 | print_job_print_ticket, |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 724 | file_type, |
[email protected] | d955fc9 | 2011-09-19 20:49:03 | [diff] [blame] | 725 | false, |
| 726 | delete_on_close); |
[email protected] | 65c9d89a | 2011-04-13 21:02:39 | [diff] [blame] | 727 | return true; |
| 728 | } |
| 729 | } |
| 730 | return false; |
| 731 | } |
| 732 | |
[email protected] | 6085c70d | 2011-03-22 22:51:07 | [diff] [blame] | 733 | } // end namespace |