| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/printing/print_dialog_cloud.h" | 5 #include "chrome/browser/printing/print_dialog_cloud.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/json/json_writer.h" |
| 12 #include "base/memory/ref_counted_memory.h" | 13 #include "base/memory/ref_counted_memory.h" |
| 13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 16 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
| 19 #include "chrome/browser/ui/browser_window.h" | 20 #include "chrome/browser/ui/browser_window.h" |
| 20 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" | 21 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" |
| 21 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
| 22 #include "components/cloud_devices/common/cloud_devices_urls.h" | 23 #include "components/cloud_devices/common/cloud_devices_urls.h" |
| 23 #include "components/google/core/browser/google_util.h" | 24 #include "components/google/core/browser/google_util.h" |
| 24 #include "content/public/browser/browser_context.h" | 25 #include "content/public/browser/browser_context.h" |
| 25 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 27 #include "content/public/browser/message_port_provider.h" |
| 26 #include "content/public/browser/render_frame_host.h" | 28 #include "content/public/browser/render_frame_host.h" |
| 29 #include "content/public/common/message_port_types.h" |
| 27 | 30 |
| 28 namespace print_dialog_cloud { | 31 namespace print_dialog_cloud { |
| 29 | 32 |
| 30 namespace { | 33 namespace { |
| 31 | 34 |
| 32 // Limit is used only to allocate memory. Cloud has own small limit. | 35 // Limit is used only to allocate memory. Cloud has own small limit. |
| 33 const int kMaxFileSize = 1024 * 1024 * 1024; | 36 const int kMaxFileSize = 1024 * 1024 * 1024; |
| 34 | 37 |
| 35 // Sets print data into Cloud Print widget. | 38 // Sets print data into Cloud Print widget. |
| 36 class PrintDataSetter : public content::WebContentsObserver { | 39 class PrintDataSetter : public content::WebContentsObserver { |
| 37 public: | 40 public: |
| 38 PrintDataSetter(content::WebContents* web_contents, | 41 PrintDataSetter(content::WebContents* web_contents, |
| 39 const scoped_refptr<base::RefCountedMemory>& data, | 42 const scoped_refptr<base::RefCountedMemory>& data, |
| 40 const base::string16& print_job_title, | 43 const base::string16& print_job_title, |
| 41 const base::string16& print_ticket, | 44 const base::string16& print_ticket, |
| 42 const std::string& file_type) | 45 const std::string& file_type) |
| 43 : WebContentsObserver(web_contents) { | 46 : WebContentsObserver(web_contents) { |
| 44 DCHECK_NE(data->size(), 0u); | 47 DCHECK_NE(data->size(), 0u); |
| 45 std::string base64_data; | 48 std::string base64_data; |
| 46 base::Base64Encode(base::StringPiece(data->front_as<char>(), data->size()), | 49 base::Base64Encode(base::StringPiece(data->front_as<char>(), data->size()), |
| 47 &base64_data); | 50 &base64_data); |
| 48 std::string header("data:"); | 51 std::string header("data:"); |
| 49 header.append(file_type); | 52 header.append(file_type); |
| 50 header.append(";base64,"); | 53 header.append(";base64,"); |
| 51 base64_data.insert(0, header); | 54 base64_data.insert(0, header); |
| 52 | 55 |
| 53 InitPrintFunction(base::StringValue(base64_data), | 56 base::DictionaryValue message_data; |
| 54 base::StringValue(print_job_title)); | 57 message_data.SetString("type", "dataUrl"); |
| 58 message_data.SetString("title", print_job_title); |
| 59 message_data.SetString("content", base64_data); |
| 60 std::string json_data; |
| 61 base::JSONWriter::Write(message_data, &json_data); |
| 62 message_data_ = L"cp-dialog-set-print-document::"; |
| 63 message_data_.append(base::UTF8ToUTF16(json_data)); |
| 55 } | 64 } |
| 56 | 65 |
| 57 private: | 66 private: |
| 58 // Overridden from content::WebContentsObserver: | 67 // Overridden from content::WebContentsObserver: |
| 59 void DocumentLoadedInFrame( | 68 void DocumentLoadedInFrame( |
| 60 content::RenderFrameHost* render_frame_host) override { | 69 content::RenderFrameHost* render_frame_host) override { |
| 61 if (cloud_devices::IsCloudPrintURL(web_contents()->GetURL())) | 70 GURL url = web_contents()->GetURL(); |
| 62 render_frame_host->ExecuteJavaScript(print_function_); | 71 if (cloud_devices::IsCloudPrintURL(url)) { |
| 72 base::string16 origin = base::UTF8ToUTF16(url.GetOrigin().spec()); |
| 73 content::MessagePortProvider::PostMessageToFrame( |
| 74 web_contents(), origin, origin, message_data_, |
| 75 std::vector<content::TransferredMessagePort>()); |
| 76 } |
| 63 } | 77 } |
| 64 | 78 |
| 65 void WebContentsDestroyed() override { delete this; } | 79 void WebContentsDestroyed() override { delete this; } |
| 66 | 80 |
| 67 void InitPrintFunction(const base::Value& arg1, const base::Value& arg2) { | 81 base::string16 message_data_; |
| 68 std::vector<const base::Value*> args; | |
| 69 args.push_back(&arg1); | |
| 70 args.push_back(&arg2); | |
| 71 print_function_ = | |
| 72 content::WebUI::GetJavascriptCall("printApp._printDataUrl", args); | |
| 73 } | |
| 74 | |
| 75 base::string16 print_function_; | |
| 76 | |
| 77 DISALLOW_COPY_AND_ASSIGN(PrintDataSetter); | 82 DISALLOW_COPY_AND_ASSIGN(PrintDataSetter); |
| 78 }; | 83 }; |
| 79 | 84 |
| 80 void CreatePrintDialog(content::BrowserContext* browser_context, | 85 void CreatePrintDialog(content::BrowserContext* browser_context, |
| 81 const base::string16& print_job_title, | 86 const base::string16& print_job_title, |
| 82 const base::string16& print_ticket, | 87 const base::string16& print_ticket, |
| 83 const std::string& file_type, | 88 const std::string& file_type, |
| 84 const scoped_refptr<base::RefCountedMemory>& data) { | 89 const scoped_refptr<base::RefCountedMemory>& data) { |
| 85 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 86 Profile* profile = Profile::FromBrowserContext(browser_context); | 91 Profile* profile = Profile::FromBrowserContext(browser_context); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 std::string file_type = | 152 std::string file_type = |
| 148 command_line.GetSwitchValueASCII(switches::kCloudPrintFileType); | 153 command_line.GetSwitchValueASCII(switches::kCloudPrintFileType); |
| 149 if (file_type.empty()) | 154 if (file_type.empty()) |
| 150 file_type = "application/pdf"; | 155 file_type = "application/pdf"; |
| 151 CreatePrintDialogForFile(profile, cloud_print_file, print_job_title, | 156 CreatePrintDialogForFile(profile, cloud_print_file, print_job_title, |
| 152 print_job_print_ticket, file_type); | 157 print_job_print_ticket, file_type); |
| 153 return true; | 158 return true; |
| 154 } | 159 } |
| 155 | 160 |
| 156 } // namespace print_dialog_cloud | 161 } // namespace print_dialog_cloud |
| OLD | NEW |