[email protected] | 9f4f332 | 2012-01-18 22:29:56 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 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_TEST_MOCK_RENDER_THREAD_H_ |
| 6 | #define CONTENT_TEST_MOCK_RENDER_THREAD_H_ |
| 7 | #pragma once |
| 8 | |
| 9 | #include "base/shared_memory.h" |
[email protected] | 6f8f8b63 | 2011-12-05 16:43:55 | [diff] [blame] | 10 | #include "base/string16.h" |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 11 | #include "content/public/renderer/render_thread.h" |
| 12 | #include "ipc/ipc_test_sink.h" |
| 13 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupType.h" |
| 14 | |
[email protected] | 744c2a2 | 2012-03-15 18:42:04 | [diff] [blame^] | 15 | struct ViewHostMsg_CreateWindow_Params; |
| 16 | |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 17 | namespace IPC { |
| 18 | class MessageReplyDeserializer; |
| 19 | } |
| 20 | |
| 21 | namespace content { |
| 22 | |
| 23 | // This class is a very simple mock of RenderThread. It simulates an IPC channel |
[email protected] | 744c2a2 | 2012-03-15 18:42:04 | [diff] [blame^] | 24 | // which supports only three messages: |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 25 | // ViewHostMsg_CreateWidget : sync message sent by the Widget. |
[email protected] | 744c2a2 | 2012-03-15 18:42:04 | [diff] [blame^] | 26 | // ViewHostMsg_CreateWindow : sync message sent by the Widget. |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 27 | // ViewMsg_Close : async, send to the Widget. |
| 28 | class MockRenderThread : public content::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 | |
[email protected] | 6f8f8b63 | 2011-12-05 16:43:55 | [diff] [blame] | 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 | |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 40 | // content::RenderThread implementation: |
| 41 | virtual bool Send(IPC::Message* msg) OVERRIDE; |
| 42 | virtual MessageLoop* GetMessageLoop() OVERRIDE; |
| 43 | virtual IPC::SyncChannel* GetChannel() OVERRIDE; |
| 44 | virtual std::string GetLocale() OVERRIDE; |
[email protected] | 07bb633 | 2012-01-21 01:07:57 | [diff] [blame] | 45 | virtual IPC::SyncMessageFilter* GetSyncMessageFilter() OVERRIDE; |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 46 | virtual void AddRoute(int32 routing_id, |
| 47 | IPC::Channel::Listener* listener) OVERRIDE; |
| 48 | virtual void RemoveRoute(int32 routing_id) OVERRIDE; |
[email protected] | 77fc9b9 | 2011-10-15 16:20:37 | [diff] [blame] | 49 | virtual int GenerateRoutingID() OVERRIDE; |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 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(content::RenderProcessObserver* observer) OVERRIDE; |
| 55 | virtual void RemoveObserver( |
| 56 | content::RenderProcessObserver* observer) OVERRIDE; |
| 57 | virtual void SetResourceDispatcherDelegate( |
| 58 | content::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; |
| 63 | virtual base::SharedMemoryHandle HostAllocateSharedMemoryBuffer( |
| 64 | uint32 buffer_size) OVERRIDE; |
| 65 | virtual void RegisterExtension(v8::Extension* extension) OVERRIDE; |
| 66 | virtual bool IsRegisteredExtension( |
| 67 | const std::string& v8_extension_name) const OVERRIDE; |
[email protected] | 6593ae1 | 2011-11-14 12:09:44 | [diff] [blame] | 68 | virtual void ScheduleIdleHandler(int64 initial_delay_ms) OVERRIDE; |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 69 | virtual void IdleHandler() OVERRIDE; |
[email protected] | 6593ae1 | 2011-11-14 12:09:44 | [diff] [blame] | 70 | virtual int64 GetIdleNotificationDelayInMs() const OVERRIDE; |
| 71 | virtual void SetIdleNotificationDelayInMs( |
| 72 | int64 idle_notification_delay_in_ms) OVERRIDE; |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 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 | |
[email protected] | 9f4f332 | 2012-01-18 22:29:56 | [diff] [blame] | 85 | void set_surface_id(int32 id) { |
| 86 | surface_id_ = id; |
| 87 | } |
| 88 | |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 89 | int32 opener_id() const { |
| 90 | return opener_id_; |
| 91 | } |
| 92 | |
| 93 | bool has_widget() const { |
| 94 | return widget_ ? true : false; |
| 95 | } |
| 96 | |
[email protected] | 744c2a2 | 2012-03-15 18:42:04 | [diff] [blame^] | 97 | void set_new_window_routing_id(int32 id) { |
| 98 | new_window_routing_id_ = id; |
| 99 | } |
| 100 | |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 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 | // The Widget expects to be returned valid route_id. |
| 112 | void OnMsgCreateWidget(int opener_id, |
| 113 | WebKit::WebPopupType popup_type, |
[email protected] | 9f4f332 | 2012-01-18 22:29:56 | [diff] [blame] | 114 | int* route_id, |
| 115 | int* surface_id); |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 116 | |
[email protected] | 744c2a2 | 2012-03-15 18:42:04 | [diff] [blame^] | 117 | // The View expects to be returned a valid route_id different from its own. |
| 118 | // We do not keep track of the newly created widget in MockRenderThread, |
| 119 | // so it must be cleaned up on its own. |
| 120 | void OnMsgCreateWindow( |
| 121 | const ViewHostMsg_CreateWindow_Params& params, |
| 122 | int* route_id, |
| 123 | int* surface_id, |
| 124 | int64* cloned_session_storage_namespace_id); |
| 125 | |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 126 | #if defined(OS_WIN) |
| 127 | void OnDuplicateSection(base::SharedMemoryHandle renderer_handle, |
| 128 | base::SharedMemoryHandle* browser_handle); |
| 129 | #endif |
| 130 | |
| 131 | IPC::TestSink sink_; |
| 132 | |
| 133 | // Routing id what will be assigned to the Widget. |
| 134 | int32 routing_id_; |
| 135 | |
[email protected] | 9f4f332 | 2012-01-18 22:29:56 | [diff] [blame] | 136 | // Surface id what will be assigned to the Widget. |
| 137 | int32 surface_id_; |
| 138 | |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 139 | // Opener id reported by the Widget. |
| 140 | int32 opener_id_; |
| 141 | |
| 142 | // We only keep track of one Widget, we learn its pointer when it |
[email protected] | 744c2a2 | 2012-03-15 18:42:04 | [diff] [blame^] | 143 | // adds a new route. We do not keep track of Widgets created with |
| 144 | // OnMsgCreateWindow. |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 145 | IPC::Channel::Listener* widget_; |
| 146 | |
[email protected] | 744c2a2 | 2012-03-15 18:42:04 | [diff] [blame^] | 147 | // Routing id that will be assigned to a CreateWindow Widget. |
| 148 | int32 new_window_routing_id_; |
| 149 | |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 150 | // The last known good deserializer for sync messages. |
| 151 | scoped_ptr<IPC::MessageReplyDeserializer> reply_deserializer_; |
| 152 | }; |
| 153 | |
| 154 | } // namespace content |
| 155 | |
| 156 | #endif // CONTENT_TEST_MOCK_RENDER_THREAD_H_ |