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