Chromium Code Reviews| Index: chrome/browser/dom_ui/print_preview_ui.cc |
| =================================================================== |
| --- chrome/browser/dom_ui/print_preview_ui.cc (revision 72691) |
| +++ chrome/browser/dom_ui/print_preview_ui.cc (working copy) |
| @@ -5,8 +5,11 @@ |
| #include "chrome/browser/dom_ui/print_preview_ui.h" |
| #include <algorithm> |
| +#include <string> |
| +#include <vector> |
| #include "base/message_loop.h" |
| +#include "base/shared_memory.h" |
| #include "base/singleton.h" |
| #include "base/string_piece.h" |
| #include "base/values.h" |
| @@ -66,7 +69,7 @@ |
| class PrintPreviewUIHTMLSource : public ChromeURLDataManager::DataSource { |
| public: |
| - PrintPreviewUIHTMLSource(); |
| + explicit PrintPreviewUIHTMLSource(PrintPreviewHandler* handler); |
| virtual ~PrintPreviewUIHTMLSource(); |
| // Called when the network layer has requested a resource underneath |
| @@ -77,11 +80,14 @@ |
| virtual std::string GetMimeType(const std::string&) const; |
| private: |
| + PrintPreviewHandler* handler_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(PrintPreviewUIHTMLSource); |
| }; |
| -PrintPreviewUIHTMLSource::PrintPreviewUIHTMLSource() |
| - : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()) { |
| +PrintPreviewUIHTMLSource::PrintPreviewUIHTMLSource(PrintPreviewHandler* handler) |
|
James Hawkins
2011/01/31 18:39:38
After reading through the flow, it seems the best
|
| + : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()), |
| + handler_(handler) { |
| } |
| PrintPreviewUIHTMLSource::~PrintPreviewUIHTMLSource() {} |
| @@ -89,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); |
| @@ -107,12 +113,27 @@ |
| SendResponse(request_id, html_bytes); |
| return; |
| + } else if (path == "print.pdf") { |
| + // Print Preview data. |
| + PrintPreviewHandler::PrintPreviewData data; |
| + handler_->GetPrintPreviewData(&data); |
| + |
| + 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); |
| + return; |
| + } else { |
| + // Invalid request. |
| + scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes); |
| + SendResponse(request_id, empty_bytes); |
| + return; |
| } |
| - |
| - // Print Preview data. |
| - NOTIMPLEMENTED() << "No PDF for you"; |
| - scoped_refptr<RefCountedBytes> data_bytes(new RefCountedBytes); |
| - SendResponse(request_id, data_bytes); |
| } |
| std::string PrintPreviewUIHTMLSource::GetMimeType( |
| @@ -132,8 +153,8 @@ |
| PrintPreviewUI::PrintPreviewUI(TabContents* contents) : DOMUI(contents) { |
| // PrintPreviewUI owns |handler|. |
| - PrintPreviewHandler* handler = new PrintPreviewHandler(); |
| - AddMessageHandler(handler->Attach(this)); |
| + handler_ = new PrintPreviewHandler(); |
| + AddMessageHandler(handler_->Attach(this)); |
| // Set up the chrome://print/ source. |
| BrowserThread::PostTask( |
| @@ -141,8 +162,12 @@ |
| NewRunnableMethod( |
| ChromeURLDataManager::GetInstance(), |
| &ChromeURLDataManager::AddDataSource, |
| - make_scoped_refptr(new PrintPreviewUIHTMLSource()))); |
| + make_scoped_refptr(new PrintPreviewUIHTMLSource(handler_)))); |
| } |
| PrintPreviewUI::~PrintPreviewUI() { |
| } |
| + |
| +PrintPreviewHandler* PrintPreviewUI::handler() { |
| + return handler_; |
| +} |