| 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 DidInitialize(); |
| 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() { | 215 void GuestViewBase::DidAttach() { |
| 174 // Give the derived class an opportunity to perform some actions. | 216 // Give the derived class an opportunity to perform some actions. |
| 175 DidAttachToEmbedder(); | 217 DidAttachToEmbedder(); |
| 176 | 218 |
| 177 SendQueuedEvents(); | 219 SendQueuedEvents(); |
| 178 } | 220 } |
| 179 | 221 |
| 222 int GuestViewBase::GetGuestInstanceID() const { |
| 223 return guest_instance_id_; |
| 224 } |
| 225 |
| 180 void GuestViewBase::SetOpener(GuestViewBase* guest) { | 226 void GuestViewBase::SetOpener(GuestViewBase* guest) { |
| 181 if (guest && guest->IsViewType(GetViewType())) { | 227 if (guest && guest->IsViewType(GetViewType())) { |
| 182 opener_ = guest->AsWeakPtr(); | 228 opener_ = guest->AsWeakPtr(); |
| 183 return; | 229 return; |
| 184 } | 230 } |
| 185 opener_ = base::WeakPtr<GuestViewBase>(); | 231 opener_ = base::WeakPtr<GuestViewBase>(); |
| 186 } | 232 } |
| 187 | 233 |
| 188 void GuestViewBase::RegisterDestructionCallback( | 234 void GuestViewBase::RegisterDestructionCallback( |
| 189 const DestructionCallback& callback) { | 235 const DestructionCallback& callback) { |
| 190 destruction_callback_ = callback; | 236 destruction_callback_ = callback; |
| 191 } | 237 } |
| 192 | 238 |
| 193 void GuestViewBase::WillAttach(content::WebContents* embedder_web_contents, | 239 void GuestViewBase::WillAttach(content::WebContents* embedder_web_contents, |
| 194 const base::DictionaryValue& extra_params) { | 240 const base::DictionaryValue& extra_params) { |
| 241 // After attachment, this GuestViewBase's lifetime is restricted to the |
| 242 // lifetime of its embedder WebContents. Observing the RenderProcessHost |
| 243 // of the embedder is no longer necessary. |
| 244 embedder_web_contents->GetRenderProcessHost()->RemoveObserver(this); |
| 195 embedder_web_contents_ = embedder_web_contents; | 245 embedder_web_contents_ = embedder_web_contents; |
| 196 embedder_web_contents_observer_.reset( | 246 embedder_web_contents_observer_.reset( |
| 197 new EmbedderWebContentsObserver(this)); | 247 new EmbedderWebContentsObserver(this)); |
| 198 embedder_render_process_id_ = | |
| 199 embedder_web_contents->GetRenderProcessHost()->GetID(); | |
| 200 extra_params.GetInteger(guestview::kParameterInstanceId, &view_instance_id_); | 248 extra_params.GetInteger(guestview::kParameterInstanceId, &view_instance_id_); |
| 201 extra_params_.reset(extra_params.DeepCopy()); | 249 extra_params_.reset(extra_params.DeepCopy()); |
| 202 | 250 |
| 203 WillAttachToEmbedder(); | 251 WillAttachToEmbedder(); |
| 204 } | 252 } |
| 205 | 253 |
| 206 void GuestViewBase::DidStopLoading(content::RenderViewHost* render_view_host) { | 254 void GuestViewBase::DidStopLoading(content::RenderViewHost* render_view_host) { |
| 207 if (!IsDragAndDropEnabled()) { | 255 if (!IsDragAndDropEnabled()) { |
| 208 const char script[] = "window.addEventListener('dragstart', function() { " | 256 const char script[] = "window.addEventListener('dragstart', function() { " |
| 209 " window.event.preventDefault(); " | 257 " window.event.preventDefault(); " |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 | 321 |
| 274 void GuestViewBase::SendQueuedEvents() { | 322 void GuestViewBase::SendQueuedEvents() { |
| 275 if (!attached()) | 323 if (!attached()) |
| 276 return; | 324 return; |
| 277 while (!pending_events_.empty()) { | 325 while (!pending_events_.empty()) { |
| 278 linked_ptr<Event> event_ptr = pending_events_.front(); | 326 linked_ptr<Event> event_ptr = pending_events_.front(); |
| 279 pending_events_.pop_front(); | 327 pending_events_.pop_front(); |
| 280 DispatchEvent(event_ptr.release()); | 328 DispatchEvent(event_ptr.release()); |
| 281 } | 329 } |
| 282 } | 330 } |
| OLD | NEW |