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 scoped_refptr
<base::SingleThreadTaskRunner
> MockRenderThread::GetTaskRunner() {
67 return base::ThreadTaskRunnerHandle::Get();
70 IPC::SyncChannel
* MockRenderThread::GetChannel() {
74 std::string
MockRenderThread::GetLocale() {
78 IPC::SyncMessageFilter
* MockRenderThread::GetSyncMessageFilter() {
82 scoped_refptr
<base::SingleThreadTaskRunner
>
83 MockRenderThread::GetIOMessageLoopProxy() {
84 return scoped_refptr
<base::SingleThreadTaskRunner
>();
87 void MockRenderThread::AddRoute(int32 routing_id
, IPC::Listener
* listener
) {
90 void MockRenderThread::RemoveRoute(int32 routing_id
) {
93 int MockRenderThread::GenerateRoutingID() {
95 return MSG_ROUTING_NONE
;
98 void MockRenderThread::AddFilter(IPC::MessageFilter
* filter
) {
99 filter
->OnFilterAdded(&sink());
100 // Add this filter to a vector so the MockRenderThread::RemoveFilter function
101 // can check if this filter is added.
102 filters_
.push_back(make_scoped_refptr(filter
));
105 void MockRenderThread::RemoveFilter(IPC::MessageFilter
* filter
) {
106 // Emulate the IPC::ChannelProxy::OnRemoveFilter function.
107 for (size_t i
= 0; i
< filters_
.size(); ++i
) {
108 if (filters_
[i
].get() == filter
) {
109 filter
->OnFilterRemoved();
110 filters_
.erase(filters_
.begin() + i
);
114 NOTREACHED() << "filter to be removed not found";
117 void MockRenderThread::AddObserver(RenderProcessObserver
* observer
) {
118 observers_
.AddObserver(observer
);
121 void MockRenderThread::RemoveObserver(RenderProcessObserver
* observer
) {
122 observers_
.RemoveObserver(observer
);
125 void MockRenderThread::SetResourceDispatcherDelegate(
126 ResourceDispatcherDelegate
* delegate
) {
129 void MockRenderThread::EnsureWebKitInitialized() {
132 void MockRenderThread::RecordAction(const base::UserMetricsAction
& action
) {
135 void MockRenderThread::RecordComputedAction(const std::string
& action
) {
138 scoped_ptr
<base::SharedMemory
>
139 MockRenderThread::HostAllocateSharedMemoryBuffer(
140 size_t buffer_size
) {
141 scoped_ptr
<base::SharedMemory
> shared_buf(new base::SharedMemory
);
142 if (!shared_buf
->CreateAnonymous(buffer_size
)) {
143 NOTREACHED() << "Cannot map shared memory buffer";
144 return scoped_ptr
<base::SharedMemory
>();
147 return scoped_ptr
<base::SharedMemory
>(shared_buf
.release());
150 cc::SharedBitmapManager
* MockRenderThread::GetSharedBitmapManager() {
151 return &shared_bitmap_manager_
;
154 void MockRenderThread::RegisterExtension(v8::Extension
* extension
) {
155 blink::WebScriptController::registerExtension(extension
);
158 void MockRenderThread::ScheduleIdleHandler(int64 initial_delay_ms
) {
161 void MockRenderThread::IdleHandler() {
164 int64
MockRenderThread::GetIdleNotificationDelayInMs() const {
168 void MockRenderThread::SetIdleNotificationDelayInMs(
169 int64 idle_notification_delay_in_ms
) {
172 void MockRenderThread::UpdateHistograms(int sequence_number
) {
175 int MockRenderThread::PostTaskToAllWebWorkers(const base::Closure
& closure
) {
179 bool MockRenderThread::ResolveProxy(const GURL
& url
, std::string
* proxy_list
) {
183 base::WaitableEvent
* MockRenderThread::GetShutdownEvent() {
188 void MockRenderThread::PreCacheFont(const LOGFONT
& log_font
) {
191 void MockRenderThread::ReleaseCachedFonts() {
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