2020-09-18 00:24:08 +02:00
|
|
|
// Copyright 2020 The Chromium Embedded Framework Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#ifndef CEF_LIBCEF_BROWSER_BROWSER_HOST_BASE_H_
|
|
|
|
#define CEF_LIBCEF_BROWSER_BROWSER_HOST_BASE_H_
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "include/cef_browser.h"
|
|
|
|
#include "include/cef_client.h"
|
|
|
|
#include "include/views/cef_browser_view.h"
|
|
|
|
#include "libcef/browser/browser_contents_delegate.h"
|
|
|
|
#include "libcef/browser/browser_info.h"
|
2020-09-25 03:40:47 +02:00
|
|
|
#include "libcef/browser/browser_platform_delegate.h"
|
2021-03-18 18:17:33 +01:00
|
|
|
#include "libcef/browser/devtools/devtools_manager.h"
|
2020-09-18 00:24:08 +02:00
|
|
|
#include "libcef/browser/frame_host_impl.h"
|
|
|
|
#include "libcef/browser/request_context_impl.h"
|
|
|
|
|
|
|
|
#include "base/observer_list.h"
|
|
|
|
#include "base/synchronization/lock.h"
|
|
|
|
#include "extensions/common/view_type.h"
|
|
|
|
|
|
|
|
namespace extensions {
|
|
|
|
class Extension;
|
|
|
|
}
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
// Parameters that are passed to the runtime-specific Create methods.
|
|
|
|
struct CefBrowserCreateParams {
|
|
|
|
CefBrowserCreateParams() {}
|
|
|
|
|
|
|
|
// Copy constructor used with the chrome runtime only.
|
|
|
|
CefBrowserCreateParams(const CefBrowserCreateParams& that) {
|
|
|
|
operator=(that);
|
|
|
|
}
|
|
|
|
CefBrowserCreateParams& operator=(const CefBrowserCreateParams& that) {
|
|
|
|
// Not all parameters can be copied.
|
|
|
|
client = that.client;
|
|
|
|
url = that.url;
|
|
|
|
settings = that.settings;
|
|
|
|
request_context = that.request_context;
|
|
|
|
extra_info = that.extra_info;
|
2021-02-18 02:58:25 +01:00
|
|
|
#if defined(TOOLKIT_VIEWS)
|
|
|
|
browser_view = that.browser_view;
|
|
|
|
#endif
|
2020-09-25 03:40:47 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Platform-specific window creation info. Will be nullptr when creating a
|
|
|
|
// views-hosted browser. Currently used with the alloy runtime only.
|
|
|
|
std::unique_ptr<CefWindowInfo> window_info;
|
2020-09-18 00:24:08 +02:00
|
|
|
|
2021-02-18 02:58:25 +01:00
|
|
|
#if defined(TOOLKIT_VIEWS)
|
|
|
|
// The BrowserView that will own a Views-hosted browser. Will be nullptr for
|
2021-04-02 22:53:17 +02:00
|
|
|
// popup browsers.
|
2020-09-25 03:40:47 +02:00
|
|
|
CefRefPtr<CefBrowserView> browser_view;
|
2021-04-02 22:53:17 +02:00
|
|
|
|
|
|
|
// True if this browser is a popup and has a Views-hosted opener, in which
|
|
|
|
// case the BrowserView for this browser will be created later (from
|
|
|
|
// PopupWebContentsCreated).
|
|
|
|
bool popup_with_views_hosted_opener = false;
|
2020-09-18 00:24:08 +02:00
|
|
|
#endif
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
// Client implementation. May be nullptr.
|
|
|
|
CefRefPtr<CefClient> client;
|
2020-09-18 00:24:08 +02:00
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
// Initial URL to load. May be empty. If this is a valid extension URL then
|
|
|
|
// the browser will be created as an app view extension host.
|
2021-04-15 01:28:22 +02:00
|
|
|
CefString url;
|
2020-09-18 00:24:08 +02:00
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
// Browser settings.
|
|
|
|
CefBrowserSettings settings;
|
2020-09-18 00:24:08 +02:00
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
// Other browser that opened this DevTools browser. Will be nullptr for non-
|
|
|
|
// DevTools browsers. Currently used with the alloy runtime only.
|
|
|
|
CefRefPtr<CefBrowserHostBase> devtools_opener;
|
2020-09-18 00:24:08 +02:00
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
// Request context to use when creating the browser. If nullptr the global
|
|
|
|
// request context will be used.
|
|
|
|
CefRefPtr<CefRequestContext> request_context;
|
2020-09-18 00:24:08 +02:00
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
// Extra information that will be passed to
|
|
|
|
// CefRenderProcessHandler::OnBrowserCreated.
|
|
|
|
CefRefPtr<CefDictionaryValue> extra_info;
|
2020-09-18 00:24:08 +02:00
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
// Used when explicitly creating the browser as an extension host via
|
|
|
|
// ProcessManager::CreateBackgroundHost. Currently used with the alloy
|
|
|
|
// runtime only.
|
|
|
|
const extensions::Extension* extension = nullptr;
|
|
|
|
extensions::ViewType extension_host_type = extensions::VIEW_TYPE_INVALID;
|
|
|
|
};
|
2020-09-18 00:24:08 +02:00
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
// Base class for CefBrowserHost implementations. Includes functionality that is
|
|
|
|
// shared by the alloy and chrome runtimes. All methods are thread-safe unless
|
|
|
|
// otherwise indicated.
|
|
|
|
class CefBrowserHostBase : public CefBrowserHost,
|
|
|
|
public CefBrowser,
|
|
|
|
public CefBrowserContentsDelegate::Observer {
|
|
|
|
public:
|
2020-09-18 00:24:08 +02:00
|
|
|
// Interface to implement for observers that wish to be informed of changes
|
|
|
|
// to the CefBrowserHostBase. All methods will be called on the UI thread.
|
|
|
|
class Observer : public base::CheckedObserver {
|
|
|
|
public:
|
|
|
|
// Called before |browser| is destroyed. Any references to |browser| should
|
|
|
|
// be cleared when this method is called.
|
|
|
|
virtual void OnBrowserDestroyed(CefBrowserHostBase* browser) = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~Observer() {}
|
|
|
|
};
|
|
|
|
|
2021-02-18 02:58:25 +01:00
|
|
|
// Create a new CefBrowserHost instance of the current runtime type with
|
|
|
|
// owned WebContents.
|
|
|
|
static CefRefPtr<CefBrowserHostBase> Create(
|
|
|
|
CefBrowserCreateParams& create_params);
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
// Returns the browser associated with the specified RenderViewHost.
|
|
|
|
static CefRefPtr<CefBrowserHostBase> GetBrowserForHost(
|
|
|
|
const content::RenderViewHost* host);
|
|
|
|
// Returns the browser associated with the specified RenderFrameHost.
|
|
|
|
static CefRefPtr<CefBrowserHostBase> GetBrowserForHost(
|
|
|
|
const content::RenderFrameHost* host);
|
|
|
|
// Returns the browser associated with the specified WebContents.
|
|
|
|
static CefRefPtr<CefBrowserHostBase> GetBrowserForContents(
|
|
|
|
const content::WebContents* contents);
|
|
|
|
// Returns the browser associated with the specified FrameTreeNode ID.
|
|
|
|
static CefRefPtr<CefBrowserHostBase> GetBrowserForFrameTreeNode(
|
|
|
|
int frame_tree_node_id);
|
|
|
|
// Returns the browser associated with the specified frame routing IDs.
|
|
|
|
static CefRefPtr<CefBrowserHostBase> GetBrowserForFrameRoute(
|
|
|
|
int render_process_id,
|
|
|
|
int render_routing_id);
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
CefBrowserHostBase(
|
|
|
|
const CefBrowserSettings& settings,
|
|
|
|
CefRefPtr<CefClient> client,
|
|
|
|
std::unique_ptr<CefBrowserPlatformDelegate> platform_delegate,
|
|
|
|
scoped_refptr<CefBrowserInfo> browser_info,
|
|
|
|
CefRefPtr<CefRequestContextImpl> request_context);
|
2020-09-18 00:24:08 +02:00
|
|
|
|
|
|
|
// Called on the UI thread after the associated WebContents is created.
|
|
|
|
virtual void InitializeBrowser();
|
|
|
|
|
2021-02-18 02:58:25 +01:00
|
|
|
// Called on the UI thread when the OS window hosting the browser is
|
|
|
|
// destroyed.
|
|
|
|
virtual void WindowDestroyed() = 0;
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
// Called on the UI thread after the associated WebContents is destroyed.
|
|
|
|
// Also called from CefBrowserInfoManager::DestroyAllBrowsers if the browser
|
|
|
|
// was not properly shut down.
|
|
|
|
virtual void DestroyBrowser();
|
|
|
|
|
|
|
|
// CefBrowserHost methods:
|
|
|
|
CefRefPtr<CefBrowser> GetBrowser() override;
|
|
|
|
CefRefPtr<CefClient> GetClient() override;
|
|
|
|
CefRefPtr<CefRequestContext> GetRequestContext() override;
|
2021-02-18 02:58:25 +01:00
|
|
|
bool HasView() override;
|
2020-09-18 00:24:08 +02:00
|
|
|
void StartDownload(const CefString& url) override;
|
|
|
|
void DownloadImage(const CefString& image_url,
|
|
|
|
bool is_favicon,
|
|
|
|
uint32 max_image_size,
|
|
|
|
bool bypass_cache,
|
|
|
|
CefRefPtr<CefDownloadImageCallback> callback) override;
|
|
|
|
void ReplaceMisspelling(const CefString& word) override;
|
|
|
|
void AddWordToDictionary(const CefString& word) override;
|
2020-09-25 03:40:47 +02:00
|
|
|
void SendKeyEvent(const CefKeyEvent& event) override;
|
|
|
|
void SendMouseClickEvent(const CefMouseEvent& event,
|
|
|
|
MouseButtonType type,
|
|
|
|
bool mouseUp,
|
|
|
|
int clickCount) override;
|
|
|
|
void SendMouseMoveEvent(const CefMouseEvent& event, bool mouseLeave) override;
|
|
|
|
void SendMouseWheelEvent(const CefMouseEvent& event,
|
|
|
|
int deltaX,
|
|
|
|
int deltaY) override;
|
2021-03-18 18:17:33 +01:00
|
|
|
bool SendDevToolsMessage(const void* message, size_t message_size) override;
|
|
|
|
int ExecuteDevToolsMethod(int message_id,
|
|
|
|
const CefString& method,
|
|
|
|
CefRefPtr<CefDictionaryValue> params) override;
|
|
|
|
CefRefPtr<CefRegistration> AddDevToolsMessageObserver(
|
|
|
|
CefRefPtr<CefDevToolsMessageObserver> observer) override;
|
2020-09-18 00:24:08 +02:00
|
|
|
void GetNavigationEntries(CefRefPtr<CefNavigationEntryVisitor> visitor,
|
|
|
|
bool current_only) override;
|
|
|
|
CefRefPtr<CefNavigationEntry> GetVisibleNavigationEntry() override;
|
|
|
|
|
|
|
|
// CefBrowser methods:
|
|
|
|
CefRefPtr<CefBrowserHost> GetHost() override;
|
|
|
|
bool CanGoBack() override;
|
2020-09-25 03:40:47 +02:00
|
|
|
void GoBack() override;
|
2020-09-18 00:24:08 +02:00
|
|
|
bool CanGoForward() override;
|
2020-09-25 03:40:47 +02:00
|
|
|
void GoForward() override;
|
2020-09-18 00:24:08 +02:00
|
|
|
bool IsLoading() override;
|
2020-09-25 03:40:47 +02:00
|
|
|
void Reload() override;
|
|
|
|
void ReloadIgnoreCache() override;
|
|
|
|
void StopLoad() override;
|
2020-09-18 00:24:08 +02:00
|
|
|
int GetIdentifier() override;
|
|
|
|
bool IsSame(CefRefPtr<CefBrowser> that) override;
|
|
|
|
bool HasDocument() override;
|
|
|
|
bool IsPopup() override;
|
|
|
|
CefRefPtr<CefFrame> GetMainFrame() override;
|
|
|
|
CefRefPtr<CefFrame> GetFocusedFrame() override;
|
|
|
|
CefRefPtr<CefFrame> GetFrame(int64 identifier) override;
|
|
|
|
CefRefPtr<CefFrame> GetFrame(const CefString& name) override;
|
|
|
|
size_t GetFrameCount() override;
|
|
|
|
void GetFrameIdentifiers(std::vector<int64>& identifiers) override;
|
|
|
|
void GetFrameNames(std::vector<CefString>& names) override;
|
|
|
|
|
|
|
|
// CefBrowserContentsDelegate::Observer methods:
|
|
|
|
void OnStateChanged(CefBrowserContentsState state_changed) override;
|
2020-09-25 03:40:47 +02:00
|
|
|
void OnWebContentsDestroyed(content::WebContents* web_contents) override;
|
2020-09-18 00:24:08 +02:00
|
|
|
|
|
|
|
// Returns the frame associated with the specified RenderFrameHost.
|
|
|
|
CefRefPtr<CefFrame> GetFrameForHost(const content::RenderFrameHost* host);
|
|
|
|
|
|
|
|
// Returns the frame associated with the specified FrameTreeNode ID.
|
|
|
|
CefRefPtr<CefFrame> GetFrameForFrameTreeNode(int frame_tree_node_id);
|
|
|
|
|
|
|
|
// Manage observer objects. The observer must either outlive this object or
|
|
|
|
// be removed before destruction. Must be called on the UI thread.
|
|
|
|
void AddObserver(Observer* observer);
|
|
|
|
void RemoveObserver(Observer* observer);
|
|
|
|
bool HasObserver(Observer* observer) const;
|
|
|
|
|
|
|
|
// Methods called from CefFrameHostImpl.
|
2020-09-25 03:40:47 +02:00
|
|
|
void LoadMainFrameURL(const content::OpenURLParams& params);
|
2020-09-18 00:24:08 +02:00
|
|
|
void OnDidFinishLoad(CefRefPtr<CefFrameHostImpl> frame,
|
|
|
|
const GURL& validated_url,
|
|
|
|
int http_status_code);
|
|
|
|
virtual void OnSetFocus(cef_focus_source_t source) = 0;
|
2020-09-25 03:40:47 +02:00
|
|
|
void ViewText(const std::string& text);
|
|
|
|
|
|
|
|
// Called from CefBrowserInfoManager::MaybeAllowNavigation.
|
|
|
|
virtual bool MaybeAllowNavigation(content::RenderFrameHost* opener,
|
|
|
|
bool is_guest_view,
|
|
|
|
const content::OpenURLParams& params);
|
2020-09-18 00:24:08 +02:00
|
|
|
|
|
|
|
// Helpers for executing client callbacks. Must be called on the UI thread.
|
|
|
|
void OnAfterCreated();
|
|
|
|
void OnBeforeClose();
|
|
|
|
void OnBrowserDestroyed();
|
|
|
|
|
|
|
|
// Thread-safe accessors.
|
|
|
|
const CefBrowserSettings& settings() const { return settings_; }
|
|
|
|
CefRefPtr<CefClient> client() const { return client_; }
|
|
|
|
scoped_refptr<CefBrowserInfo> browser_info() const { return browser_info_; }
|
|
|
|
int browser_id() const;
|
|
|
|
CefRefPtr<CefRequestContextImpl> request_context() const {
|
|
|
|
return request_context_;
|
|
|
|
}
|
2021-02-18 02:58:25 +01:00
|
|
|
bool is_views_hosted() const { return is_views_hosted_; }
|
2021-04-18 03:12:54 +02:00
|
|
|
SkColor GetBackgroundColor() const;
|
|
|
|
|
|
|
|
// Returns true if windowless rendering is enabled.
|
|
|
|
virtual bool IsWindowless() const;
|
2020-09-18 00:24:08 +02:00
|
|
|
|
|
|
|
// Accessors that must be called on the UI thread.
|
|
|
|
content::WebContents* GetWebContents() const;
|
|
|
|
content::BrowserContext* GetBrowserContext() const;
|
2020-09-25 03:40:47 +02:00
|
|
|
CefBrowserPlatformDelegate* platform_delegate() const {
|
|
|
|
return platform_delegate_.get();
|
|
|
|
}
|
2020-09-18 00:24:08 +02:00
|
|
|
CefBrowserContentsDelegate* contents_delegate() const {
|
|
|
|
return contents_delegate_.get();
|
|
|
|
}
|
|
|
|
|
2021-02-18 02:58:25 +01:00
|
|
|
#if defined(TOOLKIT_VIEWS)
|
|
|
|
// Returns the Widget owner for the browser window. Only used with windowed
|
|
|
|
// rendering.
|
|
|
|
views::Widget* GetWindowWidget() const;
|
|
|
|
|
|
|
|
// Returns the BrowserView associated with this browser. Only used with Views-
|
|
|
|
// based browsers.
|
|
|
|
CefRefPtr<CefBrowserView> GetBrowserView() const;
|
|
|
|
#endif
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
protected:
|
2021-03-18 18:17:33 +01:00
|
|
|
bool EnsureDevToolsManager();
|
|
|
|
void InitializeDevToolsRegistrationOnUIThread(
|
|
|
|
CefRefPtr<CefRegistration> registration);
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
// Called from LoadMainFrameURL to perform the actual navigation.
|
|
|
|
virtual bool Navigate(const content::OpenURLParams& params);
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
// Thread-safe members.
|
|
|
|
CefBrowserSettings settings_;
|
|
|
|
CefRefPtr<CefClient> client_;
|
2020-09-25 03:40:47 +02:00
|
|
|
std::unique_ptr<CefBrowserPlatformDelegate> platform_delegate_;
|
2020-09-18 00:24:08 +02:00
|
|
|
scoped_refptr<CefBrowserInfo> browser_info_;
|
|
|
|
CefRefPtr<CefRequestContextImpl> request_context_;
|
2021-02-18 02:58:25 +01:00
|
|
|
const bool is_views_hosted_;
|
2020-09-18 00:24:08 +02:00
|
|
|
|
|
|
|
// Only accessed on the UI thread.
|
|
|
|
std::unique_ptr<CefBrowserContentsDelegate> contents_delegate_;
|
|
|
|
|
|
|
|
// Observers that want to be notified of changes to this object.
|
|
|
|
// Only accessed on the UI thread.
|
|
|
|
base::ObserverList<Observer> observers_;
|
|
|
|
|
|
|
|
// Volatile state accessed from multiple threads. All access must be protected
|
|
|
|
// by |state_lock_|.
|
|
|
|
base::Lock state_lock_;
|
|
|
|
bool is_loading_ = false;
|
|
|
|
bool can_go_back_ = false;
|
|
|
|
bool can_go_forward_ = false;
|
|
|
|
bool has_document_ = false;
|
|
|
|
bool is_fullscreen_ = false;
|
|
|
|
CefRefPtr<CefFrameHostImpl> focused_frame_;
|
|
|
|
|
2021-03-18 18:17:33 +01:00
|
|
|
// Used for creating and managing DevTools instances.
|
|
|
|
std::unique_ptr<CefDevToolsManager> devtools_manager_;
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
private:
|
|
|
|
IMPLEMENT_REFCOUNTING(CefBrowserHostBase);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefBrowserHostBase);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // CEF_LIBCEF_BROWSER_BROWSER_HOST_BASE_H_
|