• 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/web_contents.h"
11 #include "extensions/browser/process_util.h"
12 #include "third_party/blink/public/common/input/web_gesture_event.h"
13 
14 using content::NativeWebKeyboardEvent;
15 using content::OpenURLParams;
16 using content::WebContents;
17 using content::WebContentsObserver;
18 
19 namespace extensions {
20 
CefExtensionViewHost(AlloyBrowserHostImpl * browser,const Extension * extension,content::WebContents * host_contents,const GURL & url,mojom::ViewType host_type)21 CefExtensionViewHost::CefExtensionViewHost(AlloyBrowserHostImpl* browser,
22                                            const Extension* extension,
23                                            content::WebContents* host_contents,
24                                            const GURL& url,
25                                            mojom::ViewType host_type)
26     : ExtensionHost(new CefExtensionHostDelegate(browser),
27                     extension,
28                     host_contents->GetBrowserContext(),
29                     host_contents,
30                     url,
31                     host_type) {
32   // Only used for dialogs and popups.
33   DCHECK(host_type == mojom::ViewType::kExtensionDialog ||
34          host_type == mojom::ViewType::kExtensionPopup);
35 }
36 
~CefExtensionViewHost()37 CefExtensionViewHost::~CefExtensionViewHost() {}
38 
OnDidStopFirstLoad()39 void CefExtensionViewHost::OnDidStopFirstLoad() {
40   // Nothing to do here, but don't call the base class method.
41 }
42 
LoadInitialURL()43 void CefExtensionViewHost::LoadInitialURL() {
44   if (process_util::GetPersistentBackgroundPageState(*extension(),
45                                                      browser_context()) ==
46       process_util::PersistentBackgroundPageState::kNotReady) {
47     // Make sure the background page loads before any others.
48     host_registry_observation_.Observe(
49         ExtensionHostRegistry::Get(browser_context()));
50     return;
51   }
52 
53   ExtensionHost::LoadInitialURL();
54 }
55 
IsBackgroundPage() const56 bool CefExtensionViewHost::IsBackgroundPage() const {
57   return false;
58 }
59 
ShouldAllowRendererInitiatedCrossProcessNavigation(bool is_main_frame_navigation)60 bool CefExtensionViewHost::ShouldAllowRendererInitiatedCrossProcessNavigation(
61     bool is_main_frame_navigation) {
62   // Block navigations that cause the main frame to navigate to non-extension
63   // content (i.e. to web content).
64   return !is_main_frame_navigation;
65 }
66 
PreHandleGestureEvent(content::WebContents * source,const blink::WebGestureEvent & event)67 bool CefExtensionViewHost::PreHandleGestureEvent(
68     content::WebContents* source,
69     const blink::WebGestureEvent& event) {
70   // Disable pinch zooming.
71   return blink::WebInputEvent::IsPinchGestureEventType(event.GetType());
72 }
73 
GetVisibleWebContents() const74 WebContents* CefExtensionViewHost::GetVisibleWebContents() const {
75   if (extension_host_type() == mojom::ViewType::kExtensionPopup)
76     return host_contents();
77   return nullptr;
78 }
79 
OnExtensionHostDocumentElementAvailable(content::BrowserContext * host_browser_context,ExtensionHost * extension_host)80 void CefExtensionViewHost::OnExtensionHostDocumentElementAvailable(
81     content::BrowserContext* host_browser_context,
82     ExtensionHost* extension_host) {
83   DCHECK(extension_host->extension());
84   if (host_browser_context != browser_context() ||
85       extension_host->extension() != extension() ||
86       extension_host->extension_host_type() !=
87           mojom::ViewType::kExtensionBackgroundPage) {
88     return;
89   }
90 
91   DCHECK_EQ(process_util::PersistentBackgroundPageState::kReady,
92             process_util::GetPersistentBackgroundPageState(*extension(),
93                                                            browser_context()));
94   // We only needed to wait for the background page to load, so stop observing.
95   host_registry_observation_.Reset();
96   LoadInitialURL();
97 }
98 
99 }  // namespace extensions
100