blob: c0c70391aa2456c9b5f13f16aa9fdcb4f14edada [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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.
4
5#ifndef CONTENT_PUBLIC_TEST_MOCK_RENDER_THREAD_H_
6#define CONTENT_PUBLIC_TEST_MOCK_RENDER_THREAD_H_
7
Ben Murdochca12bfa2013-07-23 11:17:05 +01008#include "base/memory/shared_memory.h"
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +01009#include "base/observer_list.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010010#include "base/strings/string16.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000011#include "content/public/renderer/render_thread.h"
12#include "ipc/ipc_test_sink.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010013#include "third_party/WebKit/public/web/WebPopupType.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000014
15struct ViewHostMsg_CreateWindow_Params;
16
17namespace IPC {
18class MessageReplyDeserializer;
19}
20
21namespace content {
22
23// This class is a very simple mock of RenderThread. It simulates an IPC channel
24// which supports only three messages:
25// ViewHostMsg_CreateWidget : sync message sent by the Widget.
26// ViewHostMsg_CreateWindow : sync message sent by the Widget.
27// ViewMsg_Close : async, send to the Widget.
28class MockRenderThread : public RenderThread {
29 public:
30 MockRenderThread();
31 virtual ~MockRenderThread();
32
33 // Provides access to the messages that have been received by this thread.
34 IPC::TestSink& sink() { return sink_; }
35
36 // Helpers for embedders to know when content IPC messages are received, since
37 // they don't have access to content IPC files.
38 void VerifyRunJavaScriptMessageSend(const string16& expected_alert_message);
39
40 // RenderThread implementation:
41 virtual bool Send(IPC::Message* msg) OVERRIDE;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010042 virtual base::MessageLoop* GetMessageLoop() OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000043 virtual IPC::SyncChannel* GetChannel() OVERRIDE;
44 virtual std::string GetLocale() OVERRIDE;
45 virtual IPC::SyncMessageFilter* GetSyncMessageFilter() OVERRIDE;
46 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy()
47 OVERRIDE;
48 virtual void AddRoute(int32 routing_id, IPC::Listener* listener) OVERRIDE;
49 virtual void RemoveRoute(int32 routing_id) OVERRIDE;
50 virtual int GenerateRoutingID() OVERRIDE;
51 virtual void AddFilter(IPC::ChannelProxy::MessageFilter* filter) OVERRIDE;
52 virtual void RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) OVERRIDE;
53 virtual void SetOutgoingMessageFilter(
54 IPC::ChannelProxy::OutgoingMessageFilter* filter) OVERRIDE;
55 virtual void AddObserver(RenderProcessObserver* observer) OVERRIDE;
56 virtual void RemoveObserver(RenderProcessObserver* observer) OVERRIDE;
57 virtual void SetResourceDispatcherDelegate(
58 ResourceDispatcherDelegate* delegate) OVERRIDE;
59 virtual void WidgetHidden() OVERRIDE;
60 virtual void WidgetRestored() OVERRIDE;
61 virtual void EnsureWebKitInitialized() OVERRIDE;
62 virtual void RecordUserMetrics(const std::string& action) OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000063 virtual scoped_ptr<base::SharedMemory> HostAllocateSharedMemoryBuffer(
64 size_t buffer_size) OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000065 virtual void RegisterExtension(v8::Extension* extension) OVERRIDE;
66 virtual void ScheduleIdleHandler(int64 initial_delay_ms) OVERRIDE;
67 virtual void IdleHandler() OVERRIDE;
68 virtual int64 GetIdleNotificationDelayInMs() const OVERRIDE;
69 virtual void SetIdleNotificationDelayInMs(
70 int64 idle_notification_delay_in_ms) OVERRIDE;
71 virtual void ToggleWebKitSharedTimer(bool suspend) OVERRIDE;
72 virtual void UpdateHistograms(int sequence_number) OVERRIDE;
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010073 virtual int PostTaskToAllWebWorkers(const base::Closure& closure) OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000074 virtual bool ResolveProxy(const GURL& url, std::string* proxy_list) OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000075#if defined(OS_WIN)
76 virtual void PreCacheFont(const LOGFONT& log_font) OVERRIDE;
77 virtual void ReleaseCachedFonts() OVERRIDE;
78#endif
79
80 //////////////////////////////////////////////////////////////////////////
81 // The following functions are called by the test itself.
82
83 void set_routing_id(int32 id) {
84 routing_id_ = id;
85 }
86
87 void set_surface_id(int32 id) {
88 surface_id_ = id;
89 }
90
91 int32 opener_id() const {
92 return opener_id_;
93 }
94
95 bool has_widget() const {
96 return (widget_ != NULL);
97 }
98
99 void set_new_window_routing_id(int32 id) {
100 new_window_routing_id_ = id;
101 }
102
103 // Simulates the Widget receiving a close message. This should result
104 // on releasing the internal reference counts and destroying the internal
105 // state.
106 void SendCloseMessage();
107
108 protected:
109 // This function operates as a regular IPC listener. Subclasses
110 // overriding this should first delegate to this implementation.
111 virtual bool OnMessageReceived(const IPC::Message& msg);
112
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +0100113 // Dispatches control messages to observers.
114 bool OnControlMessageReceived(const IPC::Message& msg);
115
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000116 // The Widget expects to be returned valid route_id.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000117 void OnCreateWidget(int opener_id,
118 WebKit::WebPopupType popup_type,
119 int* route_id,
120 int* surface_id);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000121
122 // The View expects to be returned a valid route_id different from its own.
123 // We do not keep track of the newly created widget in MockRenderThread,
124 // so it must be cleaned up on its own.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000125 void OnCreateWindow(
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000126 const ViewHostMsg_CreateWindow_Params& params,
127 int* route_id,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100128 int* main_frame_route_id,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000129 int* surface_id,
130 int64* cloned_session_storage_namespace_id);
131
132#if defined(OS_WIN)
133 void OnDuplicateSection(base::SharedMemoryHandle renderer_handle,
134 base::SharedMemoryHandle* browser_handle);
135#endif
136
137 IPC::TestSink sink_;
138
139 // Routing id what will be assigned to the Widget.
140 int32 routing_id_;
141
142 // Surface id what will be assigned to the Widget.
143 int32 surface_id_;
144
145 // Opener id reported by the Widget.
146 int32 opener_id_;
147
148 // We only keep track of one Widget, we learn its pointer when it
149 // adds a new route. We do not keep track of Widgets created with
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000150 // OnCreateWindow.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000151 IPC::Listener* widget_;
152
153 // Routing id that will be assigned to a CreateWindow Widget.
154 int32 new_window_routing_id_;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100155 int32 new_window_main_frame_routing_id_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000156
157 // The last known good deserializer for sync messages.
158 scoped_ptr<IPC::MessageReplyDeserializer> reply_deserializer_;
159
160 // A list of message filters added to this thread.
161 std::vector<scoped_refptr<IPC::ChannelProxy::MessageFilter> > filters_;
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +0100162
163 // Observers to notify.
164 ObserverList<RenderProcessObserver> observers_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000165};
166
167} // namespace content
168
169#endif // CONTENT_PUBLIC_TEST_MOCK_RENDER_THREAD_H_