Home | History | Annotate | Download | only in renderer
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/renderer/chrome_mock_render_thread.h"
      6 
      7 #include <vector>
      8 
      9 #include "base/values.h"
     10 #include "chrome/common/extensions/extension_messages.h"
     11 #include "chrome/renderer/mock_printer.h"
     12 #include "ipc/ipc_sync_message.h"
     13 #include "printing/print_job_constants.h"
     14 #include "printing/page_range.h"
     15 #include "testing/gtest/include/gtest/gtest.h"
     16 
     17 #if defined(OS_CHROMEOS)
     18 #include <fcntl.h>
     19 
     20 #include "base/file_util.h"
     21 #endif
     22 
     23 #if defined(ENABLE_PRINTING)
     24 #include "chrome/common/print_messages.h"
     25 #endif
     26 
     27 ChromeMockRenderThread::ChromeMockRenderThread()
     28 #if defined(ENABLE_PRINTING)
     29     : printer_(new MockPrinter),
     30       print_dialog_user_response_(true),
     31       print_preview_cancel_page_number_(-1),
     32       print_preview_pages_remaining_(0)
     33 #endif
     34 {
     35 }
     36 
     37 ChromeMockRenderThread::~ChromeMockRenderThread() {
     38 }
     39 
     40 scoped_refptr<base::MessageLoopProxy>
     41 ChromeMockRenderThread::GetIOMessageLoopProxy() {
     42   return io_message_loop_proxy_;
     43 }
     44 
     45 void ChromeMockRenderThread::set_io_message_loop_proxy(
     46     const scoped_refptr<base::MessageLoopProxy>& proxy) {
     47   io_message_loop_proxy_ = proxy;
     48 }
     49 
     50 bool ChromeMockRenderThread::OnMessageReceived(const IPC::Message& msg) {
     51   if (content::MockRenderThread::OnMessageReceived(msg))
     52     return true;
     53 
     54   // Some messages we do special handling.
     55   bool handled = true;
     56   bool msg_is_ok = true;
     57   IPC_BEGIN_MESSAGE_MAP_EX(ChromeMockRenderThread, msg, msg_is_ok)
     58     IPC_MESSAGE_HANDLER(ExtensionHostMsg_OpenChannelToExtension,
     59                         OnOpenChannelToExtension)
     60 #if defined(ENABLE_PRINTING)
     61     IPC_MESSAGE_HANDLER(PrintHostMsg_GetDefaultPrintSettings,
     62                         OnGetDefaultPrintSettings)
     63     IPC_MESSAGE_HANDLER(PrintHostMsg_ScriptedPrint, OnScriptedPrint)
     64     IPC_MESSAGE_HANDLER(PrintHostMsg_UpdatePrintSettings, OnUpdatePrintSettings)
     65     IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPrintedPagesCount,
     66                         OnDidGetPrintedPagesCount)
     67     IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage)
     68     IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount,
     69                         OnDidGetPreviewPageCount)
     70     IPC_MESSAGE_HANDLER(PrintHostMsg_DidPreviewPage, OnDidPreviewPage)
     71     IPC_MESSAGE_HANDLER(PrintHostMsg_CheckForCancel, OnCheckForCancel)
     72 #if defined(OS_WIN)
     73     IPC_MESSAGE_HANDLER(PrintHostMsg_DuplicateSection, OnDuplicateSection)
     74 #endif
     75 #if defined(OS_CHROMEOS)
     76     IPC_MESSAGE_HANDLER(PrintHostMsg_AllocateTempFileForPrinting,
     77                         OnAllocateTempFileForPrinting)
     78     IPC_MESSAGE_HANDLER(PrintHostMsg_TempFileForPrintingWritten,
     79                         OnTempFileForPrintingWritten)
     80 #endif  // defined(OS_CHROMEOS)
     81 #endif  // defined(ENABLE_PRINTING)
     82     IPC_MESSAGE_UNHANDLED(handled = false)
     83   IPC_END_MESSAGE_MAP_EX()
     84   return handled;
     85 }
     86 
     87 void ChromeMockRenderThread::OnOpenChannelToExtension(
     88     int routing_id,
     89     const ExtensionMsg_ExternalConnectionInfo& info,
     90     const std::string& channel_name,
     91     bool include_tls_channel_id,
     92     int* port_id) {
     93   *port_id = 0;
     94 }
     95 
     96 #if defined(ENABLE_PRINTING)
     97 #if defined(OS_CHROMEOS)
     98 void ChromeMockRenderThread::OnAllocateTempFileForPrinting(
     99     int render_view_id,
    100     base::FileDescriptor* renderer_fd,
    101     int* browser_fd) {
    102   renderer_fd->fd = *browser_fd = -1;
    103   renderer_fd->auto_close = false;
    104 
    105   base::FilePath path;
    106   if (base::CreateTemporaryFile(&path)) {
    107     int fd = open(path.value().c_str(), O_WRONLY);
    108     DCHECK_GE(fd, 0);
    109     renderer_fd->fd = *browser_fd = fd;
    110   }
    111 }
    112 
    113 void ChromeMockRenderThread::OnTempFileForPrintingWritten(int render_view_id,
    114                                                           int browser_fd) {
    115   close(browser_fd);
    116 }
    117 #endif  // defined(OS_CHROMEOS)
    118 
    119 void ChromeMockRenderThread::OnGetDefaultPrintSettings(
    120     PrintMsg_Print_Params* params) {
    121   printer_->GetDefaultPrintSettings(params);
    122 }
    123 
    124 void ChromeMockRenderThread::OnScriptedPrint(
    125     const PrintHostMsg_ScriptedPrint_Params& params,
    126     PrintMsg_PrintPages_Params* settings) {
    127   if (print_dialog_user_response_) {
    128     printer_->ScriptedPrint(params.cookie,
    129                             params.expected_pages_count,
    130                             params.has_selection,
    131                             settings);
    132   }
    133 }
    134 
    135 void ChromeMockRenderThread::OnDidGetPrintedPagesCount(
    136     int cookie, int number_pages) {
    137   printer_->SetPrintedPagesCount(cookie, number_pages);
    138 }
    139 
    140 void ChromeMockRenderThread::OnDidPrintPage(
    141     const PrintHostMsg_DidPrintPage_Params& params) {
    142   printer_->PrintPage(params);
    143 }
    144 
    145 void ChromeMockRenderThread::OnDidGetPreviewPageCount(
    146     const PrintHostMsg_DidGetPreviewPageCount_Params& params) {
    147   print_preview_pages_remaining_ = params.page_count;
    148 }
    149 
    150 void ChromeMockRenderThread::OnDidPreviewPage(
    151     const PrintHostMsg_DidPreviewPage_Params& params) {
    152   DCHECK_GE(params.page_number, printing::FIRST_PAGE_INDEX);
    153   print_preview_pages_remaining_--;
    154 }
    155 
    156 void ChromeMockRenderThread::OnCheckForCancel(int32 preview_ui_id,
    157                                               int preview_request_id,
    158                                               bool* cancel) {
    159   *cancel =
    160       (print_preview_pages_remaining_ == print_preview_cancel_page_number_);
    161 }
    162 
    163 void ChromeMockRenderThread::OnUpdatePrintSettings(
    164     int document_cookie,
    165     const base::DictionaryValue& job_settings,
    166     PrintMsg_PrintPages_Params* params) {
    167   // Check and make sure the required settings are all there.
    168   // We don't actually care about the values.
    169   std::string dummy_string;
    170   int margins_type = 0;
    171   if (!job_settings.GetBoolean(printing::kSettingLandscape, NULL) ||
    172       !job_settings.GetBoolean(printing::kSettingCollate, NULL) ||
    173       !job_settings.GetInteger(printing::kSettingColor, NULL) ||
    174       !job_settings.GetBoolean(printing::kSettingPrintToPDF, NULL) ||
    175       !job_settings.GetBoolean(printing::kIsFirstRequest, NULL) ||
    176       !job_settings.GetString(printing::kSettingDeviceName, &dummy_string) ||
    177       !job_settings.GetInteger(printing::kSettingDuplexMode, NULL) ||
    178       !job_settings.GetInteger(printing::kSettingCopies, NULL) ||
    179       !job_settings.GetInteger(printing::kPreviewUIID, NULL) ||
    180       !job_settings.GetInteger(printing::kPreviewRequestID, NULL) ||
    181       !job_settings.GetInteger(printing::kSettingMarginsType, &margins_type)) {
    182     return;
    183   }
    184 
    185   // Just return the default settings.
    186   const ListValue* page_range_array;
    187   printing::PageRanges new_ranges;
    188   if (job_settings.GetList(printing::kSettingPageRange, &page_range_array)) {
    189     for (size_t index = 0; index < page_range_array->GetSize(); ++index) {
    190       const base::DictionaryValue* dict;
    191       if (!page_range_array->GetDictionary(index, &dict))
    192         continue;
    193       printing::PageRange range;
    194       if (!dict->GetInteger(printing::kSettingPageRangeFrom, &range.from) ||
    195           !dict->GetInteger(printing::kSettingPageRangeTo, &range.to)) {
    196         continue;
    197       }
    198       // Page numbers are 1-based in the dictionary.
    199       // Page numbers are 0-based for the printing context.
    200       range.from--;
    201       range.to--;
    202       new_ranges.push_back(range);
    203     }
    204   }
    205   std::vector<int> pages(printing::PageRange::GetPages(new_ranges));
    206   printer_->UpdateSettings(document_cookie, params, pages, margins_type);
    207 
    208   job_settings.GetBoolean(printing::kSettingShouldPrintSelectionOnly,
    209                           &params->params.selection_only);
    210   job_settings.GetBoolean(printing::kSettingShouldPrintBackgrounds,
    211                           &params->params.should_print_backgrounds);
    212 }
    213 
    214 MockPrinter* ChromeMockRenderThread::printer() {
    215   return printer_.get();
    216 }
    217 
    218 void ChromeMockRenderThread::set_print_dialog_user_response(bool response) {
    219   print_dialog_user_response_ = response;
    220 }
    221 
    222 void ChromeMockRenderThread::set_print_preview_cancel_page_number(int page) {
    223   print_preview_cancel_page_number_ = page;
    224 }
    225 
    226 int ChromeMockRenderThread::print_preview_pages_remaining() const {
    227   return print_preview_pages_remaining_;
    228 }
    229 #endif  // defined(ENABLE_PRINTING)
    230