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/single_thread_task_runner.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "content/common/frame_messages.h"
10 #include "content/common/view_messages.h"
11 #include "content/public/renderer/render_process_observer.h"
12 #include "content/renderer/render_view_impl.h"
13 #include "ipc/ipc_message_utils.h"
14 #include "ipc/ipc_sync_message.h"
15 #include "ipc/message_filter.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/WebKit/public/web/WebScriptController.h"
21 MockRenderThread::MockRenderThread()
25 new_window_routing_id_(0),
26 new_window_main_frame_routing_id_(0),
27 new_frame_routing_id_(0) {
30 MockRenderThread::~MockRenderThread() {
31 while (!filters_
.empty()) {
32 scoped_refptr
<IPC::MessageFilter
> filter
= filters_
.back();
34 filter
->OnFilterRemoved();
38 // Called by the Widget. Used to send messages to the browser.
39 // We short-circuit the mechanism and handle the messages right here on this
41 bool MockRenderThread::Send(IPC::Message
* msg
) {
42 // We need to simulate a synchronous channel, thus we are going to receive
43 // through this function messages, messages with reply and reply messages.
44 // We can only handle one synchronous message at a time.
45 if (msg
->is_reply()) {
46 if (reply_deserializer_
) {
47 reply_deserializer_
->SerializeOutputParameters(*msg
);
48 reply_deserializer_
.reset();
52 // We actually need to handle deleting the reply deserializer for sync
54 reply_deserializer_
.reset(
55 static_cast<IPC::SyncMessage
*>(msg
)->GetReplyDeserializer());
57 if (msg
->routing_id() == MSG_ROUTING_CONTROL
)
58 OnControlMessageReceived(*msg
);
60 OnMessageReceived(*msg
);
66 IPC::SyncChannel
* MockRenderThread::GetChannel() {
70 std::string
MockRenderThread::GetLocale() {
74 IPC::SyncMessageFilter
* MockRenderThread::GetSyncMessageFilter() {
78 scoped_refptr
<base::SingleThreadTaskRunner
>
79 MockRenderThread::GetIOMessageLoopProxy() {
80 return scoped_refptr
<base::SingleThreadTaskRunner
>();
83 void MockRenderThread::AddRoute(int32 routing_id
, IPC::Listener
* listener
) {
86 void MockRenderThread::RemoveRoute(int32 routing_id
) {
89 int MockRenderThread::GenerateRoutingID() {
91 return MSG_ROUTING_NONE
;
94 void MockRenderThread::AddFilter(IPC::MessageFilter
* filter
) {
95 filter
->OnFilterAdded(&sink());
96 // Add this filter to a vector so the MockRenderThread::RemoveFilter function
97 // can check if this filter is added.
98 filters_
.push_back(make_scoped_refptr(filter
));
101 void MockRenderThread::RemoveFilter(IPC::MessageFilter
* filter
) {
102 // Emulate the IPC::ChannelProxy::OnRemoveFilter function.
103 for (size_t i
= 0; i
< filters_
.size(); ++i
) {
104 if (filters_
[i
].get() == filter
) {
105 filter
->OnFilterRemoved();
106 filters_
.erase(filters_
.begin() + i
);
110 NOTREACHED() << "filter to be removed not found";
113 void MockRenderThread::AddObserver(RenderProcessObserver
* observer
) {
114 observers_
.AddObserver(observer
);
117 void MockRenderThread::RemoveObserver(RenderProcessObserver
* observer
) {
118 observers_
.RemoveObserver(observer
);
121 void MockRenderThread::SetResourceDispatcherDelegate(
122 ResourceDispatcherDelegate
* delegate
) {
125 void MockRenderThread::EnsureWebKitInitialized() {
128 void MockRenderThread::RecordAction(const base::UserMetricsAction
& action
) {
131 void MockRenderThread::RecordComputedAction(const std::string
& action
) {
134 scoped_ptr
<base::SharedMemory
>
135 MockRenderThread::HostAllocateSharedMemoryBuffer(
136 size_t buffer_size
) {
137 scoped_ptr
<base::SharedMemory
> shared_buf(new base::SharedMemory
);
138 if (!shared_buf
->CreateAnonymous(buffer_size
)) {
139 NOTREACHED() << "Cannot map shared memory buffer";
140 return scoped_ptr
<base::SharedMemory
>();
143 return scoped_ptr
<base::SharedMemory
>(shared_buf
.release());
146 cc::SharedBitmapManager
* MockRenderThread::GetSharedBitmapManager() {
147 return &shared_bitmap_manager_
;
150 void MockRenderThread::RegisterExtension(v8::Extension
* extension
) {
151 blink::WebScriptController::registerExtension(extension
);
154 void MockRenderThread::ScheduleIdleHandler(int64 initial_delay_ms
) {
157 void MockRenderThread::IdleHandler() {
160 int64
MockRenderThread::GetIdleNotificationDelayInMs() const {
164 void MockRenderThread::SetIdleNotificationDelayInMs(
165 int64 idle_notification_delay_in_ms
) {
168 void MockRenderThread::UpdateHistograms(int sequence_number
) {
171 int MockRenderThread::PostTaskToAllWebWorkers(const base::Closure
& closure
) {
175 bool MockRenderThread::ResolveProxy(const GURL
& url
, std::string
* proxy_list
) {
179 base::WaitableEvent
* MockRenderThread::GetShutdownEvent() {
184 void MockRenderThread::PreCacheFont(const LOGFONT
& log_font
) {
187 void MockRenderThread::ReleaseCachedFonts() {
192 IPC::AttachmentBroker
* MockRenderThread::GetAttachmentBroker() {
196 ServiceRegistry
* MockRenderThread::GetServiceRegistry() {
200 void MockRenderThread::SendCloseMessage() {
201 ViewMsg_Close
msg(routing_id_
);
202 RenderViewImpl::FromRoutingID(routing_id_
)->OnMessageReceived(msg
);
205 // The Widget expects to be returned valid route_id.
206 void MockRenderThread::OnCreateWidget(int opener_id
,
207 blink::WebPopupType popup_type
,
210 opener_id_
= opener_id
;
211 *route_id
= routing_id_
;
212 *surface_id
= surface_id_
;
215 // The View expects to be returned a valid route_id different from its own.
216 void MockRenderThread::OnCreateWindow(
217 const ViewHostMsg_CreateWindow_Params
& params
,
219 int* main_frame_route_id
,
221 int64
* cloned_session_storage_namespace_id
) {
222 *route_id
= new_window_routing_id_
;
223 *main_frame_route_id
= new_window_main_frame_routing_id_
;
224 *surface_id
= surface_id_
;
225 *cloned_session_storage_namespace_id
= 0;
228 // The Frame expects to be returned a valid route_id different from its own.
229 void MockRenderThread::OnCreateChildFrame(int new_frame_routing_id
,
230 blink::WebTreeScopeType scope
,
231 const std::string
& frame_name
,
232 blink::WebSandboxFlags sandbox_flags
,
233 int* new_render_frame_id
) {
234 *new_render_frame_id
= new_frame_routing_id_
++;
237 bool MockRenderThread::OnControlMessageReceived(const IPC::Message
& msg
) {
238 base::ObserverListBase
<RenderProcessObserver
>::Iterator
it(&observers_
);
239 RenderProcessObserver
* observer
;
240 while ((observer
= it
.GetNext()) != NULL
) {
241 if (observer
->OnControlMessageReceived(msg
))
244 return OnMessageReceived(msg
);
247 bool MockRenderThread::OnMessageReceived(const IPC::Message
& msg
) {
248 // Save the message in the sink.
249 sink_
.OnMessageReceived(msg
);
252 IPC_BEGIN_MESSAGE_MAP(MockRenderThread
, msg
)
253 IPC_MESSAGE_HANDLER(ViewHostMsg_CreateWidget
, OnCreateWidget
)
254 IPC_MESSAGE_HANDLER(ViewHostMsg_CreateWindow
, OnCreateWindow
)
255 IPC_MESSAGE_HANDLER(FrameHostMsg_CreateChildFrame
, OnCreateChildFrame
)
256 IPC_MESSAGE_UNHANDLED(handled
= false)
257 IPC_END_MESSAGE_MAP()
262 void MockRenderThread::OnDuplicateSection(
263 base::SharedMemoryHandle renderer_handle
,
264 base::SharedMemoryHandle
* browser_handle
) {
265 // We don't have to duplicate the input handles since RenderViewTest does not
266 // separate a browser process from a renderer process.
267 *browser_handle
= renderer_handle
;
269 #endif // defined(OS_WIN)
271 } // namespace content