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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_VIEW_HOST_H_
#define CHROME_BROWSER_EXTENSIONS_EXTENSION_VIEW_HOST_H_
#include <memory>
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "build/build_config.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/extension_host_registry.h"
#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/extensions/extension_view_host_web_modal_handler.h"
#endif // !BUILDFLAG(IS_ANDROID)
namespace content {
class SiteInstance;
class WebContents;
}
namespace extensions {
class ExtensionView;
// The ExtensionHost for an extension that backs a view in the browser UI. For
// example, this could be an extension popup or dialog, but not a background
// page.
class ExtensionViewHost
: public ExtensionHost,
public ExtensionHostRegistry::Observer {
public:
class Delegate {
public:
Delegate(const Delegate&) = delete;
Delegate& operator=(const Delegate&) = delete;
virtual ~Delegate();
// Opens a URL with the given disposition.
virtual content::WebContents* OpenURL(
const content::OpenURLParams& params,
base::OnceCallback<void(content::NavigationHandle&)>
navigation_handle_callback) = 0;
// Allows handling keyboard events before sending to the renderer.
virtual content::KeyboardEventProcessingResult PreHandleKeyboardEvent(
content::WebContents* source,
const input::NativeWebKeyboardEvent& event) = 0;
// Called when an eye dropper should open. Returns the eye dropper window.
virtual std::unique_ptr<content::EyeDropper> OpenEyeDropper(
content::RenderFrameHost* frame,
content::EyeDropperListener* listener) = 0;
// Returns the WindowController associated with this ExtensionViewHost, or
// nullptr if no window is associated with the delegate.
virtual WindowController* GetExtensionWindowController() const = 0;
protected:
Delegate();
};
ExtensionViewHost(const Extension* extension,
content::SiteInstance* site_instance,
content::BrowserContext* browser_context,
const GURL& url,
mojom::ViewType host_type,
std::unique_ptr<Delegate> delegate);
ExtensionViewHost(const ExtensionViewHost&) = delete;
ExtensionViewHost& operator=(const ExtensionViewHost&) = delete;
~ExtensionViewHost() override;
void set_view(ExtensionView* view) { view_ = view; }
ExtensionView* view() { return view_; }
// ExtensionHost
void OnDidStopFirstLoad() override;
void LoadInitialURL() override;
bool IsBackgroundPage() const override;
// content::WebContentsObserver:
void ReadyToCommitNavigation(
content::NavigationHandle* navigation_handle) override;
// content::WebContentsDelegate
content::WebContents* OpenURLFromTab(
content::WebContents* source,
const content::OpenURLParams& params,
base::OnceCallback<void(content::NavigationHandle&)>
navigation_handle_callback) override;
bool ShouldAllowRendererInitiatedCrossProcessNavigation(
bool is_outermost_main_frame_navigation) override;
content::KeyboardEventProcessingResult PreHandleKeyboardEvent(
content::WebContents* source,
const input::NativeWebKeyboardEvent& event) override;
bool HandleKeyboardEvent(content::WebContents* source,
const input::NativeWebKeyboardEvent& event) override;
bool PreHandleGestureEvent(content::WebContents* source,
const blink::WebGestureEvent& event) override;
void RunFileChooser(content::RenderFrameHost* render_frame_host,
scoped_refptr<content::FileSelectListener> listener,
const blink::mojom::FileChooserParams& params) override;
std::unique_ptr<content::EyeDropper> OpenEyeDropper(
content::RenderFrameHost* frame,
content::EyeDropperListener* listener) override;
void ResizeDueToAutoResize(content::WebContents* source,
const gfx::Size& new_size) override;
// content::WebContentsObserver
void RenderFrameCreated(content::RenderFrameHost* frame_host) override;
// extensions::ExtensionFunctionDispatcher::Delegate
WindowController* GetExtensionWindowController() const override;
content::WebContents* GetVisibleWebContents() const override;
// ExtensionHostRegistry::Observer:
void OnExtensionHostDocumentElementAvailable(
content::BrowserContext* browser_context,
ExtensionHost* extension_host) override;
private:
// Returns whether the provided event is a raw escape keypress in a
// mojom::ViewType::kExtensionPopup.
bool IsEscapeInPopup(const input::NativeWebKeyboardEvent& event) const;
// Handles keyboard events that were not handled by HandleKeyboardEvent().
// Platform specific implementation may override this method to handle the
// event in platform specific way. Returns whether the events are handled.
bool UnhandledKeyboardEvent(content::WebContents* source,
const input::NativeWebKeyboardEvent& event);
std::unique_ptr<Delegate> delegate_;
// View that shows the rendered content in the UI.
raw_ptr<ExtensionView, DanglingUntriaged> view_ = nullptr;
#if !BUILDFLAG(IS_ANDROID)
std::unique_ptr<ExtensionViewHostWebModalHandler> web_modal_handler_;
#endif // !BUILDFLAG(IS_ANDROID)
base::ScopedObservation<ExtensionHostRegistry,
ExtensionHostRegistry::Observer>
host_registry_observation_{this};
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_VIEW_HOST_H_
|