summaryrefslogtreecommitdiffstats
path: root/extensions/browser/guest_view/extensions_guest_view_manager_delegate.cc
blob: 854c6b289f657e328b059976af604503b4ed156a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/guest_view/extensions_guest_view_manager_delegate.h"

#include "components/guest_view/browser/guest_view_base.h"
#include "components/guest_view/browser/guest_view_manager.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/render_process_host.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/guest_view/app_view/app_view_guest.h"
#include "extensions/browser/guest_view/extension_options/extension_options_guest.h"
#include "extensions/browser/guest_view/extension_view/extension_view_guest.h"
#include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h"
#include "extensions/browser/guest_view/surface_worker/surface_worker_guest.h"
#include "extensions/browser/guest_view/web_view/web_view_guest.h"
#include "extensions/browser/process_manager.h"
#include "extensions/browser/process_map.h"
#include "extensions/common/features/feature.h"
#include "extensions/common/features/feature_provider.h"

using guest_view::GuestViewBase;
using guest_view::GuestViewManager;

namespace extensions {

ExtensionsGuestViewManagerDelegate::ExtensionsGuestViewManagerDelegate(
    content::BrowserContext* context)
    : context_(context) {
}

ExtensionsGuestViewManagerDelegate::~ExtensionsGuestViewManagerDelegate() {
}

void ExtensionsGuestViewManagerDelegate::DispatchEvent(
    const std::string& event_name,
    scoped_ptr<base::DictionaryValue> args,
    GuestViewBase* guest,
    int instance_id) {
  EventFilteringInfo info;
  info.SetInstanceID(instance_id);
  scoped_ptr<base::ListValue> event_args(new base::ListValue());
  event_args->Append(args.release());

  EventRouter::DispatchEvent(
      guest->owner_web_contents(),
      guest->browser_context(),
      guest->owner_host(),
      event_name,
      event_args.Pass(),
      EventRouter::USER_GESTURE_UNKNOWN,
      info);
}

bool ExtensionsGuestViewManagerDelegate::IsGuestAvailableToContext(
    GuestViewBase* guest) {
  const Feature* feature =
      FeatureProvider::GetAPIFeature(guest->GetAPINamespace());
  CHECK(feature);

  ProcessMap* process_map = ProcessMap::Get(context_);
  CHECK(process_map);

  const Extension* owner_extension = ProcessManager::Get(context_)->
      GetExtensionForWebContents(guest->owner_web_contents());

  // Ok for |owner_extension| to be nullptr, the embedder might be WebUI.
  Feature::Availability availability = feature->IsAvailableToContext(
      owner_extension,
      process_map->GetMostLikelyContextType(
          owner_extension,
          guest->owner_web_contents()->GetRenderProcessHost()->GetID()),
      guest->GetOwnerSiteURL());

  return availability.is_available();
}

bool ExtensionsGuestViewManagerDelegate::IsOwnedByExtension(
    GuestViewBase* guest) {
  return !!ProcessManager::Get(context_)->
      GetExtensionForWebContents(guest->owner_web_contents());
}

void ExtensionsGuestViewManagerDelegate::RegisterAdditionalGuestViewTypes() {
  GuestViewManager* manager = GuestViewManager::FromBrowserContext(context_);
  manager->RegisterGuestViewType<AppViewGuest>();
  manager->RegisterGuestViewType<ExtensionOptionsGuest>();
  manager->RegisterGuestViewType<ExtensionViewGuest>();
  manager->RegisterGuestViewType<MimeHandlerViewGuest>();
  manager->RegisterGuestViewType<SurfaceWorkerGuest>();
  manager->RegisterGuestViewType<WebViewGuest>();
}

}  // namespace extensions