| 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/ui/webui/print_preview_ui.h" | 5 #include "chrome/browser/ui/webui/print_preview_ui.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram.h" |
| 8 #include "base/string_util.h" |
| 7 #include "base/values.h" | 9 #include "base/values.h" |
| 8 #include "base/metrics/histogram.h" | 10 #include "chrome/browser/printing/print_preview_data_service.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/webui/print_preview_data_source.h" |
| 10 #include "chrome/browser/ui/webui/print_preview_handler.h" | 13 #include "chrome/browser/ui/webui/print_preview_handler.h" |
| 11 #include "chrome/browser/ui/webui/print_preview_ui_html_source.h" | |
| 12 #include "content/browser/browser_thread.h" | |
| 13 #include "content/browser/tab_contents/tab_contents.h" | 14 #include "content/browser/tab_contents/tab_contents.h" |
| 14 | 15 |
| 15 PrintPreviewUI::PrintPreviewUI(TabContents* contents) | 16 PrintPreviewUI::PrintPreviewUI(TabContents* contents) |
| 16 : WebUI(contents), | 17 : WebUI(contents), |
| 17 html_source_(new PrintPreviewUIHTMLSource()), | |
| 18 initial_preview_start_time_(base::TimeTicks::Now()) { | 18 initial_preview_start_time_(base::TimeTicks::Now()) { |
| 19 // PrintPreviewUI owns |handler|. | 19 // PrintPreviewUI owns |handler|. |
| 20 PrintPreviewHandler* handler = new PrintPreviewHandler(); | 20 PrintPreviewHandler* handler = new PrintPreviewHandler(); |
| 21 AddMessageHandler(handler->Attach(this)); | 21 AddMessageHandler(handler->Attach(this)); |
| 22 | 22 |
| 23 // Set up the chrome://print/ source. | 23 // Set up the chrome://print/ data source. |
| 24 contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source_); | 24 contents->profile()->GetChromeURLDataManager()->AddDataSource( |
| 25 new PrintPreviewDataSource()); |
| 26 |
| 27 // Store the PrintPreviewUIAddress as a string. |
| 28 // "0x" + deadc0de + '\0' = 2 + 2 * sizeof(this) + 1; |
| 29 char preview_ui_addr[2 + (2 * sizeof(this)) + 1]; |
| 30 base::snprintf(preview_ui_addr, sizeof(preview_ui_addr), "%p", this); |
| 31 preview_ui_addr_str_ = preview_ui_addr; |
| 25 } | 32 } |
| 26 | 33 |
| 27 PrintPreviewUI::~PrintPreviewUI() { | 34 PrintPreviewUI::~PrintPreviewUI() { |
| 35 print_preview_data_service()->RemoveEntry(preview_ui_addr_str_); |
| 28 } | 36 } |
| 29 | 37 |
| 30 PrintPreviewUIHTMLSource* PrintPreviewUI::html_source() { | 38 void PrintPreviewUI::GetPrintPreviewData(scoped_refptr<RefCountedBytes>* data) { |
| 31 return html_source_.get(); | 39 print_preview_data_service()->GetDataEntry(preview_ui_addr_str_, data); |
| 40 } |
| 41 |
| 42 void PrintPreviewUI::SetPrintPreviewData(const RefCountedBytes* data) { |
| 43 print_preview_data_service()->SetDataEntry(preview_ui_addr_str_, data); |
| 32 } | 44 } |
| 33 | 45 |
| 34 void PrintPreviewUI::OnInitiatorTabClosed( | 46 void PrintPreviewUI::OnInitiatorTabClosed( |
| 35 const std::string& initiator_url) { | 47 const std::string& initiator_url) { |
| 36 StringValue initiator_tab_url(initiator_url); | 48 StringValue initiator_tab_url(initiator_url); |
| 37 CallJavascriptFunction("onInitiatorTabClosed", initiator_tab_url); | 49 CallJavascriptFunction("onInitiatorTabClosed", initiator_tab_url); |
| 38 } | 50 } |
| 39 | 51 |
| 40 void PrintPreviewUI::OnPreviewDataIsAvailable(int expected_pages_count, | 52 void PrintPreviewUI::OnPreviewDataIsAvailable(int expected_pages_count, |
| 41 const string16& job_title, | 53 const string16& job_title, |
| 42 bool modifiable) { | 54 bool modifiable) { |
| 43 VLOG(1) << "Print preview request finished with " | 55 VLOG(1) << "Print preview request finished with " |
| 44 << expected_pages_count << " pages"; | 56 << expected_pages_count << " pages"; |
| 45 if (!initial_preview_start_time_.is_null()) { | 57 if (!initial_preview_start_time_.is_null()) { |
| 46 UMA_HISTOGRAM_TIMES("PrintPreview.InitalDisplayTime", | 58 UMA_HISTOGRAM_TIMES("PrintPreview.InitalDisplayTime", |
| 47 base::TimeTicks::Now() - initial_preview_start_time_); | 59 base::TimeTicks::Now() - initial_preview_start_time_); |
| 48 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.Initial", | 60 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.Initial", |
| 49 expected_pages_count); | 61 expected_pages_count); |
| 50 initial_preview_start_time_ = base::TimeTicks(); | 62 initial_preview_start_time_ = base::TimeTicks(); |
| 51 } | 63 } |
| 52 FundamentalValue pages_count(expected_pages_count); | 64 FundamentalValue pages_count(expected_pages_count); |
| 53 StringValue title(job_title); | 65 StringValue title(job_title); |
| 54 FundamentalValue is_preview_modifiable(modifiable); | 66 FundamentalValue is_preview_modifiable(modifiable); |
| 67 StringValue ui_identifier(preview_ui_addr_str_); |
| 55 CallJavascriptFunction("updatePrintPreview", pages_count, title, | 68 CallJavascriptFunction("updatePrintPreview", pages_count, title, |
| 56 is_preview_modifiable); | 69 is_preview_modifiable, ui_identifier); |
| 57 } | 70 } |
| 71 |
| 72 PrintPreviewDataService* PrintPreviewUI::print_preview_data_service() { |
| 73 return PrintPreviewDataService::GetInstance(); |
| 74 } |
| OLD | NEW |