| 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, |
| 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, | |
| 159 const base::DictionaryValue& args) { | |
| 160 embedder_web_contents_ = embedder_web_contents; | |
| 161 embedder_web_contents_observer_.reset( | |
| 162 new EmbedderWebContentsObserver(this)); | |
| 163 embedder_render_process_id_ = | |
| 164 embedder_web_contents->GetRenderProcessHost()->GetID(); | |
| 165 args.GetInteger(guestview::kParameterInstanceId, &view_instance_id_); | |
| 166 extra_params_.reset(args.DeepCopy()); | |
| 167 | |
| 168 // GuestViewBase::Attach is called prior to initialization (and initial | |
| 169 // 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 | |
| 171 // is needed by the <webview> WebRequest API to allow intercepting resource | |
| 172 // requests during navigation. However, queued events should be fired after | |
| 173 // content layer initialization in order to ensure that load events (such as | |
| 174 // 'loadstop') fire in embedder after the contentWindow is available. | |
| 175 if (!in_extension()) | |
| 176 return; | |
| 177 | |
| 178 base::MessageLoop::current()->PostTask( | |
| 179 FROM_HERE, | |
| 180 base::Bind(&GuestViewBase::SendQueuedEvents, | |
| 181 weak_ptr_factory_.GetWeakPtr())); | |
| 182 } | |
| 183 | |
| 184 void GuestViewBase::Destroy() { | 166 void GuestViewBase::Destroy() { |
| 185 WillDestroy(); | 167 WillDestroy(); |
| 186 if (!destruction_callback_.is_null()) | 168 if (!destruction_callback_.is_null()) |
| 187 destruction_callback_.Run(); | 169 destruction_callback_.Run(); |
| 188 delete guest_web_contents(); | 170 delete guest_web_contents(); |
| 189 } | 171 } |
| 190 | 172 |
| 173 void GuestViewBase::DidAttach() { |
| 174 // Give the derived class an opportunity to perform some actions. |
| 175 DidAttachToEmbedder(); |
| 176 |
| 177 SendQueuedEvents(); |
| 178 } |
| 191 | 179 |
| 192 void GuestViewBase::SetOpener(GuestViewBase* guest) { | 180 void GuestViewBase::SetOpener(GuestViewBase* guest) { |
| 193 if (guest && guest->IsViewType(GetViewType())) { | 181 if (guest && guest->IsViewType(GetViewType())) { |
| 194 opener_ = guest->AsWeakPtr(); | 182 opener_ = guest->AsWeakPtr(); |
| 195 return; | 183 return; |
| 196 } | 184 } |
| 197 opener_ = base::WeakPtr<GuestViewBase>(); | 185 opener_ = base::WeakPtr<GuestViewBase>(); |
| 198 } | 186 } |
| 199 | 187 |
| 200 void GuestViewBase::RegisterDestructionCallback( | 188 void GuestViewBase::RegisterDestructionCallback( |
| 201 const DestructionCallback& callback) { | 189 const DestructionCallback& callback) { |
| 202 destruction_callback_ = callback; | 190 destruction_callback_ = callback; |
| 203 } | 191 } |
| 204 | 192 |
| 193 void GuestViewBase::WillAttach(content::WebContents* embedder_web_contents, |
| 194 const base::DictionaryValue& extra_params) { |
| 195 embedder_web_contents_ = embedder_web_contents; |
| 196 embedder_web_contents_observer_.reset( |
| 197 new EmbedderWebContentsObserver(this)); |
| 198 embedder_render_process_id_ = |
| 199 embedder_web_contents->GetRenderProcessHost()->GetID(); |
| 200 extra_params.GetInteger(guestview::kParameterInstanceId, &view_instance_id_); |
| 201 extra_params_.reset(extra_params.DeepCopy()); |
| 202 |
| 203 WillAttachToEmbedder(); |
| 204 } |
| 205 |
| 205 void GuestViewBase::DidStopLoading(content::RenderViewHost* render_view_host) { | 206 void GuestViewBase::DidStopLoading(content::RenderViewHost* render_view_host) { |
| 206 if (!IsDragAndDropEnabled()) { | 207 if (!IsDragAndDropEnabled()) { |
| 207 const char script[] = "window.addEventListener('dragstart', function() { " | 208 const char script[] = "window.addEventListener('dragstart', function() { " |
| 208 " window.event.preventDefault(); " | 209 " window.event.preventDefault(); " |
| 209 "});"; | 210 "});"; |
| 210 render_view_host->GetMainFrame()->ExecuteJavaScript( | 211 render_view_host->GetMainFrame()->ExecuteJavaScript( |
| 211 base::ASCIIToUTF16(script)); | 212 base::ASCIIToUTF16(script)); |
| 212 } | 213 } |
| 213 DidStopLoading(); | 214 DidStopLoading(); |
| 214 } | 215 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 | 273 |
| 273 void GuestViewBase::SendQueuedEvents() { | 274 void GuestViewBase::SendQueuedEvents() { |
| 274 if (!attached()) | 275 if (!attached()) |
| 275 return; | 276 return; |
| 276 while (!pending_events_.empty()) { | 277 while (!pending_events_.empty()) { |
| 277 linked_ptr<Event> event_ptr = pending_events_.front(); | 278 linked_ptr<Event> event_ptr = pending_events_.front(); |
| 278 pending_events_.pop_front(); | 279 pending_events_.pop_front(); |
| 279 DispatchEvent(event_ptr.release()); | 280 DispatchEvent(event_ptr.release()); |
| 280 } | 281 } |
| 281 } | 282 } |
| OLD | NEW |