Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/guest_view/guest_view_base.h" | 5 #include "chrome/browser/guest_view/guest_view_base.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/guest_view/guest_view_constants.h" | 9 #include "chrome/browser/guest_view/guest_view_constants.h" |
| 10 #include "chrome/browser/guest_view/guest_view_manager.h" | 10 #include "chrome/browser/guest_view/guest_view_manager.h" |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 69 GuestViewBase::GuestViewBase(int guest_instance_id) | 69 GuestViewBase::GuestViewBase(int guest_instance_id) |
| 70 : embedder_web_contents_(NULL), | 70 : embedder_web_contents_(NULL), |
| 71 embedder_render_process_id_(0), | 71 embedder_render_process_id_(0), |
| 72 browser_context_(NULL), | 72 browser_context_(NULL), |
| 73 guest_instance_id_(guest_instance_id), | 73 guest_instance_id_(guest_instance_id), |
| 74 view_instance_id_(guestview::kInstanceIDNone), | 74 view_instance_id_(guestview::kInstanceIDNone), |
| 75 initialized_(false), | 75 initialized_(false), |
| 76 weak_ptr_factory_(this) { | 76 weak_ptr_factory_(this) { |
| 77 } | 77 } |
| 78 | 78 |
| 79 void GuestViewBase::Init(WebContents* guest_web_contents, | 79 void GuestViewBase::Init( |
| 80 const std::string& embedder_extension_id) { | 80 const std::string& embedder_extension_id, |
| 81 content::RenderProcessHost* embedder_render_process_host, | |
| 82 const base::DictionaryValue& create_params) { | |
| 81 if (initialized_) | 83 if (initialized_) |
| 82 return; | 84 return; |
| 83 initialized_ = true; | 85 initialized_ = true; |
| 86 | |
| 87 CreateWebContents(embedder_extension_id, | |
| 88 embedder_render_process_host, | |
| 89 create_params, | |
| 90 base::Bind(&GuestViewBase::InitWithWebContents, | |
| 91 AsWeakPtr(), | |
| 92 embedder_extension_id, | |
| 93 embedder_render_process_host)); | |
| 94 } | |
| 95 | |
| 96 void GuestViewBase::InitWithWebContents( | |
| 97 const std::string& embedder_extension_id, | |
| 98 content::RenderProcessHost* embedder_render_process_host, | |
| 99 content::WebContents* guest_web_contents) { | |
| 100 if (!guest_web_contents) | |
|
lazyboy
2014/06/17 23:46:45
Should this be DCHECK instead?
Fady Samuel
2014/06/18 21:08:33
Done.
| |
| 101 return; | |
| 102 | |
| 84 browser_context_ = guest_web_contents->GetBrowserContext(); | 103 browser_context_ = guest_web_contents->GetBrowserContext(); |
| 85 embedder_extension_id_ = embedder_extension_id; | 104 embedder_extension_id_ = embedder_extension_id; |
| 105 embedder_render_process_id_ = embedder_render_process_host->GetID(); | |
| 106 embedder_render_process_host->AddObserver(this); | |
| 86 | 107 |
| 87 WebContentsObserver::Observe(guest_web_contents); | 108 WebContentsObserver::Observe(guest_web_contents); |
| 88 guest_web_contents->SetDelegate(this); | 109 guest_web_contents->SetDelegate(this); |
| 89 webcontents_guestview_map.Get().insert( | 110 webcontents_guestview_map.Get().insert( |
| 90 std::make_pair(guest_web_contents, this)); | 111 std::make_pair(guest_web_contents, this)); |
| 91 GuestViewManager::FromBrowserContext(browser_context_)-> | 112 GuestViewManager::FromBrowserContext(browser_context_)-> |
| 92 AddGuest(guest_instance_id_, guest_web_contents); | 113 AddGuest(guest_instance_id_, guest_web_contents); |
| 114 | |
| 115 // Give the derived class an opportunity to perform additional initialization. | |
| 116 Init(); | |
| 93 } | 117 } |
| 94 | 118 |
| 95 // static | 119 // static |
| 96 GuestViewBase* GuestViewBase::Create( | 120 GuestViewBase* GuestViewBase::Create( |
| 97 int guest_instance_id, | 121 int guest_instance_id, |
| 98 WebContents* guest_web_contents, | |
| 99 const std::string& embedder_extension_id, | |
| 100 const std::string& view_type) { | 122 const std::string& view_type) { |
| 101 if (view_type == "webview") { | 123 if (view_type == "webview") { |
| 102 return new WebViewGuest(guest_instance_id, | 124 return new WebViewGuest(guest_instance_id); |
| 103 guest_web_contents, | |
| 104 embedder_extension_id); | |
| 105 } | 125 } |
| 106 NOTREACHED(); | 126 NOTREACHED(); |
| 107 return NULL; | 127 return NULL; |
| 108 } | 128 } |
| 109 | 129 |
| 110 // static | 130 // static |
| 111 GuestViewBase* GuestViewBase::FromWebContents(WebContents* web_contents) { | 131 GuestViewBase* GuestViewBase::FromWebContents(WebContents* web_contents) { |
| 112 WebContentsGuestViewMap* guest_map = webcontents_guestview_map.Pointer(); | 132 WebContentsGuestViewMap* guest_map = webcontents_guestview_map.Pointer(); |
| 113 WebContentsGuestViewMap::iterator it = guest_map->find(web_contents); | 133 WebContentsGuestViewMap::iterator it = guest_map->find(web_contents); |
| 114 return it == guest_map->end() ? NULL : it->second; | 134 return it == guest_map->end() ? NULL : it->second; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 } | 176 } |
| 157 | 177 |
| 158 base::WeakPtr<GuestViewBase> GuestViewBase::AsWeakPtr() { | 178 base::WeakPtr<GuestViewBase> GuestViewBase::AsWeakPtr() { |
| 159 return weak_ptr_factory_.GetWeakPtr(); | 179 return weak_ptr_factory_.GetWeakPtr(); |
| 160 } | 180 } |
| 161 | 181 |
| 162 bool GuestViewBase::IsDragAndDropEnabled() const { | 182 bool GuestViewBase::IsDragAndDropEnabled() const { |
| 163 return false; | 183 return false; |
| 164 } | 184 } |
| 165 | 185 |
| 186 void GuestViewBase::RenderProcessExited(content::RenderProcessHost* host, | |
| 187 base::ProcessHandle handle, | |
| 188 base::TerminationStatus status, | |
| 189 int exit_code) { | |
| 190 // GuestViewBase tracks the lifetime of its embedder render process until it | |
|
lazyboy
2014/06/17 23:46:45
This comment should also be class level comment fo
Fady Samuel
2014/06/18 21:08:33
Done.
| |
| 191 // is attached to a particular embedder WebContents. At that point, its | |
| 192 // lifetime is restricted in scope to the lifetime of its embedder | |
| 193 // WebContents. | |
| 194 CHECK(!attached()); | |
| 195 CHECK_EQ(host->GetID(), embedder_render_process_id()); | |
| 196 | |
| 197 // This code path may be reached if the embedder WebContents is killed for | |
| 198 // whatever reason immediately after a called to GuestViewInternal.createGuest | |
| 199 // and before attaching the new guest to a frame. | |
| 200 Destroy(); | |
| 201 } | |
| 202 | |
| 166 void GuestViewBase::Destroy() { | 203 void GuestViewBase::Destroy() { |
| 204 content::RenderProcessHost* host = | |
| 205 content::RenderProcessHost::FromID(embedder_render_process_id()); | |
| 206 if (host) | |
| 207 host->RemoveObserver(this); | |
| 167 WillDestroy(); | 208 WillDestroy(); |
| 168 if (!destruction_callback_.is_null()) | 209 if (!destruction_callback_.is_null()) |
| 169 destruction_callback_.Run(); | 210 destruction_callback_.Run(); |
| 170 delete guest_web_contents(); | 211 delete guest_web_contents(); |
| 171 } | 212 } |
| 172 | 213 |
| 173 void GuestViewBase::DidAttach(content::WebContents* embedder_web_contents, | 214 void GuestViewBase::DidAttach(content::WebContents* embedder_web_contents, |
| 174 const base::DictionaryValue& extra_params) { | 215 const base::DictionaryValue& extra_params) { |
| 216 // After attachment, this GuestViewBase's lifetime is restricted to the | |
|
lazyboy
2014/06/17 23:46:45
Same as above, lifetime should also be documented
Fady Samuel
2014/06/18 21:08:33
Done.
| |
| 217 // lifetime of its embedder WebContents. Observing the RenderProcessHost | |
| 218 // of the embedder is no longer necessary. | |
| 219 embedder_web_contents->GetRenderProcessHost()->RemoveObserver(this); | |
| 175 embedder_web_contents_ = embedder_web_contents; | 220 embedder_web_contents_ = embedder_web_contents; |
| 176 embedder_web_contents_observer_.reset( | 221 embedder_web_contents_observer_.reset( |
| 177 new EmbedderWebContentsObserver(this)); | 222 new EmbedderWebContentsObserver(this)); |
| 178 embedder_render_process_id_ = | |
| 179 embedder_web_contents->GetRenderProcessHost()->GetID(); | |
| 180 extra_params.GetInteger(guestview::kParameterInstanceId, &view_instance_id_); | 223 extra_params.GetInteger(guestview::kParameterInstanceId, &view_instance_id_); |
| 181 extra_params_.reset(extra_params.DeepCopy()); | 224 extra_params_.reset(extra_params.DeepCopy()); |
| 182 | 225 |
| 183 // Give the derived class an opportunity to perform some actions. | 226 // Give the derived class an opportunity to perform some actions. |
| 184 DidAttach(); | 227 DidAttach(); |
| 185 | 228 |
| 186 // GuestViewBase::Attach is called prior to initialization (and initial | 229 // GuestViewBase::Attach is called prior to initialization (and initial |
| 187 // navigation) of the guest in the content layer in order to permit mapping | 230 // navigation) of the guest in the content layer in order to permit mapping |
| 188 // the necessary associations between the <*view> element and its guest. This | 231 // the necessary associations between the <*view> element and its guest. This |
| 189 // is needed by the <webview> WebRequest API to allow intercepting resource | 232 // is needed by the <webview> WebRequest API to allow intercepting resource |
| 190 // requests during navigation. However, queued events should be fired after | 233 // requests during navigation. However, queued events should be fired after |
| 191 // content layer initialization in order to ensure that load events (such as | 234 // content layer initialization in order to ensure that load events (such as |
| 192 // 'loadstop') fire in embedder after the contentWindow is available. | 235 // 'loadstop') fire in embedder after the contentWindow is available. |
| 193 if (!in_extension()) | 236 if (!in_extension()) |
| 194 return; | 237 return; |
| 195 | 238 |
| 196 base::MessageLoop::current()->PostTask( | 239 base::MessageLoop::current()->PostTask( |
| 197 FROM_HERE, | 240 FROM_HERE, |
| 198 base::Bind(&GuestViewBase::SendQueuedEvents, | 241 base::Bind(&GuestViewBase::SendQueuedEvents, |
| 199 weak_ptr_factory_.GetWeakPtr())); | 242 weak_ptr_factory_.GetWeakPtr())); |
| 200 } | 243 } |
| 201 | 244 |
| 245 int GuestViewBase::GetGuestInstanceID() const { | |
|
lazyboy
2014/06/17 23:46:45
Why do you add this method? to make it virtual? In
Fady Samuel
2014/06/18 21:08:33
Done.
| |
| 246 return guest_instance_id(); | |
| 247 } | |
| 248 | |
| 202 void GuestViewBase::SetOpener(GuestViewBase* guest) { | 249 void GuestViewBase::SetOpener(GuestViewBase* guest) { |
| 203 if (guest && guest->IsViewType(GetViewType())) { | 250 if (guest && guest->IsViewType(GetViewType())) { |
| 204 opener_ = guest->AsWeakPtr(); | 251 opener_ = guest->AsWeakPtr(); |
| 205 return; | 252 return; |
| 206 } | 253 } |
| 207 opener_ = base::WeakPtr<GuestViewBase>(); | 254 opener_ = base::WeakPtr<GuestViewBase>(); |
| 208 } | 255 } |
| 209 | 256 |
| 210 void GuestViewBase::RegisterDestructionCallback( | 257 void GuestViewBase::RegisterDestructionCallback( |
| 211 const DestructionCallback& callback) { | 258 const DestructionCallback& callback) { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 | 329 |
| 283 void GuestViewBase::SendQueuedEvents() { | 330 void GuestViewBase::SendQueuedEvents() { |
| 284 if (!attached()) | 331 if (!attached()) |
| 285 return; | 332 return; |
| 286 while (!pending_events_.empty()) { | 333 while (!pending_events_.empty()) { |
| 287 linked_ptr<Event> event_ptr = pending_events_.front(); | 334 linked_ptr<Event> event_ptr = pending_events_.front(); |
| 288 pending_events_.pop_front(); | 335 pending_events_.pop_front(); |
| 289 DispatchEvent(event_ptr.release()); | 336 DispatchEvent(event_ptr.release()); |
| 290 } | 337 } |
| 291 } | 338 } |
| OLD | NEW |