| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/renderer_host/mock_render_process_host.h" | 5 #include "content/browser/renderer_host/mock_render_process_host.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "base/time.h" |
| 7 #include "content/browser/child_process_security_policy.h" | 10 #include "content/browser/child_process_security_policy.h" |
| 11 #include "content/common/child_process_info.h" |
| 12 #include "content/public/browser/notification_service.h" |
| 13 #include "content/public/browser/notification_types.h" |
| 14 |
| 15 // This map is the global list of all renderer processes and is defined in |
| 16 // render_process_host_impl.cc |
| 17 // TODO(ananta) |
| 18 // Clean up this dependency in a subsequent CL. |
| 19 extern base::LazyInstance< |
| 20 IDMap<content::RenderProcessHost>, |
| 21 base::LeakyLazyInstanceTraits<IDMap<content::RenderProcessHost> > > |
| 22 g_all_hosts; |
| 8 | 23 |
| 9 MockRenderProcessHost::MockRenderProcessHost( | 24 MockRenderProcessHost::MockRenderProcessHost( |
| 10 content::BrowserContext* browser_context) | 25 content::BrowserContext* browser_context) |
| 11 : RenderProcessHost(browser_context), | 26 : transport_dib_(NULL), |
| 12 transport_dib_(NULL), | |
| 13 bad_msg_count_(0), | 27 bad_msg_count_(0), |
| 14 factory_(NULL) { | 28 factory_(NULL), |
| 29 id_(ChildProcessInfo::GenerateChildProcessUniqueId()), |
| 30 browser_context_(browser_context), |
| 31 max_page_id_(-1), |
| 32 fast_shutdown_started_(false) { |
| 15 // Child process security operations can't be unit tested unless we add | 33 // Child process security operations can't be unit tested unless we add |
| 16 // ourselves as an existing child process. | 34 // ourselves as an existing child process. |
| 17 ChildProcessSecurityPolicy::GetInstance()->Add(id()); | 35 ChildProcessSecurityPolicy::GetInstance()->Add(GetID()); |
| 36 g_all_hosts.Get().AddWithID(this, GetID()); |
| 18 } | 37 } |
| 19 | 38 |
| 20 MockRenderProcessHost::~MockRenderProcessHost() { | 39 MockRenderProcessHost::~MockRenderProcessHost() { |
| 21 ChildProcessSecurityPolicy::GetInstance()->Remove(id()); | 40 ChildProcessSecurityPolicy::GetInstance()->Remove(GetID()); |
| 22 delete transport_dib_; | 41 delete transport_dib_; |
| 23 if (factory_) | 42 if (factory_) |
| 24 factory_->Remove(this); | 43 factory_->Remove(this); |
| 44 // In unit tests, Release() might not have been called. |
| 45 if (g_all_hosts.Get().Lookup(GetID())) |
| 46 g_all_hosts.Get().Remove(GetID()); |
| 25 } | 47 } |
| 26 | 48 |
| 27 void MockRenderProcessHost::EnableSendQueue() { | 49 void MockRenderProcessHost::EnableSendQueue() { |
| 28 } | 50 } |
| 29 | 51 |
| 30 bool MockRenderProcessHost::Init(bool is_accessibility_enabled) { | 52 bool MockRenderProcessHost::Init(bool is_accessibility_enabled) { |
| 31 return true; | 53 return true; |
| 32 } | 54 } |
| 33 | 55 |
| 34 int MockRenderProcessHost::GetNextRoutingID() { | 56 int MockRenderProcessHost::GetNextRoutingID() { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 63 void MockRenderProcessHost::WidgetHidden() { | 85 void MockRenderProcessHost::WidgetHidden() { |
| 64 } | 86 } |
| 65 | 87 |
| 66 int MockRenderProcessHost::VisibleWidgetCount() const { | 88 int MockRenderProcessHost::VisibleWidgetCount() const { |
| 67 return 1; | 89 return 1; |
| 68 } | 90 } |
| 69 | 91 |
| 70 void MockRenderProcessHost::AddWord(const string16& word) { | 92 void MockRenderProcessHost::AddWord(const string16& word) { |
| 71 } | 93 } |
| 72 | 94 |
| 73 | |
| 74 bool MockRenderProcessHost::FastShutdownIfPossible() { | 95 bool MockRenderProcessHost::FastShutdownIfPossible() { |
| 75 // We aren't actually going to do anything, but set |fast_shutdown_started_| | 96 // We aren't actually going to do anything, but set |fast_shutdown_started_| |
| 76 // to true so that tests know we've been called. | 97 // to true so that tests know we've been called. |
| 77 fast_shutdown_started_ = true; | 98 fast_shutdown_started_ = true; |
| 78 return true; | 99 return true; |
| 79 } | 100 } |
| 80 | 101 |
| 102 bool MockRenderProcessHost::FastShutdownStarted() const { |
| 103 return fast_shutdown_started_; |
| 104 } |
| 105 |
| 81 void MockRenderProcessHost::DumpHandles() { | 106 void MockRenderProcessHost::DumpHandles() { |
| 82 } | 107 } |
| 83 | 108 |
| 84 base::ProcessHandle MockRenderProcessHost::GetHandle() { | 109 base::ProcessHandle MockRenderProcessHost::GetHandle() { |
| 85 return base::kNullProcessHandle; | 110 return base::kNullProcessHandle; |
| 86 } | 111 } |
| 87 | 112 |
| 88 bool MockRenderProcessHost::Send(IPC::Message* msg) { | 113 bool MockRenderProcessHost::Send(IPC::Message* msg) { |
| 89 // Save the message in the sink. | 114 // Save the message in the sink. |
| 90 sink_.OnMessageReceived(*msg); | 115 sink_.OnMessageReceived(*msg); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 109 #endif | 134 #endif |
| 110 | 135 |
| 111 return transport_dib_; | 136 return transport_dib_; |
| 112 } | 137 } |
| 113 | 138 |
| 114 void MockRenderProcessHost::SetCompositingSurface( | 139 void MockRenderProcessHost::SetCompositingSurface( |
| 115 int render_widget_id, | 140 int render_widget_id, |
| 116 gfx::PluginWindowHandle compositing_surface) { | 141 gfx::PluginWindowHandle compositing_surface) { |
| 117 } | 142 } |
| 118 | 143 |
| 144 int MockRenderProcessHost::GetID() const { |
| 145 return id_; |
| 146 } |
| 147 |
| 148 bool MockRenderProcessHost::HasConnection() const { |
| 149 return true; |
| 150 } |
| 151 |
| 152 void MockRenderProcessHost::SetIgnoreInputEvents(bool ignore_input_events) { |
| 153 } |
| 154 |
| 155 bool MockRenderProcessHost::IgnoreInputEvents() const { |
| 156 return false; |
| 157 } |
| 158 |
| 159 void MockRenderProcessHost::Attach(IPC::Channel::Listener* listener, |
| 160 int routing_id) { |
| 161 listeners_.AddWithID(listener, routing_id); |
| 162 } |
| 163 |
| 164 void MockRenderProcessHost::Release(int listener_id) { |
| 165 listeners_.Remove(listener_id); |
| 166 Cleanup(); |
| 167 } |
| 168 |
| 169 void MockRenderProcessHost::Cleanup() { |
| 170 if (listeners_.IsEmpty()) { |
| 171 content::NotificationService::current()->Notify( |
| 172 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 173 content::Source<RenderProcessHost>(this), |
| 174 content::NotificationService::NoDetails()); |
| 175 MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
| 176 g_all_hosts.Get().Remove(GetID()); |
| 177 } |
| 178 } |
| 179 |
| 180 void MockRenderProcessHost::ReportExpectingClose(int32 listener_id) { |
| 181 } |
| 182 |
| 183 void MockRenderProcessHost::AddPendingView() { |
| 184 } |
| 185 |
| 186 void MockRenderProcessHost::RemovePendingView() { |
| 187 } |
| 188 |
| 189 void MockRenderProcessHost::SetSuddenTerminationAllowed(bool allowed) { |
| 190 } |
| 191 |
| 192 bool MockRenderProcessHost::SuddenTerminationAllowed() const { |
| 193 return true; |
| 194 } |
| 195 |
| 196 IPC::Channel::Listener* MockRenderProcessHost::GetListenerByID( |
| 197 int routing_id) { |
| 198 return listeners_.Lookup(routing_id); |
| 199 } |
| 200 |
| 201 void MockRenderProcessHost::UpdateMaxPageID(int32 page_id) { |
| 202 if (page_id > max_page_id_) |
| 203 max_page_id_ = page_id; |
| 204 } |
| 205 |
| 206 content::BrowserContext* MockRenderProcessHost::GetBrowserContext() const { |
| 207 return browser_context_; |
| 208 } |
| 209 |
| 210 IPC::ChannelProxy* MockRenderProcessHost::GetChannel() { |
| 211 return NULL; |
| 212 } |
| 213 |
| 214 bool MockRenderProcessHost::FastShutdownForPageCount(size_t count) { |
| 215 if (listeners_.size() == count) |
| 216 return FastShutdownIfPossible(); |
| 217 return false; |
| 218 } |
| 219 |
| 220 base::TimeDelta MockRenderProcessHost::GetChildProcessIdleTime() const { |
| 221 return base::TimeDelta::FromMilliseconds(0); |
| 222 } |
| 223 |
| 224 content::RenderProcessHost::listeners_iterator |
| 225 MockRenderProcessHost::ListenersIterator() { |
| 226 IDMap<IPC::Channel::Listener> listeners; |
| 227 return listeners_iterator(&listeners); |
| 228 } |
| 229 |
| 119 bool MockRenderProcessHost::OnMessageReceived(const IPC::Message& msg) { | 230 bool MockRenderProcessHost::OnMessageReceived(const IPC::Message& msg) { |
| 120 return false; | 231 return false; |
| 121 } | 232 } |
| 122 | 233 |
| 123 void MockRenderProcessHost::OnChannelConnected(int32 peer_pid) { | 234 void MockRenderProcessHost::OnChannelConnected(int32 peer_pid) { |
| 124 } | 235 } |
| 125 | 236 |
| 126 MockRenderProcessHostFactory::MockRenderProcessHostFactory() {} | 237 MockRenderProcessHostFactory::MockRenderProcessHostFactory() {} |
| 127 | 238 |
| 128 MockRenderProcessHostFactory::~MockRenderProcessHostFactory() { | 239 MockRenderProcessHostFactory::~MockRenderProcessHostFactory() { |
| 129 // Detach this object from MockRenderProcesses to prevent STLDeleteElements() | 240 // Detach this object from MockRenderProcesses to prevent STLDeleteElements() |
| 130 // from calling MockRenderProcessHostFactory::Remove(). | 241 // from calling MockRenderProcessHostFactory::Remove(). |
| 131 for (ScopedVector<MockRenderProcessHost>::iterator it = processes_.begin(); | 242 for (ScopedVector<MockRenderProcessHost>::iterator it = processes_.begin(); |
| 132 it != processes_.end(); ++it) { | 243 it != processes_.end(); ++it) { |
| 133 (*it)->SetFactory(NULL); | 244 (*it)->SetFactory(NULL); |
| 134 } | 245 } |
| 135 } | 246 } |
| 136 | 247 |
| 137 RenderProcessHost* MockRenderProcessHostFactory::CreateRenderProcessHost( | 248 content::RenderProcessHost* |
| 138 content::BrowserContext* browser_context) const { | 249 MockRenderProcessHostFactory::CreateRenderProcessHost( |
| 250 content::BrowserContext* browser_context) const { |
| 139 MockRenderProcessHost* host = new MockRenderProcessHost(browser_context); | 251 MockRenderProcessHost* host = new MockRenderProcessHost(browser_context); |
| 140 if (host) { | 252 if (host) { |
| 141 processes_.push_back(host); | 253 processes_.push_back(host); |
| 142 host->SetFactory(this); | 254 host->SetFactory(this); |
| 143 } | 255 } |
| 144 return host; | 256 return host; |
| 145 } | 257 } |
| 146 | 258 |
| 147 void MockRenderProcessHostFactory::Remove(MockRenderProcessHost* host) const { | 259 void MockRenderProcessHostFactory::Remove(MockRenderProcessHost* host) const { |
| 148 for (ScopedVector<MockRenderProcessHost>::iterator it = processes_.begin(); | 260 for (ScopedVector<MockRenderProcessHost>::iterator it = processes_.begin(); |
| 149 it != processes_.end(); ++it) { | 261 it != processes_.end(); ++it) { |
| 150 if (*it == host) { | 262 if (*it == host) { |
| 151 processes_.weak_erase(it); | 263 processes_.weak_erase(it); |
| 152 break; | 264 break; |
| 153 } | 265 } |
| 154 } | 266 } |
| 155 } | 267 } |
| OLD | NEW |