mock_render_thread.h revision f2477e01787aa58f445919b809d89e252beef54f
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 // 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; 42 virtual base::MessageLoop* GetMessageLoop() OVERRIDE; 43 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 AddObserver(RenderProcessObserver* observer) OVERRIDE; 54 virtual void RemoveObserver(RenderProcessObserver* observer) OVERRIDE; 55 virtual void SetResourceDispatcherDelegate( 56 ResourceDispatcherDelegate* delegate) OVERRIDE; 57 virtual void WidgetHidden() OVERRIDE; 58 virtual void WidgetRestored() OVERRIDE; 59 virtual void EnsureWebKitInitialized() OVERRIDE; 60 virtual void RecordUserMetrics(const std::string& action) OVERRIDE; 61 virtual scoped_ptr<base::SharedMemory> HostAllocateSharedMemoryBuffer( 62 size_t buffer_size) OVERRIDE; 63 virtual void RegisterExtension(v8::Extension* extension) OVERRIDE; 64 virtual void ScheduleIdleHandler(int64 initial_delay_ms) OVERRIDE; 65 virtual void IdleHandler() OVERRIDE; 66 virtual int64 GetIdleNotificationDelayInMs() const OVERRIDE; 67 virtual void SetIdleNotificationDelayInMs( 68 int64 idle_notification_delay_in_ms) OVERRIDE; 69 virtual void ToggleWebKitSharedTimer(bool suspend) OVERRIDE; 70 virtual void UpdateHistograms(int sequence_number) OVERRIDE; 71 virtual int PostTaskToAllWebWorkers(const base::Closure& closure) OVERRIDE; 72 virtual bool ResolveProxy(const GURL& url, std::string* proxy_list) OVERRIDE; 73#if defined(OS_WIN) 74 virtual void PreCacheFont(const LOGFONT& log_font) OVERRIDE; 75 virtual void ReleaseCachedFonts() OVERRIDE; 76#endif 77 78 ////////////////////////////////////////////////////////////////////////// 79 // The following functions are called by the test itself. 80 81 void set_routing_id(int32 id) { 82 routing_id_ = id; 83 } 84 85 void set_surface_id(int32 id) { 86 surface_id_ = id; 87 } 88 89 int32 opener_id() const { 90 return opener_id_; 91 } 92 93 bool has_widget() const { 94 return (widget_ != NULL); 95 } 96 97 void set_new_window_routing_id(int32 id) { 98 new_window_routing_id_ = id; 99 } 100 101 // Simulates the Widget receiving a close message. This should result 102 // on releasing the internal reference counts and destroying the internal 103 // state. 104 void SendCloseMessage(); 105 106 protected: 107 // This function operates as a regular IPC listener. Subclasses 108 // overriding this should first delegate to this implementation. 109 virtual bool OnMessageReceived(const IPC::Message& msg); 110 111 // Dispatches control messages to observers. 112 bool OnControlMessageReceived(const IPC::Message& msg); 113 114 // The Widget expects to be returned valid route_id. 115 void OnCreateWidget(int opener_id, 116 blink::WebPopupType popup_type, 117 int* route_id, 118 int* surface_id); 119 120 // The View expects to be returned a valid route_id different from its own. 121 // We do not keep track of the newly created widget in MockRenderThread, 122 // so it must be cleaned up on its own. 123 void OnCreateWindow( 124 const ViewHostMsg_CreateWindow_Params& params, 125 int* route_id, 126 int* main_frame_route_id, 127 int* surface_id, 128 int64* cloned_session_storage_namespace_id); 129 130#if defined(OS_WIN) 131 void OnDuplicateSection(base::SharedMemoryHandle renderer_handle, 132 base::SharedMemoryHandle* browser_handle); 133#endif 134 135 IPC::TestSink sink_; 136 137 // Routing id what will be assigned to the Widget. 138 int32 routing_id_; 139 140 // Surface id what will be assigned to the Widget. 141 int32 surface_id_; 142 143 // Opener id reported by the Widget. 144 int32 opener_id_; 145 146 // We only keep track of one Widget, we learn its pointer when it 147 // adds a new route. We do not keep track of Widgets created with 148 // OnCreateWindow. 149 IPC::Listener* widget_; 150 151 // Routing id that will be assigned to a CreateWindow Widget. 152 int32 new_window_routing_id_; 153 int32 new_window_main_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