| 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 "extensions/browser/guest_view/guest_view_base.h" | 5 #include "extensions/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 "components/ui/zoom/page_zoom.h" | 9 #include "components/ui/zoom/page_zoom.h" |
| 10 #include "components/ui/zoom/zoom_controller.h" | 10 #include "components/ui/zoom/zoom_controller.h" |
| 11 #include "content/public/browser/navigation_details.h" | 11 #include "content/public/browser/navigation_details.h" |
| 12 #include "content/public/browser/render_frame_host.h" | 12 #include "content/public/browser/render_frame_host.h" |
| 13 #include "content/public/browser/render_process_host.h" | 13 #include "content/public/browser/render_process_host.h" |
| 14 #include "content/public/browser/render_view_host.h" | 14 #include "content/public/browser/render_view_host.h" |
| 15 #include "content/public/browser/render_widget_host_view.h" | 15 #include "content/public/browser/render_widget_host_view.h" |
| 16 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/common/page_zoom.h" | 17 #include "content/public/common/page_zoom.h" |
| 18 #include "content/public/common/url_constants.h" | 18 #include "content/public/common/url_constants.h" |
| 19 #include "extensions/browser/api/extensions_api_client.h" | |
| 20 #include "extensions/browser/event_router.h" | |
| 21 #include "extensions/browser/extension_registry.h" | |
| 22 #include "extensions/browser/guest_view/app_view/app_view_guest.h" | 19 #include "extensions/browser/guest_view/app_view/app_view_guest.h" |
| 23 #include "extensions/browser/guest_view/extension_options/extension_options_gues
t.h" | 20 #include "extensions/browser/guest_view/extension_options/extension_options_gues
t.h" |
| 24 #include "extensions/browser/guest_view/extension_view/extension_view_guest.h" | 21 #include "extensions/browser/guest_view/extension_view/extension_view_guest.h" |
| 22 #include "extensions/browser/guest_view/guest_view_event.h" |
| 25 #include "extensions/browser/guest_view/guest_view_manager.h" | 23 #include "extensions/browser/guest_view/guest_view_manager.h" |
| 26 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_gues
t.h" | 24 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_gues
t.h" |
| 27 #include "extensions/browser/guest_view/surface_worker/surface_worker_guest.h" | 25 #include "extensions/browser/guest_view/surface_worker/surface_worker_guest.h" |
| 28 #include "extensions/browser/guest_view/web_view/web_view_guest.h" | 26 #include "extensions/browser/guest_view/web_view/web_view_guest.h" |
| 29 #include "extensions/browser/process_manager.h" | |
| 30 #include "extensions/browser/process_map.h" | |
| 31 #include "extensions/common/features/feature.h" | |
| 32 #include "extensions/common/features/feature_provider.h" | |
| 33 #include "extensions/common/guest_view/guest_view_constants.h" | 27 #include "extensions/common/guest_view/guest_view_constants.h" |
| 34 #include "extensions/common/guest_view/guest_view_messages.h" | 28 #include "extensions/common/guest_view/guest_view_messages.h" |
| 35 #include "third_party/WebKit/public/web/WebInputEvent.h" | 29 #include "third_party/WebKit/public/web/WebInputEvent.h" |
| 36 | 30 |
| 37 using content::WebContents; | 31 using content::WebContents; |
| 38 | 32 |
| 39 namespace content { | 33 namespace content { |
| 40 struct FrameNavigateParams; | 34 struct FrameNavigateParams; |
| 41 } | 35 } |
| 42 | 36 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 53 static base::LazyInstance<WebContentsGuestViewMap> webcontents_guestview_map = | 47 static base::LazyInstance<WebContentsGuestViewMap> webcontents_guestview_map = |
| 54 LAZY_INSTANCE_INITIALIZER; | 48 LAZY_INSTANCE_INITIALIZER; |
| 55 | 49 |
| 56 } // namespace | 50 } // namespace |
| 57 | 51 |
| 58 SetSizeParams::SetSizeParams() { | 52 SetSizeParams::SetSizeParams() { |
| 59 } | 53 } |
| 60 SetSizeParams::~SetSizeParams() { | 54 SetSizeParams::~SetSizeParams() { |
| 61 } | 55 } |
| 62 | 56 |
| 63 GuestViewBase::Event::Event(const std::string& name, | |
| 64 scoped_ptr<base::DictionaryValue> args) | |
| 65 : name_(name), args_(args.Pass()) { | |
| 66 } | |
| 67 | |
| 68 GuestViewBase::Event::~Event() { | |
| 69 } | |
| 70 | |
| 71 scoped_ptr<base::DictionaryValue> GuestViewBase::Event::GetArguments() { | |
| 72 return args_.Pass(); | |
| 73 } | |
| 74 | |
| 75 // This observer ensures that the GuestViewBase destroys itself when its | 57 // This observer ensures that the GuestViewBase destroys itself when its |
| 76 // embedder goes away. It also tracks when the embedder's fullscreen is | 58 // embedder goes away. It also tracks when the embedder's fullscreen is |
| 77 // toggled so the guest can change itself accordingly. | 59 // toggled so the guest can change itself accordingly. |
| 78 class GuestViewBase::OwnerContentsObserver : public WebContentsObserver { | 60 class GuestViewBase::OwnerContentsObserver : public WebContentsObserver { |
| 79 public: | 61 public: |
| 80 OwnerContentsObserver(GuestViewBase* guest, | 62 OwnerContentsObserver(GuestViewBase* guest, |
| 81 content::WebContents* embedder_web_contents) | 63 content::WebContents* embedder_web_contents) |
| 82 : WebContentsObserver(embedder_web_contents), | 64 : WebContentsObserver(embedder_web_contents), |
| 83 is_fullscreen_(false), | 65 is_fullscreen_(false), |
| 84 destroyed_(false), | 66 destroyed_(false), |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 guest_proxy_routing_id_(MSG_ROUTING_NONE), | 169 guest_proxy_routing_id_(MSG_ROUTING_NONE), |
| 188 weak_ptr_factory_(this) { | 170 weak_ptr_factory_(this) { |
| 189 } | 171 } |
| 190 | 172 |
| 191 void GuestViewBase::Init(const base::DictionaryValue& create_params, | 173 void GuestViewBase::Init(const base::DictionaryValue& create_params, |
| 192 const WebContentsCreatedCallback& callback) { | 174 const WebContentsCreatedCallback& callback) { |
| 193 if (initialized_) | 175 if (initialized_) |
| 194 return; | 176 return; |
| 195 initialized_ = true; | 177 initialized_ = true; |
| 196 | 178 |
| 197 const Feature* feature = FeatureProvider::GetAPIFeature(GetAPINamespace()); | 179 if (!GuestViewManager::FromBrowserContext(browser_context_)-> |
| 198 CHECK(feature); | 180 IsGuestAvailableToContext(this, &owner_extension_id_)) { |
| 199 | |
| 200 ProcessMap* process_map = ProcessMap::Get(browser_context()); | |
| 201 CHECK(process_map); | |
| 202 | |
| 203 const Extension* owner_extension = | |
| 204 ProcessManager::Get(owner_web_contents()->GetBrowserContext())-> | |
| 205 GetExtensionForWebContents(owner_web_contents()); | |
| 206 owner_extension_id_ = owner_extension ? owner_extension->id() : std::string(); | |
| 207 | |
| 208 // Ok for |owner_extension| to be nullptr, the embedder might be WebUI. | |
| 209 Feature::Availability availability = feature->IsAvailableToContext( | |
| 210 owner_extension, | |
| 211 process_map->GetMostLikelyContextType( | |
| 212 owner_extension, | |
| 213 owner_web_contents()->GetRenderProcessHost()->GetID()), | |
| 214 GetOwnerSiteURL()); | |
| 215 if (!availability.is_available()) { | |
| 216 // The derived class did not create a WebContents so this class serves no | 181 // The derived class did not create a WebContents so this class serves no |
| 217 // purpose. Let's self-destruct. | 182 // purpose. Let's self-destruct. |
| 218 delete this; | 183 delete this; |
| 219 callback.Run(nullptr); | 184 callback.Run(nullptr); |
| 220 return; | 185 return; |
| 221 } | 186 } |
| 222 | 187 |
| 223 scoped_ptr<base::DictionaryValue> params(create_params.DeepCopy()); | 188 scoped_ptr<base::DictionaryValue> params(create_params.DeepCopy()); |
| 224 CreateWebContents(create_params, | 189 CreateWebContents(create_params, |
| 225 base::Bind(&GuestViewBase::CompleteInit, | 190 base::Bind(&GuestViewBase::CompleteInit, |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 const gfx::Size& new_size) { | 250 const gfx::Size& new_size) { |
| 286 if (new_size == old_size) | 251 if (new_size == old_size) |
| 287 return; | 252 return; |
| 288 | 253 |
| 289 // Dispatch the onResize event. | 254 // Dispatch the onResize event. |
| 290 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | 255 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); |
| 291 args->SetInteger(guestview::kOldWidth, old_size.width()); | 256 args->SetInteger(guestview::kOldWidth, old_size.width()); |
| 292 args->SetInteger(guestview::kOldHeight, old_size.height()); | 257 args->SetInteger(guestview::kOldHeight, old_size.height()); |
| 293 args->SetInteger(guestview::kNewWidth, new_size.width()); | 258 args->SetInteger(guestview::kNewWidth, new_size.width()); |
| 294 args->SetInteger(guestview::kNewHeight, new_size.height()); | 259 args->SetInteger(guestview::kNewHeight, new_size.height()); |
| 295 DispatchEventToGuestProxy(new Event(guestview::kEventResize, args.Pass())); | 260 DispatchEventToGuestProxy( |
| 261 new GuestViewEvent(guestview::kEventResize, args.Pass())); |
| 296 } | 262 } |
| 297 | 263 |
| 298 gfx::Size GuestViewBase::GetDefaultSize() const { | 264 gfx::Size GuestViewBase::GetDefaultSize() const { |
| 299 if (is_full_page_plugin()) { | 265 if (is_full_page_plugin()) { |
| 300 // Full page plugins default to the size of the owner's viewport. | 266 // Full page plugins default to the size of the owner's viewport. |
| 301 return owner_web_contents() | 267 return owner_web_contents() |
| 302 ->GetRenderWidgetHostView() | 268 ->GetRenderWidgetHostView() |
| 303 ->GetVisibleViewportSize(); | 269 ->GetVisibleViewportSize(); |
| 304 } else { | 270 } else { |
| 305 return gfx::Size(guestview::kDefaultWidth, guestview::kDefaultHeight); | 271 return gfx::Size(guestview::kDefaultWidth, guestview::kDefaultHeight); |
| (...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 731 EmbedderZoomChanged(data.old_zoom_level, data.new_zoom_level); | 697 EmbedderZoomChanged(data.old_zoom_level, data.new_zoom_level); |
| 732 return; | 698 return; |
| 733 } | 699 } |
| 734 | 700 |
| 735 if (data.web_contents == web_contents()) { | 701 if (data.web_contents == web_contents()) { |
| 736 // The guest's zoom level has changed. | 702 // The guest's zoom level has changed. |
| 737 GuestZoomChanged(data.old_zoom_level, data.new_zoom_level); | 703 GuestZoomChanged(data.old_zoom_level, data.new_zoom_level); |
| 738 } | 704 } |
| 739 } | 705 } |
| 740 | 706 |
| 741 void GuestViewBase::DispatchEventToGuestProxy(Event* event) { | 707 void GuestViewBase::DispatchEventToGuestProxy(GuestViewEvent* event) { |
| 742 DispatchEvent(event, guest_instance_id_); | 708 event->Dispatch(this, guest_instance_id_); |
| 743 } | 709 } |
| 744 | 710 |
| 745 void GuestViewBase::DispatchEventToView(Event* event) { | 711 void GuestViewBase::DispatchEventToView(GuestViewEvent* event) { |
| 746 if (!attached() && | 712 if (!attached() && |
| 747 (!CanRunInDetachedState() || !can_owner_receive_events())) { | 713 (!CanRunInDetachedState() || !can_owner_receive_events())) { |
| 748 pending_events_.push_back(linked_ptr<Event>(event)); | 714 pending_events_.push_back(linked_ptr<GuestViewEvent>(event)); |
| 749 return; | 715 return; |
| 750 } | 716 } |
| 751 | 717 |
| 752 DispatchEvent(event, view_instance_id_); | 718 event->Dispatch(this, view_instance_id_); |
| 753 } | |
| 754 | |
| 755 void GuestViewBase::DispatchEvent(Event* event, int instance_id) { | |
| 756 scoped_ptr<Event> event_ptr(event); | |
| 757 | |
| 758 EventFilteringInfo info; | |
| 759 info.SetInstanceID(instance_id); | |
| 760 scoped_ptr<base::ListValue> args(new base::ListValue()); | |
| 761 args->Append(event->GetArguments().release()); | |
| 762 | |
| 763 EventRouter::DispatchEvent( | |
| 764 owner_web_contents_, | |
| 765 browser_context_, | |
| 766 owner_extension_id_, | |
| 767 event->name(), | |
| 768 args.Pass(), | |
| 769 EventRouter::USER_GESTURE_UNKNOWN, | |
| 770 info); | |
| 771 } | 719 } |
| 772 | 720 |
| 773 void GuestViewBase::SendQueuedEvents() { | 721 void GuestViewBase::SendQueuedEvents() { |
| 774 if (!attached()) | 722 if (!attached()) |
| 775 return; | 723 return; |
| 776 while (!pending_events_.empty()) { | 724 while (!pending_events_.empty()) { |
| 777 linked_ptr<Event> event_ptr = pending_events_.front(); | 725 linked_ptr<GuestViewEvent> event_ptr = pending_events_.front(); |
| 778 pending_events_.pop_front(); | 726 pending_events_.pop_front(); |
| 779 DispatchEvent(event_ptr.release(), view_instance_id_); | 727 event_ptr.release()->Dispatch(this, view_instance_id_); |
| 780 } | 728 } |
| 781 } | 729 } |
| 782 | 730 |
| 783 void GuestViewBase::CompleteInit( | 731 void GuestViewBase::CompleteInit( |
| 784 scoped_ptr<base::DictionaryValue> create_params, | 732 scoped_ptr<base::DictionaryValue> create_params, |
| 785 const WebContentsCreatedCallback& callback, | 733 const WebContentsCreatedCallback& callback, |
| 786 content::WebContents* guest_web_contents) { | 734 content::WebContents* guest_web_contents) { |
| 787 if (!guest_web_contents) { | 735 if (!guest_web_contents) { |
| 788 // The derived class did not create a WebContents so this class serves no | 736 // The derived class did not create a WebContents so this class serves no |
| 789 // purpose. Let's self-destruct. | 737 // purpose. Let's self-destruct. |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 894 void GuestViewBase::RegisterGuestViewTypes() { | 842 void GuestViewBase::RegisterGuestViewTypes() { |
| 895 AppViewGuest::Register(); | 843 AppViewGuest::Register(); |
| 896 ExtensionOptionsGuest::Register(); | 844 ExtensionOptionsGuest::Register(); |
| 897 ExtensionViewGuest::Register(); | 845 ExtensionViewGuest::Register(); |
| 898 MimeHandlerViewGuest::Register(); | 846 MimeHandlerViewGuest::Register(); |
| 899 SurfaceWorkerGuest::Register(); | 847 SurfaceWorkerGuest::Register(); |
| 900 WebViewGuest::Register(); | 848 WebViewGuest::Register(); |
| 901 } | 849 } |
| 902 | 850 |
| 903 } // namespace extensions | 851 } // namespace extensions |
| OLD | NEW |