DevTools: allow attaching/detaching DevToolsAgentHosts
[chromium-blink-merge.git] / chrome / renderer / mock_render_thread.cc
blobe36047ebd1f33d1c4a392323095350761aa64993
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
3 // found in the LICENSE file.
5 #include "chrome/renderer/mock_render_thread.h"
7 #include <fcntl.h>
9 #include "base/file_util.h"
10 #include "base/process_util.h"
11 #include "chrome/common/extensions/extension_messages.h"
12 #include "chrome/common/print_messages.h"
13 #include "chrome/common/render_messages.h"
14 #include "chrome/common/url_constants.h"
15 #include "content/common/view_messages.h"
16 #include "ipc/ipc_message_utils.h"
17 #include "ipc/ipc_sync_message.h"
18 #include "printing/print_job_constants.h"
19 #include "printing/page_range.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 MockRenderThread::MockRenderThread()
23 : routing_id_(0),
24 opener_id_(0),
25 widget_(NULL),
26 reply_deserializer_(NULL),
27 printer_(new MockPrinter),
28 print_dialog_user_response_(true),
29 print_preview_cancel_page_number_(-1),
30 print_preview_pages_remaining_(0) {
33 MockRenderThread::~MockRenderThread() {
36 // Called by the Widget. Used to send messages to the browser.
37 // We short-circuit the mechanism and handle the messages right here on this
38 // class.
39 bool MockRenderThread::Send(IPC::Message* msg) {
40 // We need to simulate a synchronous channel, thus we are going to receive
41 // through this function messages, messages with reply and reply messages.
42 // We can only handle one synchronous message at a time.
43 if (msg->is_reply()) {
44 if (reply_deserializer_.get()) {
45 reply_deserializer_->SerializeOutputParameters(*msg);
46 reply_deserializer_.reset();
48 } else {
49 if (msg->is_sync()) {
50 // We actually need to handle deleting the reply deserializer for sync
51 // messages.
52 reply_deserializer_.reset(
53 static_cast<IPC::SyncMessage*>(msg)->GetReplyDeserializer());
55 OnMessageReceived(*msg);
57 delete msg;
58 return true;
61 MessageLoop* MockRenderThread::GetMessageLoop() {
62 return NULL;
65 IPC::SyncChannel* MockRenderThread::GetChannel() {
66 return NULL;
69 ResourceDispatcher* MockRenderThread::GetResourceDispatcher() {
70 return NULL;
73 std::string MockRenderThread::GetLocale() {
74 return std::string();
77 void MockRenderThread::AddRoute(int32 routing_id,
78 IPC::Channel::Listener* listener) {
79 EXPECT_EQ(routing_id_, routing_id);
80 widget_ = listener;
83 void MockRenderThread::RemoveRoute(int32 routing_id) {
84 EXPECT_EQ(routing_id_, routing_id);
85 widget_ = NULL;
88 void MockRenderThread::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
89 filter->OnFilterAdded(&sink());
92 void MockRenderThread::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
93 filter->OnFilterRemoved();
96 void MockRenderThread::SetOutgoingMessageFilter(
97 IPC::ChannelProxy::OutgoingMessageFilter* filter) {
100 void MockRenderThread::AddObserver(content::RenderProcessObserver* observer) {
103 void MockRenderThread::RemoveObserver(
104 content::RenderProcessObserver* observer) {
107 void MockRenderThread::WidgetHidden() {
110 void MockRenderThread::WidgetRestored() {
113 void MockRenderThread::EnsureWebKitInitialized() {
116 void MockRenderThread::RecordUserMetrics(const std::string& action) {
119 base::SharedMemoryHandle MockRenderThread::HostAllocateSharedMemoryBuffer(
120 uint32 buffer_size) {
121 base::SharedMemory shared_buf;
122 if (!shared_buf.CreateAndMapAnonymous(buffer_size)) {
123 NOTREACHED() << "Cannot map shared memory buffer";
124 return base::SharedMemory::NULLHandle();
126 base::SharedMemoryHandle handle;
127 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), &handle);
128 return handle;
131 void MockRenderThread::RegisterExtension(v8::Extension* extension) {
134 bool MockRenderThread::IsRegisteredExtension(
135 const std::string& v8_extension_name) const {
136 return false;
139 void MockRenderThread::ScheduleIdleHandler(double initial_delay_s) {
142 void MockRenderThread::IdleHandler() {
145 double MockRenderThread::GetIdleNotificationDelayInS() const {
146 return 0.0;
149 void MockRenderThread::SetIdleNotificationDelayInS(
150 double idle_notification_delay_in_s) {
153 #if defined(OS_WIN)
154 void MockRenderThread::PreCacheFont(const LOGFONT& log_font) {
157 void MockRenderThread::ReleaseCachedFonts() {
160 #endif // OS_WIN
162 void MockRenderThread::SendCloseMessage() {
163 ViewMsg_Close msg(routing_id_);
164 widget_->OnMessageReceived(msg);
167 bool MockRenderThread::OnMessageReceived(const IPC::Message& msg) {
168 // Save the message in the sink.
169 sink_.OnMessageReceived(msg);
171 // Some messages we do special handling.
172 bool handled = true;
173 bool msg_is_ok = true;
174 IPC_BEGIN_MESSAGE_MAP_EX(MockRenderThread, msg, msg_is_ok)
175 IPC_MESSAGE_HANDLER(ViewHostMsg_CreateWidget, OnMsgCreateWidget)
176 IPC_MESSAGE_HANDLER(ExtensionHostMsg_OpenChannelToExtension,
177 OnMsgOpenChannelToExtension)
178 IPC_MESSAGE_HANDLER(PrintHostMsg_GetDefaultPrintSettings,
179 OnGetDefaultPrintSettings)
180 IPC_MESSAGE_HANDLER(PrintHostMsg_ScriptedPrint, OnScriptedPrint)
181 IPC_MESSAGE_HANDLER(PrintHostMsg_UpdatePrintSettings, OnUpdatePrintSettings)
182 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPrintedPagesCount,
183 OnDidGetPrintedPagesCount)
184 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage)
185 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount,
186 OnDidGetPreviewPageCount)
187 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPreviewPage, OnDidPreviewPage)
188 IPC_MESSAGE_HANDLER(PrintHostMsg_CheckForCancel, OnCheckForCancel)
189 #if defined(OS_WIN)
190 IPC_MESSAGE_HANDLER(PrintHostMsg_DuplicateSection, OnDuplicateSection)
191 #endif
192 #if defined(OS_CHROMEOS)
193 IPC_MESSAGE_HANDLER(PrintHostMsg_AllocateTempFileForPrinting,
194 OnAllocateTempFileForPrinting)
195 IPC_MESSAGE_HANDLER(PrintHostMsg_TempFileForPrintingWritten,
196 OnTempFileForPrintingWritten)
197 #endif
198 IPC_MESSAGE_UNHANDLED(handled = false)
199 IPC_END_MESSAGE_MAP_EX()
200 return handled;
203 // The Widget expects to be returned valid route_id.
204 void MockRenderThread::OnMsgCreateWidget(int opener_id,
205 WebKit::WebPopupType popup_type,
206 int* route_id) {
207 opener_id_ = opener_id;
208 *route_id = routing_id_;
211 void MockRenderThread::OnMsgOpenChannelToExtension(
212 int routing_id, const std::string& source_extension_id,
213 const std::string& target_extension_id,
214 const std::string& channel_name, int* port_id) {
215 *port_id = 0;
218 #if defined(OS_WIN)
219 void MockRenderThread::OnDuplicateSection(
220 base::SharedMemoryHandle renderer_handle,
221 base::SharedMemoryHandle* browser_handle) {
222 // We don't have to duplicate the input handles since RenderViewTest does not
223 // separate a browser process from a renderer process.
224 *browser_handle = renderer_handle;
226 #endif // defined(OS_WIN)
228 #if defined(OS_CHROMEOS)
229 void MockRenderThread::OnAllocateTempFileForPrinting(
230 base::FileDescriptor* renderer_fd,
231 int* browser_fd) {
232 renderer_fd->fd = *browser_fd = -1;
233 renderer_fd->auto_close = false;
235 FilePath path;
236 if (file_util::CreateTemporaryFile(&path)) {
237 int fd = open(path.value().c_str(), O_WRONLY);
238 DCHECK_GE(fd, 0);
239 renderer_fd->fd = *browser_fd = fd;
243 void MockRenderThread::OnTempFileForPrintingWritten(int browser_fd) {
244 close(browser_fd);
246 #endif // defined(OS_CHROMEOS)
248 void MockRenderThread::OnGetDefaultPrintSettings(
249 PrintMsg_Print_Params* params) {
250 if (printer_.get())
251 printer_->GetDefaultPrintSettings(params);
254 void MockRenderThread::OnScriptedPrint(
255 const PrintHostMsg_ScriptedPrint_Params& params,
256 PrintMsg_PrintPages_Params* settings) {
257 if (print_dialog_user_response_ && printer_.get()) {
258 printer_->ScriptedPrint(params.cookie,
259 params.expected_pages_count,
260 params.has_selection,
261 settings);
265 void MockRenderThread::OnDidGetPrintedPagesCount(int cookie, int number_pages) {
266 if (printer_.get())
267 printer_->SetPrintedPagesCount(cookie, number_pages);
270 void MockRenderThread::OnDidPrintPage(
271 const PrintHostMsg_DidPrintPage_Params& params) {
272 if (printer_.get())
273 printer_->PrintPage(params);
276 void MockRenderThread::OnDidGetPreviewPageCount(
277 const PrintHostMsg_DidGetPreviewPageCount_Params& params) {
278 print_preview_pages_remaining_ = params.page_count;
281 void MockRenderThread::OnDidPreviewPage(
282 const PrintHostMsg_DidPreviewPage_Params& params) {
283 DCHECK(params.page_number >= printing::FIRST_PAGE_INDEX);
284 print_preview_pages_remaining_--;
287 void MockRenderThread::OnCheckForCancel(const std::string& preview_ui_addr,
288 int preview_request_id,
289 bool* cancel) {
290 *cancel =
291 (print_preview_pages_remaining_ == print_preview_cancel_page_number_);
294 void MockRenderThread::OnUpdatePrintSettings(
295 int document_cookie,
296 const base::DictionaryValue& job_settings,
297 PrintMsg_PrintPages_Params* params) {
298 // Check and make sure the required settings are all there.
299 // We don't actually care about the values.
300 std::string dummy_string;
301 if (!job_settings.GetBoolean(printing::kSettingLandscape, NULL) ||
302 !job_settings.GetBoolean(printing::kSettingCollate, NULL) ||
303 !job_settings.GetInteger(printing::kSettingColor, NULL) ||
304 !job_settings.GetBoolean(printing::kSettingPrintToPDF, NULL) ||
305 !job_settings.GetBoolean(printing::kIsFirstRequest, NULL) ||
306 !job_settings.GetString(printing::kSettingDeviceName, &dummy_string) ||
307 !job_settings.GetInteger(printing::kSettingDuplexMode, NULL) ||
308 !job_settings.GetInteger(printing::kSettingCopies, NULL) ||
309 !job_settings.GetString(printing::kPreviewUIAddr, &dummy_string) ||
310 !job_settings.GetInteger(printing::kPreviewRequestID, NULL)) {
311 return;
314 // Just return the default settings.
315 if (printer_.get()) {
316 ListValue* page_range_array;
317 printing::PageRanges new_ranges;
318 if (job_settings.GetList(printing::kSettingPageRange, &page_range_array)) {
319 for (size_t index = 0; index < page_range_array->GetSize(); ++index) {
320 base::DictionaryValue* dict;
321 if (!page_range_array->GetDictionary(index, &dict))
322 continue;
323 printing::PageRange range;
324 if (!dict->GetInteger(printing::kSettingPageRangeFrom, &range.from) ||
325 !dict->GetInteger(printing::kSettingPageRangeTo, &range.to)) {
326 continue;
328 // Page numbers are 1-based in the dictionary.
329 // Page numbers are 0-based for the printing context.
330 range.from--;
331 range.to--;
332 new_ranges.push_back(range);
335 std::vector<int> pages(printing::PageRange::GetPages(new_ranges));
336 printer_->UpdateSettings(document_cookie, params, pages);
340 void MockRenderThread::set_print_dialog_user_response(bool response) {
341 print_dialog_user_response_ = response;
344 void MockRenderThread::set_print_preview_cancel_page_number(int page) {
345 print_preview_cancel_page_number_ = page;
348 int MockRenderThread::print_preview_pages_remaining() {
349 return print_preview_pages_remaining_;