mock_render_thread.h revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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.
4
5#ifndef CONTENT_PUBLIC_TEST_MOCK_RENDER_THREAD_H_
6#define CONTENT_PUBLIC_TEST_MOCK_RENDER_THREAD_H_
7
8#include "base/memory/shared_memory.h"
9#include "base/observer_list.h"
10#include "base/strings/string16.h"
11#include "content/public/renderer/render_thread.h"
12#include "ipc/ipc_test_sink.h"
13#include "third_party/WebKit/public/web/WebPopupType.h"
14
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  // RenderThread implementation:
37  virtual bool Send(IPC::Message* msg) OVERRIDE;
38  virtual base::MessageLoop* GetMessageLoop() OVERRIDE;
39  virtual IPC::SyncChannel* GetChannel() OVERRIDE;
40  virtual std::string GetLocale() OVERRIDE;
41  virtual IPC::SyncMessageFilter* GetSyncMessageFilter() OVERRIDE;
42  virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy()
43      OVERRIDE;
44  virtual void AddRoute(int32 routing_id, IPC::Listener* listener) OVERRIDE;
45  virtual void RemoveRoute(int32 routing_id) OVERRIDE;
46  virtual int GenerateRoutingID() OVERRIDE;
47  virtual void AddFilter(IPC::ChannelProxy::MessageFilter* filter) OVERRIDE;
48  virtual void RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) OVERRIDE;
49  virtual void AddObserver(RenderProcessObserver* observer) OVERRIDE;
50  virtual void RemoveObserver(RenderProcessObserver* observer) OVERRIDE;
51  virtual void SetResourceDispatcherDelegate(
52      ResourceDispatcherDelegate* delegate) OVERRIDE;
53  virtual void EnsureWebKitInitialized() OVERRIDE;
54  virtual void RecordAction(const base::UserMetricsAction& action) OVERRIDE;
55  virtual void RecordComputedAction(const std::string& action) OVERRIDE;
56  virtual scoped_ptr<base::SharedMemory> HostAllocateSharedMemoryBuffer(
57      size_t buffer_size) OVERRIDE;
58  virtual void RegisterExtension(v8::Extension* extension) OVERRIDE;
59  virtual void ScheduleIdleHandler(int64 initial_delay_ms) OVERRIDE;
60  virtual void IdleHandler() OVERRIDE;
61  virtual int64 GetIdleNotificationDelayInMs() const OVERRIDE;
62  virtual void SetIdleNotificationDelayInMs(
63      int64 idle_notification_delay_in_ms) OVERRIDE;
64  virtual void UpdateHistograms(int sequence_number) OVERRIDE;
65  virtual int PostTaskToAllWebWorkers(const base::Closure& closure) OVERRIDE;
66  virtual bool ResolveProxy(const GURL& url, std::string* proxy_list) OVERRIDE;
67  virtual base::WaitableEvent* GetShutdownEvent() OVERRIDE;
68#if defined(OS_WIN)
69  virtual void PreCacheFont(const LOGFONT& log_font) OVERRIDE;
70  virtual void ReleaseCachedFonts() OVERRIDE;
71#endif
72
73  //////////////////////////////////////////////////////////////////////////
74  // The following functions are called by the test itself.
75
76  void set_routing_id(int32 id) {
77    routing_id_ = id;
78  }
79
80  void set_surface_id(int32 id) {
81    surface_id_ = id;
82  }
83
84  int32 opener_id() const {
85    return opener_id_;
86  }
87
88  void set_new_window_routing_id(int32 id) {
89    new_window_routing_id_ = id;
90  }
91
92  void set_new_frame_routing_id(int32 id) {
93    new_frame_routing_id_ = id;
94  }
95
96  // Simulates the Widget receiving a close message. This should result
97  // on releasing the internal reference counts and destroying the internal
98  // state.
99  void SendCloseMessage();
100
101  // Dispatches control messages to observers.
102  bool OnControlMessageReceived(const IPC::Message& msg);
103
104  ObserverList<RenderProcessObserver>& observers() {
105    return observers_;
106  }
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
113  // The Widget expects to be returned valid route_id.
114  void OnCreateWidget(int opener_id,
115                      blink::WebPopupType popup_type,
116                      int* route_id,
117                      int* surface_id);
118
119  // The View expects to be returned a valid route_id different from its own.
120  // We do not keep track of the newly created widget in MockRenderThread,
121  // so it must be cleaned up on its own.
122  void OnCreateWindow(
123    const ViewHostMsg_CreateWindow_Params& params,
124    int* route_id,
125    int* main_frame_route_id,
126    int* surface_id,
127    int64* cloned_session_storage_namespace_id);
128
129  // The Frame expects to be returned a valid route_id different from its own.
130  void OnCreateChildFrame(int new_frame_routing_id,
131                          const std::string& frame_name,
132                          int* new_render_frame_id);
133
134#if defined(OS_WIN)
135  void OnDuplicateSection(base::SharedMemoryHandle renderer_handle,
136                          base::SharedMemoryHandle* browser_handle);
137#endif
138
139  IPC::TestSink sink_;
140
141  // Routing id what will be assigned to the Widget.
142  int32 routing_id_;
143
144  // Surface id what will be assigned to the Widget.
145  int32 surface_id_;
146
147  // Opener id reported by the Widget.
148  int32 opener_id_;
149
150  // Routing id that will be assigned to a CreateWindow Widget.
151  int32 new_window_routing_id_;
152  int32 new_window_main_frame_routing_id_;
153  int32 new_frame_routing_id_;
154
155  // The last known good deserializer for sync messages.
156  scoped_ptr<IPC::MessageReplyDeserializer> reply_deserializer_;
157
158  // A list of message filters added to this thread.
159  std::vector<scoped_refptr<IPC::ChannelProxy::MessageFilter> > filters_;
160
161  // Observers to notify.
162  ObserverList<RenderProcessObserver> observers_;
163};
164
165}  // namespace content
166
167#endif  // CONTENT_PUBLIC_TEST_MOCK_RENDER_THREAD_H_
168