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.
5 #include "content/public/test/mock_render_thread.h"
7 #include "base/message_loop/message_loop_proxy.h"
8 #include "content/common/frame_messages.h"
9 #include "content/common/view_messages.h"
10 #include "content/public/renderer/render_process_observer.h"
11 #include "content/renderer/render_view_impl.h"
12 #include "ipc/ipc_message_utils.h"
13 #include "ipc/ipc_sync_message.h"
14 #include "ipc/message_filter.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/WebKit/public/web/WebScriptController.h"
20 MockRenderThread::MockRenderThread()
24 new_window_routing_id_(0),
25 new_window_main_frame_routing_id_(0),
26 new_frame_routing_id_(0) {
29 MockRenderThread::~MockRenderThread() {
30 while (!filters_
.empty()) {
31 scoped_refptr
<IPC::MessageFilter
> filter
= filters_
.back();
33 filter
->OnFilterRemoved();
37 // Called by the Widget. Used to send messages to the browser.
38 // We short-circuit the mechanism and handle the messages right here on this
40 bool MockRenderThread::Send(IPC::Message
* msg
) {
41 // We need to simulate a synchronous channel, thus we are going to receive
42 // through this function messages, messages with reply and reply messages.
43 // We can only handle one synchronous message at a time.
44 if (msg
->is_reply()) {
45 if (reply_deserializer_
) {
46 reply_deserializer_
->SerializeOutputParameters(*msg
);
47 reply_deserializer_
.reset();
51 // We actually need to handle deleting the reply deserializer for sync
53 reply_deserializer_
.reset(
54 static_cast<IPC::SyncMessage
*>(msg
)->GetReplyDeserializer());
56 if (msg
->routing_id() == MSG_ROUTING_CONTROL
)
57 OnControlMessageReceived(*msg
);
59 OnMessageReceived(*msg
);
65 scoped_refptr
<base::SingleThreadTaskRunner
> MockRenderThread::GetTaskRunner() {
66 return scoped_refptr
<base::MessageLoopProxy
>();
69 IPC::SyncChannel
* MockRenderThread::GetChannel() {
73 std::string
MockRenderThread::GetLocale() {
77 IPC::SyncMessageFilter
* MockRenderThread::GetSyncMessageFilter() {
81 scoped_refptr
<base::MessageLoopProxy
>
82 MockRenderThread::GetIOMessageLoopProxy() {
83 return scoped_refptr
<base::MessageLoopProxy
>();
86 void MockRenderThread::AddRoute(int32 routing_id
, IPC::Listener
* listener
) {
89 void MockRenderThread::RemoveRoute(int32 routing_id
) {
92 int MockRenderThread::GenerateRoutingID() {
94 return MSG_ROUTING_NONE
;
97 void MockRenderThread::AddFilter(IPC::MessageFilter
* filter
) {
98 filter
->OnFilterAdded(&sink());
99 // Add this filter to a vector so the MockRenderThread::RemoveFilter function
100 // can check if this filter is added.
101 filters_
.push_back(make_scoped_refptr(filter
));
104 void MockRenderThread::RemoveFilter(IPC::MessageFilter
* filter
) {
105 // Emulate the IPC::ChannelProxy::OnRemoveFilter function.
106 for (size_t i
= 0; i
< filters_
.size(); ++i
) {
107 if (filters_
[i
].get() == filter
) {
108 filter
->OnFilterRemoved();
109 filters_
.erase(filters_
.begin() + i
);
113 NOTREACHED() << "filter to be removed not found";
116 void MockRenderThread::AddObserver(RenderProcessObserver
* observer
) {
117 observers_
.AddObserver(observer
);
120 void MockRenderThread::RemoveObserver(RenderProcessObserver
* observer
) {
121 observers_
.RemoveObserver(observer
);
124 void MockRenderThread::SetResourceDispatcherDelegate(
125 ResourceDispatcherDelegate
* delegate
) {
128 void MockRenderThread::EnsureWebKitInitialized() {
131 void MockRenderThread::RecordAction(const base::UserMetricsAction
& action
) {
134 void MockRenderThread::RecordComputedAction(const std::string
& action
) {
137 scoped_ptr
<base::SharedMemory
>
138 MockRenderThread::HostAllocateSharedMemoryBuffer(
139 size_t buffer_size
) {
140 scoped_ptr
<base::SharedMemory
> shared_buf(new base::SharedMemory
);
141 if (!shared_buf
->CreateAnonymous(buffer_size
)) {
142 NOTREACHED() << "Cannot map shared memory buffer";
143 return scoped_ptr
<base::SharedMemory
>();
146 return scoped_ptr
<base::SharedMemory
>(shared_buf
.release());
149 cc::SharedBitmapManager
* MockRenderThread::GetSharedBitmapManager() {
150 return &shared_bitmap_manager_
;
153 void MockRenderThread::RegisterExtension(v8::Extension
* extension
) {
154 blink::WebScriptController::registerExtension(extension
);
157 void MockRenderThread::ScheduleIdleHandler(int64 initial_delay_ms
) {
160 void MockRenderThread::IdleHandler() {
163 int64
MockRenderThread::GetIdleNotificationDelayInMs() const {
167 void MockRenderThread::SetIdleNotificationDelayInMs(
168 int64 idle_notification_delay_in_ms
) {
171 void MockRenderThread::UpdateHistograms(int sequence_number
) {
174 int MockRenderThread::PostTaskToAllWebWorkers(const base::Closure
& closure
) {
178 bool MockRenderThread::ResolveProxy(const GURL
& url
, std::string
* proxy_list
) {
182 base::WaitableEvent
* MockRenderThread::GetShutdownEvent() {
187 void MockRenderThread::PreCacheFont(const LOGFONT
& log_font
) {
190 void MockRenderThread::ReleaseCachedFonts() {
195 ServiceRegistry
* MockRenderThread::GetServiceRegistry() {
199 void MockRenderThread::SendCloseMessage() {
200 ViewMsg_Close
msg(routing_id_
);
201 RenderViewImpl::FromRoutingID(routing_id_
)->OnMessageReceived(msg
);
204 // The Widget expects to be returned valid route_id.
205 void MockRenderThread::OnCreateWidget(int opener_id
,
206 blink::WebPopupType popup_type
,
209 opener_id_
= opener_id
;
210 *route_id
= routing_id_
;
211 *surface_id
= surface_id_
;
214 // The View expects to be returned a valid route_id different from its own.
215 void MockRenderThread::OnCreateWindow(
216 const ViewHostMsg_CreateWindow_Params
& params
,
218 int* main_frame_route_id
,
220 int64
* cloned_session_storage_namespace_id
) {
221 *route_id
= new_window_routing_id_
;
222 *main_frame_route_id
= new_window_main_frame_routing_id_
;
223 *surface_id
= surface_id_
;
224 *cloned_session_storage_namespace_id
= 0;
227 // The Frame expects to be returned a valid route_id different from its own.
228 void MockRenderThread::OnCreateChildFrame(int new_frame_routing_id
,
229 const std::string
& frame_name
,
230 int* new_render_frame_id
) {
231 *new_render_frame_id
= new_frame_routing_id_
++;
234 bool MockRenderThread::OnControlMessageReceived(const IPC::Message
& msg
) {
235 ObserverListBase
<RenderProcessObserver
>::Iterator
it(observers_
);
236 RenderProcessObserver
* observer
;
237 while ((observer
= it
.GetNext()) != NULL
) {
238 if (observer
->OnControlMessageReceived(msg
))
241 return OnMessageReceived(msg
);
244 bool MockRenderThread::OnMessageReceived(const IPC::Message
& msg
) {
245 // Save the message in the sink.
246 sink_
.OnMessageReceived(msg
);
249 IPC_BEGIN_MESSAGE_MAP(MockRenderThread
, msg
)
250 IPC_MESSAGE_HANDLER(ViewHostMsg_CreateWidget
, OnCreateWidget
)
251 IPC_MESSAGE_HANDLER(ViewHostMsg_CreateWindow
, OnCreateWindow
)
252 IPC_MESSAGE_HANDLER(FrameHostMsg_CreateChildFrame
, OnCreateChildFrame
)
253 IPC_MESSAGE_UNHANDLED(handled
= false)
254 IPC_END_MESSAGE_MAP()
259 void MockRenderThread::OnDuplicateSection(
260 base::SharedMemoryHandle renderer_handle
,
261 base::SharedMemoryHandle
* browser_handle
) {
262 // We don't have to duplicate the input handles since RenderViewTest does not
263 // separate a browser process from a renderer process.
264 *browser_handle
= renderer_handle
;
266 #endif // defined(OS_WIN)
268 } // namespace content