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 #ifndef CONTENT_PUBLIC_TEST_MOCK_RENDER_THREAD_H_
6 #define CONTENT_PUBLIC_TEST_MOCK_RENDER_THREAD_H_
8 #include "base/memory/shared_memory.h"
9 #include "base/observer_list.h"
10 #include "base/strings/string16.h"
11 #include "cc/test/test_shared_bitmap_manager.h"
12 #include "content/public/renderer/render_thread.h"
13 #include "ipc/ipc_test_sink.h"
14 #include "ipc/message_filter.h"
15 #include "third_party/WebKit/public/web/WebPopupType.h"
17 struct ViewHostMsg_CreateWindow_Params
;
21 class MessageReplyDeserializer
;
26 enum class SandboxFlags
;
28 // This class is a very simple mock of RenderThread. It simulates an IPC channel
29 // which supports only three messages:
30 // ViewHostMsg_CreateWidget : sync message sent by the Widget.
31 // ViewHostMsg_CreateWindow : sync message sent by the Widget.
32 // ViewMsg_Close : async, send to the Widget.
33 class MockRenderThread
: public RenderThread
{
36 ~MockRenderThread() override
;
38 // Provides access to the messages that have been received by this thread.
39 IPC::TestSink
& sink() { return sink_
; }
41 // RenderThread implementation:
42 bool Send(IPC::Message
* msg
) override
;
43 scoped_refptr
<base::SingleThreadTaskRunner
> GetTaskRunner() override
;
44 IPC::SyncChannel
* GetChannel() override
;
45 std::string
GetLocale() override
;
46 IPC::SyncMessageFilter
* GetSyncMessageFilter() override
;
47 scoped_refptr
<base::MessageLoopProxy
> GetIOMessageLoopProxy() override
;
48 void AddRoute(int32 routing_id
, IPC::Listener
* listener
) override
;
49 void RemoveRoute(int32 routing_id
) override
;
50 int GenerateRoutingID() override
;
51 void AddFilter(IPC::MessageFilter
* filter
) override
;
52 void RemoveFilter(IPC::MessageFilter
* filter
) override
;
53 void AddObserver(RenderProcessObserver
* observer
) override
;
54 void RemoveObserver(RenderProcessObserver
* observer
) override
;
55 void SetResourceDispatcherDelegate(
56 ResourceDispatcherDelegate
* delegate
) override
;
57 void EnsureWebKitInitialized() override
;
58 void RecordAction(const base::UserMetricsAction
& action
) override
;
59 void RecordComputedAction(const std::string
& action
) override
;
60 scoped_ptr
<base::SharedMemory
> HostAllocateSharedMemoryBuffer(
61 size_t buffer_size
) override
;
62 cc::SharedBitmapManager
* GetSharedBitmapManager() override
;
63 void RegisterExtension(v8::Extension
* extension
) override
;
64 void ScheduleIdleHandler(int64 initial_delay_ms
) override
;
65 void IdleHandler() override
;
66 int64
GetIdleNotificationDelayInMs() const override
;
67 void SetIdleNotificationDelayInMs(
68 int64 idle_notification_delay_in_ms
) override
;
69 void UpdateHistograms(int sequence_number
) override
;
70 int PostTaskToAllWebWorkers(const base::Closure
& closure
) override
;
71 bool ResolveProxy(const GURL
& url
, std::string
* proxy_list
) override
;
72 base::WaitableEvent
* GetShutdownEvent() override
;
74 virtual void PreCacheFont(const LOGFONT
& log_font
) override
;
75 virtual void ReleaseCachedFonts() override
;
77 ServiceRegistry
* GetServiceRegistry() override
;
79 //////////////////////////////////////////////////////////////////////////
80 // The following functions are called by the test itself.
82 void set_routing_id(int32 id
) {
86 void set_surface_id(int32 id
) {
90 int32
opener_id() const {
94 void set_new_window_routing_id(int32 id
) {
95 new_window_routing_id_
= id
;
98 void set_new_frame_routing_id(int32 id
) {
99 new_frame_routing_id_
= id
;
102 // Simulates the Widget receiving a close message. This should result
103 // on releasing the internal reference counts and destroying the internal
105 void SendCloseMessage();
107 // Dispatches control messages to observers.
108 bool OnControlMessageReceived(const IPC::Message
& msg
);
110 ObserverList
<RenderProcessObserver
>& observers() {
115 // This function operates as a regular IPC listener. Subclasses
116 // overriding this should first delegate to this implementation.
117 virtual bool OnMessageReceived(const IPC::Message
& msg
);
119 // The Widget expects to be returned valid route_id.
120 void OnCreateWidget(int opener_id
,
121 blink::WebPopupType popup_type
,
125 // The View expects to be returned a valid route_id different from its own.
126 // We do not keep track of the newly created widget in MockRenderThread,
127 // so it must be cleaned up on its own.
129 const ViewHostMsg_CreateWindow_Params
& params
,
131 int* main_frame_route_id
,
133 int64
* cloned_session_storage_namespace_id
);
135 // The Frame expects to be returned a valid route_id different from its own.
136 void OnCreateChildFrame(int new_frame_routing_id
,
137 const std::string
& frame_name
,
138 SandboxFlags sandbox_flags
,
139 int* new_render_frame_id
);
142 void OnDuplicateSection(base::SharedMemoryHandle renderer_handle
,
143 base::SharedMemoryHandle
* browser_handle
);
148 // Routing id what will be assigned to the Widget.
151 // Surface id what will be assigned to the Widget.
154 // Opener id reported by the Widget.
157 // Routing id that will be assigned to a CreateWindow Widget.
158 int32 new_window_routing_id_
;
159 int32 new_window_main_frame_routing_id_
;
160 int32 new_frame_routing_id_
;
162 // The last known good deserializer for sync messages.
163 scoped_ptr
<IPC::MessageReplyDeserializer
> reply_deserializer_
;
165 // A list of message filters added to this thread.
166 std::vector
<scoped_refptr
<IPC::MessageFilter
> > filters_
;
168 // Observers to notify.
169 ObserverList
<RenderProcessObserver
> observers_
;
171 cc::TestSharedBitmapManager shared_bitmap_manager_
;
174 } // namespace content
176 #endif // CONTENT_PUBLIC_TEST_MOCK_RENDER_THREAD_H_