Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 | #include "content/public/test/mock_render_process_host.h" |
| 6 | |
| 7 | #include "base/lazy_instance.h" |
| 8 | #include "base/message_loop.h" |
| 9 | #include "base/time.h" |
| 10 | #include "content/browser/child_process_security_policy_impl.h" |
| 11 | #include "content/browser/renderer_host/render_process_host_impl.h" |
| 12 | #include "content/common/child_process_host_impl.h" |
| 13 | #include "content/public/browser/notification_service.h" |
| 14 | #include "content/public/browser/notification_types.h" |
| 15 | #include "content/public/browser/storage_partition.h" |
| 16 | |
| 17 | namespace content { |
| 18 | |
| 19 | MockRenderProcessHost::MockRenderProcessHost( |
| 20 | BrowserContext* browser_context) |
| 21 | : transport_dib_(NULL), |
| 22 | bad_msg_count_(0), |
| 23 | factory_(NULL), |
| 24 | id_(ChildProcessHostImpl::GenerateChildProcessUniqueId()), |
| 25 | browser_context_(browser_context), |
| 26 | fast_shutdown_started_(false) { |
| 27 | // Child process security operations can't be unit tested unless we add |
| 28 | // ourselves as an existing child process. |
| 29 | ChildProcessSecurityPolicyImpl::GetInstance()->Add(GetID()); |
| 30 | |
| 31 | RenderProcessHostImpl::RegisterHost(GetID(), this); |
| 32 | } |
| 33 | |
| 34 | MockRenderProcessHost::~MockRenderProcessHost() { |
| 35 | ChildProcessSecurityPolicyImpl::GetInstance()->Remove(GetID()); |
| 36 | delete transport_dib_; |
| 37 | if (factory_) |
| 38 | factory_->Remove(this); |
| 39 | // In unit tests, Release() might not have been called. |
| 40 | RenderProcessHostImpl::UnregisterHost(GetID()); |
| 41 | } |
| 42 | |
| 43 | void MockRenderProcessHost::EnableSendQueue() { |
| 44 | } |
| 45 | |
| 46 | bool MockRenderProcessHost::Init() { |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | int MockRenderProcessHost::GetNextRoutingID() { |
| 51 | static int prev_routing_id = 0; |
| 52 | return ++prev_routing_id; |
| 53 | } |
| 54 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 55 | void MockRenderProcessHost::SimulateSwapOutACK( |
| 56 | const ViewMsg_SwapOut_Params& params) { |
| 57 | } |
| 58 | |
| 59 | bool MockRenderProcessHost::WaitForBackingStoreMsg( |
| 60 | int render_widget_id, |
| 61 | const base::TimeDelta& max_delay, |
| 62 | IPC::Message* msg) { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | void MockRenderProcessHost::ReceivedBadMessage() { |
| 67 | ++bad_msg_count_; |
| 68 | } |
| 69 | |
| 70 | void MockRenderProcessHost::WidgetRestored() { |
| 71 | } |
| 72 | |
| 73 | void MockRenderProcessHost::WidgetHidden() { |
| 74 | } |
| 75 | |
| 76 | int MockRenderProcessHost::VisibleWidgetCount() const { |
| 77 | return 1; |
| 78 | } |
| 79 | |
| 80 | bool MockRenderProcessHost::IsGuest() const { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | StoragePartition* MockRenderProcessHost::GetStoragePartition() const { |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | void MockRenderProcessHost::AddWord(const string16& word) { |
| 89 | } |
| 90 | |
| 91 | bool MockRenderProcessHost::FastShutdownIfPossible() { |
| 92 | // We aren't actually going to do anything, but set |fast_shutdown_started_| |
| 93 | // to true so that tests know we've been called. |
| 94 | fast_shutdown_started_ = true; |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | bool MockRenderProcessHost::FastShutdownStarted() const { |
| 99 | return fast_shutdown_started_; |
| 100 | } |
| 101 | |
| 102 | void MockRenderProcessHost::DumpHandles() { |
| 103 | } |
| 104 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame^] | 105 | base::ProcessHandle MockRenderProcessHost::GetHandle() const { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 106 | // Return the current-process handle for the IPC::GetFileHandleForProcess |
| 107 | // function. |
| 108 | return base::Process::Current().handle(); |
| 109 | } |
| 110 | |
| 111 | bool MockRenderProcessHost::Send(IPC::Message* msg) { |
| 112 | // Save the message in the sink. |
| 113 | sink_.OnMessageReceived(*msg); |
| 114 | delete msg; |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | TransportDIB* MockRenderProcessHost::GetTransportDIB(TransportDIB::Id dib_id) { |
| 119 | if (transport_dib_) |
| 120 | return transport_dib_; |
| 121 | #if defined(OS_WIN) |
| 122 | HANDLE duped; |
| 123 | DuplicateHandle(GetCurrentProcess(), dib_id.handle, GetCurrentProcess(), |
| 124 | &duped, 0, TRUE, DUPLICATE_SAME_ACCESS); |
| 125 | transport_dib_ = TransportDIB::Map(duped); |
| 126 | #elif defined(OS_MACOSX) |
| 127 | // On Mac, TransportDIBs are always created in the browser, so we cannot map |
| 128 | // one from a dib_id. |
| 129 | transport_dib_ = TransportDIB::Create(100 * 100 * 4, 0); |
| 130 | #elif defined(OS_ANDROID) |
| 131 | // On Android, Handles and Ids are the same underlying type. |
| 132 | transport_dib_ = TransportDIB::Map(dib_id); |
| 133 | #elif defined(OS_POSIX) |
| 134 | transport_dib_ = TransportDIB::Map(dib_id.shmkey); |
| 135 | #endif |
| 136 | |
| 137 | return transport_dib_; |
| 138 | } |
| 139 | |
| 140 | int MockRenderProcessHost::GetID() const { |
| 141 | return id_; |
| 142 | } |
| 143 | |
| 144 | bool MockRenderProcessHost::HasConnection() const { |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | void MockRenderProcessHost::SetIgnoreInputEvents(bool ignore_input_events) { |
| 149 | } |
| 150 | |
| 151 | bool MockRenderProcessHost::IgnoreInputEvents() const { |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | void MockRenderProcessHost::Attach(RenderWidgetHost* host, |
| 156 | int routing_id) { |
| 157 | render_widget_hosts_.AddWithID(host, routing_id); |
| 158 | } |
| 159 | |
| 160 | void MockRenderProcessHost::Release(int routing_id) { |
| 161 | render_widget_hosts_.Remove(routing_id); |
| 162 | Cleanup(); |
| 163 | } |
| 164 | |
| 165 | void MockRenderProcessHost::Cleanup() { |
| 166 | if (render_widget_hosts_.IsEmpty()) { |
| 167 | NotificationService::current()->Notify( |
| 168 | NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 169 | Source<RenderProcessHost>(this), |
| 170 | NotificationService::NoDetails()); |
| 171 | MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
| 172 | RenderProcessHostImpl::UnregisterHost(GetID()); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | void MockRenderProcessHost::AddPendingView() { |
| 177 | } |
| 178 | |
| 179 | void MockRenderProcessHost::RemovePendingView() { |
| 180 | } |
| 181 | |
| 182 | void MockRenderProcessHost::SetSuddenTerminationAllowed(bool allowed) { |
| 183 | } |
| 184 | |
| 185 | bool MockRenderProcessHost::SuddenTerminationAllowed() const { |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | RenderWidgetHost* MockRenderProcessHost::GetRenderWidgetHostByID( |
| 190 | int routing_id) { |
| 191 | return render_widget_hosts_.Lookup(routing_id); |
| 192 | } |
| 193 | |
| 194 | BrowserContext* MockRenderProcessHost::GetBrowserContext() const { |
| 195 | return browser_context_; |
| 196 | } |
| 197 | |
| 198 | bool MockRenderProcessHost::InSameStoragePartition( |
| 199 | StoragePartition* partition) const { |
| 200 | // Mock RPHs only have one partition. |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | IPC::ChannelProxy* MockRenderProcessHost::GetChannel() { |
| 205 | return NULL; |
| 206 | } |
| 207 | |
| 208 | bool MockRenderProcessHost::FastShutdownForPageCount(size_t count) { |
| 209 | if (render_widget_hosts_.size() == count) |
| 210 | return FastShutdownIfPossible(); |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | base::TimeDelta MockRenderProcessHost::GetChildProcessIdleTime() const { |
| 215 | return base::TimeDelta::FromMilliseconds(0); |
| 216 | } |
| 217 | |
| 218 | void MockRenderProcessHost::SurfaceUpdated(int32 surface_id) { |
| 219 | } |
| 220 | |
| 221 | void MockRenderProcessHost::ResumeRequestsForView(int route_id) { |
| 222 | } |
| 223 | |
| 224 | RenderProcessHost::RenderWidgetHostsIterator |
| 225 | MockRenderProcessHost::GetRenderWidgetHostsIterator() { |
| 226 | return RenderWidgetHostsIterator(&render_widget_hosts_); |
| 227 | } |
| 228 | |
| 229 | bool MockRenderProcessHost::OnMessageReceived(const IPC::Message& msg) { |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | void MockRenderProcessHost::OnChannelConnected(int32 peer_pid) { |
| 234 | } |
| 235 | |
| 236 | MockRenderProcessHostFactory::MockRenderProcessHostFactory() {} |
| 237 | |
| 238 | MockRenderProcessHostFactory::~MockRenderProcessHostFactory() { |
| 239 | // Detach this object from MockRenderProcesses to prevent STLDeleteElements() |
| 240 | // from calling MockRenderProcessHostFactory::Remove(). |
| 241 | for (ScopedVector<MockRenderProcessHost>::iterator it = processes_.begin(); |
| 242 | it != processes_.end(); ++it) { |
| 243 | (*it)->SetFactory(NULL); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | RenderProcessHost* MockRenderProcessHostFactory::CreateRenderProcessHost( |
| 248 | BrowserContext* browser_context) const { |
| 249 | MockRenderProcessHost* host = new MockRenderProcessHost(browser_context); |
| 250 | if (host) { |
| 251 | processes_.push_back(host); |
| 252 | host->SetFactory(this); |
| 253 | } |
| 254 | return host; |
| 255 | } |
| 256 | |
| 257 | void MockRenderProcessHostFactory::Remove(MockRenderProcessHost* host) const { |
| 258 | for (ScopedVector<MockRenderProcessHost>::iterator it = processes_.begin(); |
| 259 | it != processes_.end(); ++it) { |
| 260 | if (*it == host) { |
| 261 | processes_.weak_erase(it); |
| 262 | break; |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | } // content |