• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 the Chromium Embedded Framework Authors. Portions copyright
2 // 2013 The Chromium Authors. All rights reserved. Use of this source code is
3 // governed by a BSD-style license that can be found in the LICENSE file.
4 
5 #include "libcef/browser/extensions/extension_view_host.h"
6 
7 #include "libcef/browser/browser_platform_delegate.h"
8 #include "libcef/browser/extensions/extension_host_delegate.h"
9 
10 #include "content/public/browser/notification_source.h"
11 #include "content/public/browser/web_contents.h"
12 #include "extensions/browser/extension_system.h"
13 #include "extensions/browser/notification_types.h"
14 #include "extensions/browser/runtime_data.h"
15 #include "third_party/blink/public/common/input/web_gesture_event.h"
16 
17 using content::NativeWebKeyboardEvent;
18 using content::OpenURLParams;
19 using content::WebContents;
20 using content::WebContentsObserver;
21 
22 namespace extensions {
23 
CefExtensionViewHost(AlloyBrowserHostImpl * browser,const Extension * extension,content::WebContents * host_contents,const GURL & url,mojom::ViewType host_type)24 CefExtensionViewHost::CefExtensionViewHost(AlloyBrowserHostImpl* browser,
25                                            const Extension* extension,
26                                            content::WebContents* host_contents,
27                                            const GURL& url,
28                                            mojom::ViewType host_type)
29     : ExtensionHost(new CefExtensionHostDelegate(browser),
30                     extension,
31                     host_contents->GetBrowserContext(),
32                     host_contents,
33                     url,
34                     host_type) {
35   // Only used for dialogs and popups.
36   DCHECK(host_type == mojom::ViewType::kExtensionDialog ||
37          host_type == mojom::ViewType::kExtensionPopup);
38 }
39 
~CefExtensionViewHost()40 CefExtensionViewHost::~CefExtensionViewHost() {}
41 
OnDidStopFirstLoad()42 void CefExtensionViewHost::OnDidStopFirstLoad() {
43   // Nothing to do here, but don't call the base class method.
44 }
45 
LoadInitialURL()46 void CefExtensionViewHost::LoadInitialURL() {
47   if (!ExtensionSystem::Get(browser_context())
48            ->runtime_data()
49            ->IsBackgroundPageReady(extension())) {
50     // Make sure the background page loads before any others.
51     registrar_.Add(this,
52                    extensions::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
53                    content::Source<Extension>(extension()));
54     return;
55   }
56 
57   ExtensionHost::LoadInitialURL();
58 }
59 
IsBackgroundPage() const60 bool CefExtensionViewHost::IsBackgroundPage() const {
61   return false;
62 }
63 
ShouldTransferNavigation(bool is_main_frame_navigation)64 bool CefExtensionViewHost::ShouldTransferNavigation(
65     bool is_main_frame_navigation) {
66   // Block navigations that cause the main frame to navigate to non-extension
67   // content (i.e. to web content).
68   return !is_main_frame_navigation;
69 }
70 
PreHandleGestureEvent(content::WebContents * source,const blink::WebGestureEvent & event)71 bool CefExtensionViewHost::PreHandleGestureEvent(
72     content::WebContents* source,
73     const blink::WebGestureEvent& event) {
74   // Disable pinch zooming.
75   return blink::WebInputEvent::IsPinchGestureEventType(event.GetType());
76 }
77 
GetVisibleWebContents() const78 WebContents* CefExtensionViewHost::GetVisibleWebContents() const {
79   if (extension_host_type() == mojom::ViewType::kExtensionPopup)
80     return host_contents();
81   return nullptr;
82 }
83 
Observe(int type,const content::NotificationSource & source,const content::NotificationDetails & details)84 void CefExtensionViewHost::Observe(
85     int type,
86     const content::NotificationSource& source,
87     const content::NotificationDetails& details) {
88   DCHECK_EQ(type, extensions::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY);
89   DCHECK(ExtensionSystem::Get(browser_context())
90              ->runtime_data()
91              ->IsBackgroundPageReady(extension()));
92   LoadInitialURL();
93 }
94 
95 }  // namespace extensions
96