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