Index: chrome/browser/dom_ui/print_preview_ui.cc |
=================================================================== |
--- chrome/browser/dom_ui/print_preview_ui.cc (revision 71344) |
+++ chrome/browser/dom_ui/print_preview_ui.cc (working copy) |
@@ -1,14 +1,16 @@ |
-// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
#include "chrome/browser/dom_ui/print_preview_ui.h" |
#include <algorithm> |
+#include <vector> |
#include "app/l10n_util.h" |
#include "app/resource_bundle.h" |
#include "base/message_loop.h" |
+#include "base/shared_memory.h" |
#include "base/singleton.h" |
#include "base/string_piece.h" |
#include "base/values.h" |
@@ -16,6 +18,7 @@ |
#include "chrome/browser/dom_ui/chrome_url_data_manager.h" |
#include "chrome/browser/dom_ui/dom_ui_theme_source.h" |
#include "chrome/browser/dom_ui/print_preview_handler.h" |
+#include "chrome/browser/printing/print_preview_manager.h" |
#include "chrome/browser/tab_contents/tab_contents.h" |
#include "chrome/common/jstemplate_builder.h" |
#include "chrome/common/url_constants.h" |
@@ -67,7 +70,7 @@ |
class PrintPreviewUIHTMLSource : public ChromeURLDataManager::DataSource { |
public: |
- PrintPreviewUIHTMLSource(); |
+ explicit PrintPreviewUIHTMLSource(TabContents* contents); |
virtual ~PrintPreviewUIHTMLSource(); |
// Called when the network layer has requested a resource underneath |
@@ -78,11 +81,13 @@ |
virtual std::string GetMimeType(const std::string&) const; |
private: |
+ TabContents* contents_; |
DISALLOW_COPY_AND_ASSIGN(PrintPreviewUIHTMLSource); |
}; |
-PrintPreviewUIHTMLSource::PrintPreviewUIHTMLSource() |
- : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()) { |
+PrintPreviewUIHTMLSource::PrintPreviewUIHTMLSource(TabContents* contents) |
+ : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()), |
+ contents_(contents) { |
} |
PrintPreviewUIHTMLSource::~PrintPreviewUIHTMLSource() {} |
@@ -90,8 +95,8 @@ |
void PrintPreviewUIHTMLSource::StartDataRequest(const std::string& path, |
bool is_off_the_record, |
int request_id) { |
- // Print Preview Index page. |
if (path.empty()) { |
+ // Print Preview Index page. |
DictionaryValue localized_strings; |
SetLocalizedStrings(&localized_strings); |
SetFontAndTextDirection(&localized_strings); |
@@ -108,12 +113,32 @@ |
SendResponse(request_id, html_bytes); |
return; |
+ } else if (path == "print.pdf") { |
+ // Print Preview data. |
+ printing::PrintPreviewManager::PrintPreviewData data; |
+ printing::PrintPreviewManager* print_preview_manager = |
+ printing::PrintPreviewManager::GetInstance(); |
+ if (!print_preview_manager->GetPrintPreviewData(contents_, &data)) { |
+ LOG(ERROR) << "failed to get pdf"; |
+ scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes); |
+ SendResponse(request_id, empty_bytes); |
+ return; |
+ } |
+ |
+ uint32 preview_data_size = data.second; |
+ char* preview_data = reinterpret_cast<char*>(data.first->memory()); |
+ |
+ scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); |
+ html_bytes->data.resize(preview_data_size); |
+ std::vector<unsigned char>::iterator it = html_bytes->data.begin(); |
+ for (uint32 i = 0; i < preview_data_size; ++i, ++it) |
+ *it++ = *(preview_data + i); |
+ SendResponse(request_id, html_bytes); |
+ } else { |
+ // Invalid request. |
+ scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes); |
+ SendResponse(request_id, empty_bytes); |
} |
- |
- // Print Preview data. |
- NOTIMPLEMENTED() << "No PDF for you"; |
- scoped_refptr<RefCountedBytes> data_bytes(new RefCountedBytes); |
- SendResponse(request_id, data_bytes); |
} |
std::string PrintPreviewUIHTMLSource::GetMimeType( |
@@ -142,7 +167,7 @@ |
NewRunnableMethod( |
ChromeURLDataManager::GetInstance(), |
&ChromeURLDataManager::AddDataSource, |
- make_scoped_refptr(new PrintPreviewUIHTMLSource()))); |
+ make_scoped_refptr(new PrintPreviewUIHTMLSource(contents)))); |
} |
PrintPreviewUI::~PrintPreviewUI() { |