Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / guest_view / extensions_guest_view_manager_delegate.cc
bloba5ae79bf448a9f4c6041ea835f7323e8ff40e4fe
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "extensions/browser/guest_view/extensions_guest_view_manager_delegate.h"
7 #include "components/guest_view/browser/guest_view_base.h"
8 #include "components/guest_view/browser/guest_view_manager.h"
9 #include "components/guest_view/common/guest_view_constants.h"
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/render_process_host.h"
12 #include "extensions/browser/api/extensions_api_client.h"
13 #include "extensions/browser/event_router.h"
14 #include "extensions/browser/guest_view/app_view/app_view_guest.h"
15 #include "extensions/browser/guest_view/extension_options/extension_options_guest.h"
16 #include "extensions/browser/guest_view/extension_view/extension_view_guest.h"
17 #include "extensions/browser/guest_view/guest_view_events.h"
18 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h"
19 #include "extensions/browser/guest_view/web_view/web_view_guest.h"
20 #include "extensions/browser/process_manager.h"
21 #include "extensions/browser/process_map.h"
22 #include "extensions/common/features/feature.h"
23 #include "extensions/common/features/feature_provider.h"
25 using guest_view::GuestViewBase;
26 using guest_view::GuestViewManager;
28 namespace extensions {
30 ExtensionsGuestViewManagerDelegate::ExtensionsGuestViewManagerDelegate(
31 content::BrowserContext* context)
32 : context_(context) {
35 ExtensionsGuestViewManagerDelegate::~ExtensionsGuestViewManagerDelegate() {
38 void ExtensionsGuestViewManagerDelegate::DispatchEvent(
39 const std::string& event_name,
40 scoped_ptr<base::DictionaryValue> args,
41 GuestViewBase* guest,
42 int instance_id) {
43 EventFilteringInfo info;
44 info.SetInstanceID(instance_id);
45 scoped_ptr<base::ListValue> event_args(new base::ListValue());
46 event_args->Append(args.release());
48 // GetEventHistogramValue maps guest view event names to their histogram
49 // value. It needs to be like this because the guest view component doesn't
50 // know about extensions, so GuestViewEvent can't have an
51 // extensions::events::HistogramValue as an argument.
52 events::HistogramValue histogram_value =
53 guest_view_events::GetEventHistogramValue(event_name);
54 DCHECK_NE(events::UNKNOWN, histogram_value) << "Event " << event_name
55 << " must have a histogram value";
57 content::WebContents* owner = guest->owner_web_contents();
58 EventRouter::DispatchEventToSender(
59 owner, guest->browser_context(), guest->owner_host(), histogram_value,
60 event_name, event_args.Pass(), EventRouter::USER_GESTURE_UNKNOWN, info);
63 bool ExtensionsGuestViewManagerDelegate::IsGuestAvailableToContext(
64 GuestViewBase* guest) {
65 const Feature* feature =
66 FeatureProvider::GetAPIFeature(guest->GetAPINamespace());
67 CHECK(feature);
69 ProcessMap* process_map = ProcessMap::Get(context_);
70 CHECK(process_map);
72 const Extension* owner_extension = ProcessManager::Get(context_)->
73 GetExtensionForWebContents(guest->owner_web_contents());
75 // Ok for |owner_extension| to be nullptr, the embedder might be WebUI.
76 Feature::Availability availability = feature->IsAvailableToContext(
77 owner_extension,
78 process_map->GetMostLikelyContextType(
79 owner_extension,
80 guest->owner_web_contents()->GetRenderProcessHost()->GetID()),
81 guest->GetOwnerSiteURL());
83 return availability.is_available();
86 bool ExtensionsGuestViewManagerDelegate::IsOwnedByExtension(
87 GuestViewBase* guest) {
88 return !!ProcessManager::Get(context_)->
89 GetExtensionForWebContents(guest->owner_web_contents());
92 void ExtensionsGuestViewManagerDelegate::RegisterAdditionalGuestViewTypes() {
93 GuestViewManager* manager = GuestViewManager::FromBrowserContext(context_);
94 manager->RegisterGuestViewType<AppViewGuest>();
95 manager->RegisterGuestViewType<ExtensionOptionsGuest>();
96 manager->RegisterGuestViewType<ExtensionViewGuest>();
97 manager->RegisterGuestViewType<MimeHandlerViewGuest>();
98 manager->RegisterGuestViewType<WebViewGuest>();
101 } // namespace extensions