Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/printing_message_filter.h" | 5 #include "chrome/browser/printing/printing_message_filter.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 #if defined(OS_CHROMEOS) | 25 #if defined(OS_CHROMEOS) |
| 26 #include <fcntl.h> | 26 #include <fcntl.h> |
| 27 | 27 |
| 28 #include <map> | 28 #include <map> |
| 29 | 29 |
| 30 #include "base/file_util.h" | 30 #include "base/file_util.h" |
| 31 #include "base/lazy_instance.h" | 31 #include "base/lazy_instance.h" |
| 32 #include "chrome/browser/printing/print_dialog_cloud.h" | 32 #include "chrome/browser/printing/print_dialog_cloud.h" |
| 33 #endif | 33 #endif |
| 34 | 34 |
| 35 #if defined(OS_ANDROID) | |
| 36 #include "chrome/browser/printing/print_view_manager_basic.h" | |
| 37 #endif | |
| 38 | |
| 35 using content::BrowserThread; | 39 using content::BrowserThread; |
| 36 | 40 |
| 37 namespace { | 41 namespace { |
| 38 | 42 |
| 39 #if defined(OS_CHROMEOS) | 43 #if defined(OS_CHROMEOS) |
| 40 typedef std::map<int, base::FilePath> SequenceToPathMap; | 44 typedef std::map<int, base::FilePath> SequenceToPathMap; |
| 41 | 45 |
| 42 struct PrintingSequencePathMap { | 46 struct PrintingSequencePathMap { |
| 43 SequenceToPathMap map; | 47 SequenceToPathMap map; |
| 44 int sequence; | 48 int sequence; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 PrintingMessageFilter::~PrintingMessageFilter() { | 97 PrintingMessageFilter::~PrintingMessageFilter() { |
| 94 } | 98 } |
| 95 | 99 |
| 96 void PrintingMessageFilter::OverrideThreadForMessage( | 100 void PrintingMessageFilter::OverrideThreadForMessage( |
| 97 const IPC::Message& message, BrowserThread::ID* thread) { | 101 const IPC::Message& message, BrowserThread::ID* thread) { |
| 98 #if defined(OS_CHROMEOS) | 102 #if defined(OS_CHROMEOS) |
| 99 if (message.type() == PrintHostMsg_AllocateTempFileForPrinting::ID || | 103 if (message.type() == PrintHostMsg_AllocateTempFileForPrinting::ID || |
| 100 message.type() == PrintHostMsg_TempFileForPrintingWritten::ID) { | 104 message.type() == PrintHostMsg_TempFileForPrintingWritten::ID) { |
| 101 *thread = BrowserThread::FILE; | 105 *thread = BrowserThread::FILE; |
| 102 } | 106 } |
| 107 #elif defined(OS_ANDROID) | |
| 108 if (message.type() == PrintHostMsg_AllocateTempFileForPrinting::ID || | |
| 109 message.type() == PrintHostMsg_TempFileForPrintingWritten::ID) { | |
| 110 *thread = BrowserThread::UI; | |
| 111 } | |
| 103 #endif | 112 #endif |
| 104 } | 113 } |
| 105 | 114 |
| 106 bool PrintingMessageFilter::OnMessageReceived(const IPC::Message& message, | 115 bool PrintingMessageFilter::OnMessageReceived(const IPC::Message& message, |
| 107 bool* message_was_ok) { | 116 bool* message_was_ok) { |
| 108 bool handled = true; | 117 bool handled = true; |
| 109 IPC_BEGIN_MESSAGE_MAP_EX(PrintingMessageFilter, message, *message_was_ok) | 118 IPC_BEGIN_MESSAGE_MAP_EX(PrintingMessageFilter, message, *message_was_ok) |
| 110 #if defined(OS_WIN) | 119 #if defined(OS_WIN) |
| 111 IPC_MESSAGE_HANDLER(PrintHostMsg_DuplicateSection, OnDuplicateSection) | 120 IPC_MESSAGE_HANDLER(PrintHostMsg_DuplicateSection, OnDuplicateSection) |
| 112 #endif | 121 #endif |
| 113 #if defined(OS_CHROMEOS) | 122 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
| 114 IPC_MESSAGE_HANDLER(PrintHostMsg_AllocateTempFileForPrinting, | 123 IPC_MESSAGE_HANDLER(PrintHostMsg_AllocateTempFileForPrinting, |
| 115 OnAllocateTempFileForPrinting) | 124 OnAllocateTempFileForPrinting) |
| 116 IPC_MESSAGE_HANDLER(PrintHostMsg_TempFileForPrintingWritten, | 125 IPC_MESSAGE_HANDLER(PrintHostMsg_TempFileForPrintingWritten, |
| 117 OnTempFileForPrintingWritten) | 126 OnTempFileForPrintingWritten) |
| 118 #endif | 127 #endif |
| 119 IPC_MESSAGE_HANDLER(PrintHostMsg_IsPrintingEnabled, OnIsPrintingEnabled) | 128 IPC_MESSAGE_HANDLER(PrintHostMsg_IsPrintingEnabled, OnIsPrintingEnabled) |
| 120 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_GetDefaultPrintSettings, | 129 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_GetDefaultPrintSettings, |
| 121 OnGetDefaultPrintSettings) | 130 OnGetDefaultPrintSettings) |
| 122 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_ScriptedPrint, OnScriptedPrint) | 131 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_ScriptedPrint, OnScriptedPrint) |
| 123 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_UpdatePrintSettings, | 132 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_UpdatePrintSettings, |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 134 void PrintingMessageFilter::OnDuplicateSection( | 143 void PrintingMessageFilter::OnDuplicateSection( |
| 135 base::SharedMemoryHandle renderer_handle, | 144 base::SharedMemoryHandle renderer_handle, |
| 136 base::SharedMemoryHandle* browser_handle) { | 145 base::SharedMemoryHandle* browser_handle) { |
| 137 // Duplicate the handle in this process right now so the memory is kept alive | 146 // Duplicate the handle in this process right now so the memory is kept alive |
| 138 // (even if it is not mapped) | 147 // (even if it is not mapped) |
| 139 base::SharedMemory shared_buf(renderer_handle, true, PeerHandle()); | 148 base::SharedMemory shared_buf(renderer_handle, true, PeerHandle()); |
| 140 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), browser_handle); | 149 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), browser_handle); |
| 141 } | 150 } |
| 142 #endif | 151 #endif |
| 143 | 152 |
| 153 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) | |
| 154 void PrintingMessageFilter::OnAllocateTempFileForPrinting( | |
| 155 int render_view_id, | |
| 156 base::FileDescriptor* temp_file_fd, | |
| 157 int* sequence_number) { | |
| 144 #if defined(OS_CHROMEOS) | 158 #if defined(OS_CHROMEOS) |
| 145 void PrintingMessageFilter::OnAllocateTempFileForPrinting( | 159 // TODO(thestig): Use |render_view_id| for Chrome OS. |
| 146 base::FileDescriptor* temp_file_fd, int* sequence_number) { | |
| 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 148 temp_file_fd->fd = *sequence_number = -1; | 161 temp_file_fd->fd = *sequence_number = -1; |
| 149 temp_file_fd->auto_close = false; | 162 temp_file_fd->auto_close = false; |
| 150 | 163 |
| 151 SequenceToPathMap* map = &g_printing_file_descriptor_map.Get().map; | 164 SequenceToPathMap* map = &g_printing_file_descriptor_map.Get().map; |
| 152 *sequence_number = g_printing_file_descriptor_map.Get().sequence++; | 165 *sequence_number = g_printing_file_descriptor_map.Get().sequence++; |
| 153 | 166 |
| 154 base::FilePath path; | 167 base::FilePath path; |
| 155 if (file_util::CreateTemporaryFile(&path)) { | 168 if (file_util::CreateTemporaryFile(&path)) { |
| 156 int fd = open(path.value().c_str(), O_WRONLY); | 169 int fd = open(path.value().c_str(), O_WRONLY); |
| 157 if (fd >= 0) { | 170 if (fd >= 0) { |
| 158 SequenceToPathMap::iterator it = map->find(*sequence_number); | 171 SequenceToPathMap::iterator it = map->find(*sequence_number); |
| 159 if (it != map->end()) { | 172 if (it != map->end()) { |
| 160 NOTREACHED() << "Sequence number already in use. seq=" << | 173 NOTREACHED() << "Sequence number already in use. seq=" << |
| 161 *sequence_number; | 174 *sequence_number; |
| 162 } else { | 175 } else { |
| 163 (*map)[*sequence_number] = path; | 176 (*map)[*sequence_number] = path; |
| 164 temp_file_fd->fd = fd; | 177 temp_file_fd->fd = fd; |
| 165 temp_file_fd->auto_close = true; | 178 temp_file_fd->auto_close = true; |
| 166 } | 179 } |
| 167 } | 180 } |
| 168 } | 181 } |
| 182 #elif defined(OS_ANDROID) | |
| 183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 184 content::WebContents* wc = GetWebContentsForRenderView(render_view_id); | |
| 185 printing::PrintViewManagerBasic* print_view_manager = | |
| 186 printing::PrintViewManagerBasic::FromWebContents(wc); | |
| 187 // The file descriptor is originally created in & passed from the Android | |
| 188 // side, and it will handle the closing. | |
| 189 temp_file_fd->auto_close = false; | |
| 190 temp_file_fd->fd = print_view_manager->GetFileDescriptor().fd; | |
|
Lei Zhang
2013/08/14 18:49:52
I don't know what the exact lifetime of |PrintView
cimamoglu1
2013/08/14 20:38:10
Done.
| |
| 191 #endif | |
| 169 } | 192 } |
| 170 | 193 |
| 171 void PrintingMessageFilter::OnTempFileForPrintingWritten(int render_view_id, | 194 void PrintingMessageFilter::OnTempFileForPrintingWritten(int render_view_id, |
| 172 int sequence_number) { | 195 int sequence_number) { |
| 196 #if defined(OS_CHROMEOS) | |
| 173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 174 SequenceToPathMap* map = &g_printing_file_descriptor_map.Get().map; | 198 SequenceToPathMap* map = &g_printing_file_descriptor_map.Get().map; |
| 175 SequenceToPathMap::iterator it = map->find(sequence_number); | 199 SequenceToPathMap::iterator it = map->find(sequence_number); |
| 176 if (it == map->end()) { | 200 if (it == map->end()) { |
| 177 NOTREACHED() << "Got a sequence that we didn't pass to the " | 201 NOTREACHED() << "Got a sequence that we didn't pass to the " |
| 178 "renderer: " << sequence_number; | 202 "renderer: " << sequence_number; |
| 179 return; | 203 return; |
| 180 } | 204 } |
| 181 BrowserThread::PostTask( | 205 BrowserThread::PostTask( |
| 182 BrowserThread::UI, FROM_HERE, | 206 BrowserThread::UI, FROM_HERE, |
| 183 base::Bind(&PrintingMessageFilter::CreatePrintDialogForFile, | 207 base::Bind(&PrintingMessageFilter::CreatePrintDialogForFile, |
| 184 this, render_view_id, it->second)); | 208 this, render_view_id, it->second)); |
| 185 | 209 |
| 186 // Erase the entry in the map. | 210 // Erase the entry in the map. |
| 187 map->erase(it); | 211 map->erase(it); |
| 212 #elif defined(OS_ANDROID) | |
| 213 NOTIMPLEMENTED(); | |
|
Lei Zhang
2013/08/14 18:49:52
So can you tell me what's going to happen with thi
cimamoglu1
2013/08/14 20:38:10
I was planning to add more code in the future. You
| |
| 214 #endif | |
| 188 } | 215 } |
| 216 #endif // defined(OS_CHROMEOS) || defined(OS_ANDROID) | |
| 189 | 217 |
| 218 #if defined(OS_CHROMEOS) | |
| 190 void PrintingMessageFilter::CreatePrintDialogForFile( | 219 void PrintingMessageFilter::CreatePrintDialogForFile( |
| 191 int render_view_id, | 220 int render_view_id, |
| 192 const base::FilePath& path) { | 221 const base::FilePath& path) { |
| 193 content::WebContents* wc = GetWebContentsForRenderView(render_view_id); | 222 content::WebContents* wc = GetWebContentsForRenderView(render_view_id); |
| 194 print_dialog_cloud::CreatePrintDialogForFile( | 223 print_dialog_cloud::CreatePrintDialogForFile( |
| 195 wc->GetBrowserContext(), | 224 wc->GetBrowserContext(), |
| 196 wc->GetView()->GetTopLevelNativeWindow(), | 225 wc->GetView()->GetTopLevelNativeWindow(), |
| 197 path, | 226 path, |
| 198 wc->GetTitle(), | 227 wc->GetTitle(), |
| 199 string16(), | 228 string16(), |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 385 | 414 |
| 386 #if defined(ENABLE_FULL_PRINTING) | 415 #if defined(ENABLE_FULL_PRINTING) |
| 387 void PrintingMessageFilter::OnCheckForCancel(int32 preview_ui_id, | 416 void PrintingMessageFilter::OnCheckForCancel(int32 preview_ui_id, |
| 388 int preview_request_id, | 417 int preview_request_id, |
| 389 bool* cancel) { | 418 bool* cancel) { |
| 390 PrintPreviewUI::GetCurrentPrintPreviewStatus(preview_ui_id, | 419 PrintPreviewUI::GetCurrentPrintPreviewStatus(preview_ui_id, |
| 391 preview_request_id, | 420 preview_request_id, |
| 392 cancel); | 421 cancel); |
| 393 } | 422 } |
| 394 #endif | 423 #endif |
| OLD | NEW |