Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_preview_message_handler.h" | 5 #include "chrome/browser/printing/print_preview_message_handler.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/ref_counted_memory.h" | 10 #include "base/memory/ref_counted_memory.h" |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 41 } | 41 } |
| 42 | 42 |
| 43 RefCountedBytes* GetDataFromHandle(base::SharedMemoryHandle handle, | 43 RefCountedBytes* GetDataFromHandle(base::SharedMemoryHandle handle, |
| 44 uint32 data_size) { | 44 uint32 data_size) { |
| 45 scoped_ptr<base::SharedMemory> shared_buf( | 45 scoped_ptr<base::SharedMemory> shared_buf( |
| 46 new base::SharedMemory(handle, true)); | 46 new base::SharedMemory(handle, true)); |
| 47 if (!shared_buf->Map(data_size)) { | 47 if (!shared_buf->Map(data_size)) { |
| 48 NOTREACHED(); | 48 NOTREACHED(); |
| 49 return NULL; | 49 return NULL; |
| 50 } | 50 } |
| 51 | |
| 51 char* preview_data = static_cast<char*>(shared_buf->memory()); | 52 char* preview_data = static_cast<char*>(shared_buf->memory()); |
| 52 std::vector<unsigned char> data(data_size); | 53 std::vector<unsigned char> data(data_size); |
| 53 memcpy(&data[0], preview_data, data_size); | 54 memcpy(&data[0], preview_data, data_size); |
| 54 return RefCountedBytes::TakeVector(&data); | 55 return RefCountedBytes::TakeVector(&data); |
| 55 } | 56 } |
| 56 | 57 |
| 57 } // namespace | 58 } // namespace |
| 58 | 59 |
| 59 namespace printing { | 60 namespace printing { |
| 60 | 61 |
| 61 PrintPreviewMessageHandler::PrintPreviewMessageHandler( | 62 PrintPreviewMessageHandler::PrintPreviewMessageHandler( |
| 62 TabContents* tab_contents) | 63 TabContents* tab_contents) |
| 63 : TabContentsObserver(tab_contents) { | 64 : TabContentsObserver(tab_contents) { |
| 64 DCHECK(tab_contents); | 65 DCHECK(tab_contents); |
| 65 } | 66 } |
| 66 | 67 |
| 67 PrintPreviewMessageHandler::~PrintPreviewMessageHandler() { | 68 PrintPreviewMessageHandler::~PrintPreviewMessageHandler() { |
| 68 } | 69 } |
| 69 | 70 |
| 70 TabContents* PrintPreviewMessageHandler::GetPrintPreviewTab() { | 71 TabContents* PrintPreviewMessageHandler::GetPrintPreviewTab() { |
| 71 // Get/Create preview tab for initiator tab. | 72 // Get/Create preview tab for initiator tab. |
| 72 printing::PrintPreviewTabController* tab_controller = | 73 printing::PrintPreviewTabController* tab_controller = |
| 73 printing::PrintPreviewTabController::GetInstance(); | 74 printing::PrintPreviewTabController::GetInstance(); |
| 74 if (!tab_controller) | 75 if (!tab_controller) |
| 75 return NULL; | 76 return NULL; |
| 77 | |
| 76 return tab_controller->GetPrintPreviewForTab(tab_contents()); | 78 return tab_controller->GetPrintPreviewForTab(tab_contents()); |
| 77 } | 79 } |
| 78 | 80 |
| 79 void PrintPreviewMessageHandler::OnRequestPrintPreview() { | 81 void PrintPreviewMessageHandler::OnRequestPrintPreview() { |
| 80 PrintPreviewTabController::PrintPreview(tab_contents()); | 82 PrintPreviewTabController::PrintPreview(tab_contents()); |
| 81 } | 83 } |
| 82 | 84 |
| 83 void PrintPreviewMessageHandler::OnDidGetPreviewPageCount(int document_cookie, | 85 void PrintPreviewMessageHandler::OnDidGetPreviewPageCount(int document_cookie, |
| 84 int page_count, | 86 int page_count, |
| 85 bool is_modifiable) { | 87 bool is_modifiable) { |
| 86 if (page_count <= 0) | 88 if (page_count <= 0) |
| 87 return; | 89 return; |
| 90 | |
| 88 TabContents* print_preview_tab = GetPrintPreviewTab(); | 91 TabContents* print_preview_tab = GetPrintPreviewTab(); |
| 89 if (!print_preview_tab) | 92 if (!print_preview_tab || !print_preview_tab->web_ui()) |
|
Lei Zhang
2011/08/09 20:51:19
Everything in this file can go into the cleanup CL
kmadhusu
2011/08/10 16:46:59
Done.
| |
| 90 return; | 93 return; |
| 91 | 94 |
| 92 PrintPreviewUI* print_preview_ui = | 95 PrintPreviewUI* print_preview_ui = |
| 93 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); | 96 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); |
| 94 print_preview_ui->OnDidGetPreviewPageCount( | 97 print_preview_ui->OnDidGetPreviewPageCount( |
| 95 document_cookie, page_count, is_modifiable); | 98 document_cookie, page_count, is_modifiable); |
| 96 } | 99 } |
| 97 | 100 |
| 98 void PrintPreviewMessageHandler::OnDidPreviewPage( | 101 void PrintPreviewMessageHandler::OnDidPreviewPage( |
| 99 const PrintHostMsg_DidPreviewPage_Params& params) { | 102 const PrintHostMsg_DidPreviewPage_Params& params) { |
| 100 RenderViewHost* rvh = tab_contents()->render_view_host(); | 103 RenderViewHost* rvh = tab_contents()->render_view_host(); |
| 101 TabContents* print_preview_tab = GetPrintPreviewTab(); | 104 TabContents* print_preview_tab = GetPrintPreviewTab(); |
| 102 if (!(print_preview_tab && print_preview_tab->web_ui())) { | 105 if (!print_preview_tab || !print_preview_tab->web_ui()) { |
| 103 // Can't find print preview tab means we should abort. | 106 // Can't find print preview tab means we should abort. |
| 104 rvh->Send(new PrintMsg_AbortPreview(rvh->routing_id())); | 107 rvh->Send(new PrintMsg_AbortPreview(rvh->routing_id())); |
| 105 return; | 108 return; |
| 106 } | 109 } |
| 107 | 110 |
| 108 PrintPreviewUI* print_preview_ui = | 111 PrintPreviewUI* print_preview_ui = |
| 109 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); | 112 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); |
| 110 bool has_pending = print_preview_ui->HasPendingRequests(); | 113 bool has_pending = print_preview_ui->HasPendingRequests(); |
| 111 if (has_pending) { | 114 if (has_pending) { |
| 112 // Cancel. Next print preview request will cancel the current one. | 115 // Cancel. Next print preview request will cancel the current one. Just do |
| 113 // Just do the required maintainance work here. | 116 // the required maintenance work here. |
| 114 StopWorker(print_preview_ui->document_cookie()); | 117 StopWorker(print_preview_ui->document_cookie()); |
| 115 print_preview_ui->OnPrintPreviewCancelled(); | 118 print_preview_ui->OnPrintPreviewCancelled(); |
| 116 return; | 119 return; |
| 117 } | 120 } |
| 118 | 121 |
| 119 int requested_preview_page_index = INVALID_PAGE_INDEX; | 122 int requested_preview_page_index = INVALID_PAGE_INDEX; |
| 120 int page_number = params.page_number; | 123 int page_number = params.page_number; |
| 121 | 124 |
| 122 if (page_number == FIRST_PAGE_INDEX) | 125 if (page_number == FIRST_PAGE_INDEX) |
| 123 print_preview_ui->ClearAllPreviewData(); | 126 print_preview_ui->ClearAllPreviewData(); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 } | 228 } |
| 226 | 229 |
| 227 void PrintPreviewMessageHandler::DidStartLoading() { | 230 void PrintPreviewMessageHandler::DidStartLoading() { |
| 228 if (tab_contents()->delegate() && | 231 if (tab_contents()->delegate() && |
| 229 printing::PrintPreviewTabController::IsPrintPreviewTab(tab_contents())) { | 232 printing::PrintPreviewTabController::IsPrintPreviewTab(tab_contents())) { |
| 230 tab_contents()->SetContentRestrictions(CONTENT_RESTRICTION_PRINT); | 233 tab_contents()->SetContentRestrictions(CONTENT_RESTRICTION_PRINT); |
| 231 } | 234 } |
| 232 } | 235 } |
| 233 | 236 |
| 234 } // namespace printing | 237 } // namespace printing |
| OLD | NEW |