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 "chrome/browser/guest_view/ad_view/ad_view_guest.h" | 8 #include "chrome/browser/guest_view/ad_view/ad_view_guest.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 22 matching lines...) Expand all Loading... | |
| 33 : name_(name), args_(args.Pass()) { | 33 : name_(name), args_(args.Pass()) { |
| 34 } | 34 } |
| 35 | 35 |
| 36 GuestViewBase::Event::~Event() { | 36 GuestViewBase::Event::~Event() { |
| 37 } | 37 } |
| 38 | 38 |
| 39 scoped_ptr<base::DictionaryValue> GuestViewBase::Event::GetArguments() { | 39 scoped_ptr<base::DictionaryValue> GuestViewBase::Event::GetArguments() { |
| 40 return args_.Pass(); | 40 return args_.Pass(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 class GuestViewBase::EmbedderWebContentsObserver : public WebContentsObserver { | |
|
lazyboy
2014/05/29 19:13:58
Add a note saying this observer ensures we destroy
Fady Samuel
2014/05/29 21:21:12
Done.
| |
| 44 public: | |
| 45 explicit EmbedderWebContentsObserver(GuestViewBase* guest) | |
| 46 : WebContentsObserver(guest->embedder_web_contents()), | |
| 47 guest_(guest) { | |
| 48 } | |
| 49 | |
| 50 virtual ~EmbedderWebContentsObserver() { | |
| 51 } | |
| 52 | |
| 53 // WebContentsObserver implementation. | |
| 54 virtual void WebContentsDestroyed() OVERRIDE { | |
| 55 guest_->embedder_web_contents_ = NULL; | |
| 56 guest_->EmbedderDestroyed(); | |
| 57 guest_->Destroy(); | |
| 58 } | |
| 59 | |
| 60 private: | |
| 61 GuestViewBase* guest_; | |
| 62 | |
| 63 DISALLOW_COPY_AND_ASSIGN(EmbedderWebContentsObserver); | |
| 64 }; | |
| 65 | |
| 43 GuestViewBase::GuestViewBase(int guest_instance_id, | 66 GuestViewBase::GuestViewBase(int guest_instance_id, |
| 44 WebContents* guest_web_contents, | 67 WebContents* guest_web_contents, |
| 45 const std::string& embedder_extension_id) | 68 const std::string& embedder_extension_id) |
| 46 : guest_web_contents_(guest_web_contents), | 69 : WebContentsObserver(guest_web_contents), |
| 47 embedder_web_contents_(NULL), | 70 embedder_web_contents_(NULL), |
| 48 embedder_extension_id_(embedder_extension_id), | 71 embedder_extension_id_(embedder_extension_id), |
| 49 embedder_render_process_id_(0), | 72 embedder_render_process_id_(0), |
| 50 browser_context_(guest_web_contents->GetBrowserContext()), | 73 browser_context_(guest_web_contents->GetBrowserContext()), |
| 51 guest_instance_id_(guest_instance_id), | 74 guest_instance_id_(guest_instance_id), |
| 52 view_instance_id_(guestview::kInstanceIDNone), | 75 view_instance_id_(guestview::kInstanceIDNone), |
| 53 weak_ptr_factory_(this) { | 76 weak_ptr_factory_(this) { |
| 54 guest_web_contents->SetDelegate(this); | 77 guest_web_contents->SetDelegate(this); |
| 55 webcontents_guestview_map.Get().insert( | 78 webcontents_guestview_map.Get().insert( |
| 56 std::make_pair(guest_web_contents, this)); | 79 std::make_pair(guest_web_contents, this)); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 incognito)); | 171 incognito)); |
| 149 } | 172 } |
| 150 | 173 |
| 151 base::WeakPtr<GuestViewBase> GuestViewBase::AsWeakPtr() { | 174 base::WeakPtr<GuestViewBase> GuestViewBase::AsWeakPtr() { |
| 152 return weak_ptr_factory_.GetWeakPtr(); | 175 return weak_ptr_factory_.GetWeakPtr(); |
| 153 } | 176 } |
| 154 | 177 |
| 155 void GuestViewBase::Attach(content::WebContents* embedder_web_contents, | 178 void GuestViewBase::Attach(content::WebContents* embedder_web_contents, |
| 156 const base::DictionaryValue& args) { | 179 const base::DictionaryValue& args) { |
| 157 embedder_web_contents_ = embedder_web_contents; | 180 embedder_web_contents_ = embedder_web_contents; |
| 181 embedder_web_contents_observer_.reset( | |
| 182 new EmbedderWebContentsObserver(this)); | |
| 158 embedder_render_process_id_ = | 183 embedder_render_process_id_ = |
| 159 embedder_web_contents->GetRenderProcessHost()->GetID(); | 184 embedder_web_contents->GetRenderProcessHost()->GetID(); |
| 160 args.GetInteger(guestview::kParameterInstanceId, &view_instance_id_); | 185 args.GetInteger(guestview::kParameterInstanceId, &view_instance_id_); |
| 161 extra_params_.reset(args.DeepCopy()); | 186 extra_params_.reset(args.DeepCopy()); |
| 162 | 187 |
| 163 // GuestViewBase::Attach is called prior to initialization (and initial | 188 // GuestViewBase::Attach is called prior to initialization (and initial |
| 164 // navigation) of the guest in the content layer in order to permit mapping | 189 // navigation) of the guest in the content layer in order to permit mapping |
| 165 // the necessary associations between the <*view> element and its guest. This | 190 // the necessary associations between the <*view> element and its guest. This |
| 166 // is needed by the <webview> WebRequest API to allow intercepting resource | 191 // is needed by the <webview> WebRequest API to allow intercepting resource |
| 167 // requests during navigation. However, queued events should be fired after | 192 // requests during navigation. However, queued events should be fired after |
| 168 // content layer initialization in order to ensure that load events (such as | 193 // content layer initialization in order to ensure that load events (such as |
| 169 // 'loadstop') fire in embedder after the contentWindow is available. | 194 // 'loadstop') fire in embedder after the contentWindow is available. |
| 170 if (!in_extension()) | 195 if (!in_extension()) |
| 171 return; | 196 return; |
| 172 | 197 |
| 173 base::MessageLoop::current()->PostTask( | 198 base::MessageLoop::current()->PostTask( |
| 174 FROM_HERE, | 199 FROM_HERE, |
| 175 base::Bind(&GuestViewBase::SendQueuedEvents, | 200 base::Bind(&GuestViewBase::SendQueuedEvents, |
| 176 weak_ptr_factory_.GetWeakPtr())); | 201 weak_ptr_factory_.GetWeakPtr())); |
| 177 } | 202 } |
| 178 | 203 |
| 179 void GuestViewBase::Destroy() { | 204 void GuestViewBase::Destroy() { |
| 180 if (!destruction_callback_.is_null()) | 205 if (!destruction_callback_.is_null()) |
| 181 destruction_callback_.Run(guest_web_contents()); | 206 destruction_callback_.Run(); |
| 182 delete guest_web_contents(); | 207 delete guest_web_contents(); |
| 183 } | 208 } |
| 184 | 209 |
| 185 | 210 |
| 186 void GuestViewBase::SetOpener(GuestViewBase* guest) { | 211 void GuestViewBase::SetOpener(GuestViewBase* guest) { |
| 187 if (guest && guest->IsViewType(GetViewType())) { | 212 if (guest && guest->IsViewType(GetViewType())) { |
| 188 opener_ = guest->AsWeakPtr(); | 213 opener_ = guest->AsWeakPtr(); |
| 189 return; | 214 return; |
| 190 } | 215 } |
| 191 opener_ = base::WeakPtr<GuestViewBase>(); | 216 opener_ = base::WeakPtr<GuestViewBase>(); |
| 192 } | 217 } |
| 193 | 218 |
| 194 void GuestViewBase::RegisterDestructionCallback( | 219 void GuestViewBase::RegisterDestructionCallback( |
| 195 const DestructionCallback& callback) { | 220 const DestructionCallback& callback) { |
| 196 destruction_callback_ = callback; | 221 destruction_callback_ = callback; |
| 197 } | 222 } |
| 198 | 223 |
| 224 void GuestViewBase::WebContentsDestroyed() { | |
| 225 delete this; | |
| 226 } | |
| 227 | |
| 199 bool GuestViewBase::ShouldFocusPageAfterCrash() { | 228 bool GuestViewBase::ShouldFocusPageAfterCrash() { |
| 200 // Focus is managed elsewhere. | 229 // Focus is managed elsewhere. |
| 201 return false; | 230 return false; |
| 202 } | 231 } |
| 203 | 232 |
| 204 bool GuestViewBase::PreHandleGestureEvent(content::WebContents* source, | 233 bool GuestViewBase::PreHandleGestureEvent(content::WebContents* source, |
| 205 const blink::WebGestureEvent& event) { | 234 const blink::WebGestureEvent& event) { |
| 206 return event.type == blink::WebGestureEvent::GesturePinchBegin || | 235 return event.type == blink::WebGestureEvent::GesturePinchBegin || |
| 207 event.type == blink::WebGestureEvent::GesturePinchUpdate || | 236 event.type == blink::WebGestureEvent::GesturePinchUpdate || |
| 208 event.type == blink::WebGestureEvent::GesturePinchEnd; | 237 event.type == blink::WebGestureEvent::GesturePinchEnd; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 251 | 280 |
| 252 void GuestViewBase::SendQueuedEvents() { | 281 void GuestViewBase::SendQueuedEvents() { |
| 253 if (!attached()) | 282 if (!attached()) |
| 254 return; | 283 return; |
| 255 while (!pending_events_.empty()) { | 284 while (!pending_events_.empty()) { |
| 256 linked_ptr<Event> event_ptr = pending_events_.front(); | 285 linked_ptr<Event> event_ptr = pending_events_.front(); |
| 257 pending_events_.pop_front(); | 286 pending_events_.pop_front(); |
| 258 DispatchEvent(event_ptr.release()); | 287 DispatchEvent(event_ptr.release()); |
| 259 } | 288 } |
| 260 } | 289 } |
| OLD | NEW |