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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 59 guest_->EmbedderDestroyed(); | 59 guest_->EmbedderDestroyed(); |
| 60 guest_->Destroy(); | 60 guest_->Destroy(); |
| 61 } | 61 } |
| 62 | 62 |
| 63 private: | 63 private: |
| 64 GuestViewBase* guest_; | 64 GuestViewBase* guest_; |
| 65 | 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(EmbedderWebContentsObserver); | 66 DISALLOW_COPY_AND_ASSIGN(EmbedderWebContentsObserver); |
| 67 }; | 67 }; |
| 68 | 68 |
| 69 GuestViewBase::GuestViewBase(int guest_instance_id, | 69 GuestViewBase::GuestViewBase(int guest_instance_id) |
| 70 WebContents* guest_web_contents, | 70 : embedder_web_contents_(NULL), |
| 71 const std::string& embedder_extension_id) | |
| 72 : WebContentsObserver(guest_web_contents), | |
| 73 embedder_web_contents_(NULL), | |
| 74 embedder_extension_id_(embedder_extension_id), | |
| 75 embedder_render_process_id_(0), | 71 embedder_render_process_id_(0), |
| 76 browser_context_(guest_web_contents->GetBrowserContext()), | 72 browser_context_(NULL), |
| 77 guest_instance_id_(guest_instance_id), | 73 guest_instance_id_(guest_instance_id), |
| 78 view_instance_id_(guestview::kInstanceIDNone), | 74 view_instance_id_(guestview::kInstanceIDNone), |
| 75 initialized_(false), | |
| 79 weak_ptr_factory_(this) { | 76 weak_ptr_factory_(this) { |
| 77 } | |
| 78 | |
| 79 void GuestViewBase::Init(WebContents* guest_web_contents, | |
|
lazyboy
2014/06/13 17:40:01
Let's add a comment/todo since it's not obvious wh
Fady Samuel
2014/06/16 14:35:40
Done.
| |
| 80 const std::string& embedder_extension_id) { | |
| 81 if (initialized_) | |
| 82 return; | |
| 83 initialized_ = true; | |
| 84 browser_context_ = guest_web_contents->GetBrowserContext(); | |
| 85 embedder_extension_id_ = embedder_extension_id; | |
| 86 | |
| 87 WebContentsObserver::Observe(guest_web_contents); | |
| 80 guest_web_contents->SetDelegate(this); | 88 guest_web_contents->SetDelegate(this); |
| 81 webcontents_guestview_map.Get().insert( | 89 webcontents_guestview_map.Get().insert( |
| 82 std::make_pair(guest_web_contents, this)); | 90 std::make_pair(guest_web_contents, this)); |
| 83 GuestViewManager::FromBrowserContext(browser_context_)-> | 91 GuestViewManager::FromBrowserContext(browser_context_)-> |
| 84 AddGuest(guest_instance_id_, guest_web_contents); | 92 AddGuest(guest_instance_id_, guest_web_contents); |
| 85 } | 93 } |
| 86 | 94 |
| 87 // static | 95 // static |
| 88 GuestViewBase* GuestViewBase::Create( | 96 GuestViewBase* GuestViewBase::Create( |
| 89 int guest_instance_id, | 97 int guest_instance_id, |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 } | 156 } |
| 149 | 157 |
| 150 base::WeakPtr<GuestViewBase> GuestViewBase::AsWeakPtr() { | 158 base::WeakPtr<GuestViewBase> GuestViewBase::AsWeakPtr() { |
| 151 return weak_ptr_factory_.GetWeakPtr(); | 159 return weak_ptr_factory_.GetWeakPtr(); |
| 152 } | 160 } |
| 153 | 161 |
| 154 bool GuestViewBase::IsDragAndDropEnabled() const { | 162 bool GuestViewBase::IsDragAndDropEnabled() const { |
| 155 return false; | 163 return false; |
| 156 } | 164 } |
| 157 | 165 |
| 158 void GuestViewBase::Attach(content::WebContents* embedder_web_contents, | 166 void GuestViewBase::Destroy() { |
| 159 const base::DictionaryValue& args) { | 167 WillDestroy(); |
| 168 if (!destruction_callback_.is_null()) | |
| 169 destruction_callback_.Run(); | |
| 170 delete guest_web_contents(); | |
| 171 } | |
| 172 | |
| 173 void GuestViewBase::DidAttach(content::WebContents* embedder_web_contents, | |
| 174 const base::DictionaryValue& extra_params) { | |
| 160 embedder_web_contents_ = embedder_web_contents; | 175 embedder_web_contents_ = embedder_web_contents; |
| 161 embedder_web_contents_observer_.reset( | 176 embedder_web_contents_observer_.reset( |
| 162 new EmbedderWebContentsObserver(this)); | 177 new EmbedderWebContentsObserver(this)); |
| 163 embedder_render_process_id_ = | 178 embedder_render_process_id_ = |
| 164 embedder_web_contents->GetRenderProcessHost()->GetID(); | 179 embedder_web_contents->GetRenderProcessHost()->GetID(); |
| 165 args.GetInteger(guestview::kParameterInstanceId, &view_instance_id_); | 180 extra_params.GetInteger(guestview::kParameterInstanceId, &view_instance_id_); |
| 166 extra_params_.reset(args.DeepCopy()); | 181 extra_params_.reset(extra_params.DeepCopy()); |
| 182 | |
| 183 // Give the derived class an opportunity to perform some actions. | |
| 184 DidAttach(); | |
| 167 | 185 |
| 168 // GuestViewBase::Attach is called prior to initialization (and initial | 186 // GuestViewBase::Attach is called prior to initialization (and initial |
| 169 // navigation) of the guest in the content layer in order to permit mapping | 187 // navigation) of the guest in the content layer in order to permit mapping |
| 170 // the necessary associations between the <*view> element and its guest. This | 188 // the necessary associations between the <*view> element and its guest. This |
| 171 // is needed by the <webview> WebRequest API to allow intercepting resource | 189 // is needed by the <webview> WebRequest API to allow intercepting resource |
| 172 // requests during navigation. However, queued events should be fired after | 190 // requests during navigation. However, queued events should be fired after |
| 173 // content layer initialization in order to ensure that load events (such as | 191 // content layer initialization in order to ensure that load events (such as |
| 174 // 'loadstop') fire in embedder after the contentWindow is available. | 192 // 'loadstop') fire in embedder after the contentWindow is available. |
| 175 if (!in_extension()) | 193 if (!in_extension()) |
| 176 return; | 194 return; |
| 177 | 195 |
| 178 base::MessageLoop::current()->PostTask( | 196 base::MessageLoop::current()->PostTask( |
| 179 FROM_HERE, | 197 FROM_HERE, |
| 180 base::Bind(&GuestViewBase::SendQueuedEvents, | 198 base::Bind(&GuestViewBase::SendQueuedEvents, |
| 181 weak_ptr_factory_.GetWeakPtr())); | 199 weak_ptr_factory_.GetWeakPtr())); |
| 182 } | 200 } |
| 183 | 201 |
| 184 void GuestViewBase::Destroy() { | |
| 185 WillDestroy(); | |
| 186 if (!destruction_callback_.is_null()) | |
| 187 destruction_callback_.Run(); | |
| 188 delete guest_web_contents(); | |
| 189 } | |
| 190 | |
| 191 | |
| 192 void GuestViewBase::SetOpener(GuestViewBase* guest) { | 202 void GuestViewBase::SetOpener(GuestViewBase* guest) { |
| 193 if (guest && guest->IsViewType(GetViewType())) { | 203 if (guest && guest->IsViewType(GetViewType())) { |
| 194 opener_ = guest->AsWeakPtr(); | 204 opener_ = guest->AsWeakPtr(); |
| 195 return; | 205 return; |
| 196 } | 206 } |
| 197 opener_ = base::WeakPtr<GuestViewBase>(); | 207 opener_ = base::WeakPtr<GuestViewBase>(); |
| 198 } | 208 } |
| 199 | 209 |
| 200 void GuestViewBase::RegisterDestructionCallback( | 210 void GuestViewBase::RegisterDestructionCallback( |
| 201 const DestructionCallback& callback) { | 211 const DestructionCallback& callback) { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 272 | 282 |
| 273 void GuestViewBase::SendQueuedEvents() { | 283 void GuestViewBase::SendQueuedEvents() { |
| 274 if (!attached()) | 284 if (!attached()) |
| 275 return; | 285 return; |
| 276 while (!pending_events_.empty()) { | 286 while (!pending_events_.empty()) { |
| 277 linked_ptr<Event> event_ptr = pending_events_.front(); | 287 linked_ptr<Event> event_ptr = pending_events_.front(); |
| 278 pending_events_.pop_front(); | 288 pending_events_.pop_front(); |
| 279 DispatchEvent(event_ptr.release()); | 289 DispatchEvent(event_ptr.release()); |
| 280 } | 290 } |
| 281 } | 291 } |
| OLD | NEW |