#ifndef UI_VIEWS_CONTROLS_WEBVIEW_WEBVIEW_H_
#define UI_VIEWS_CONTROLS_WEBVIEW_WEBVIEW_H_
#include <stdint.h>
#include <memory>
#include "base/callback_list.h"
#include "base/functional/callback.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_contents_observer.h"
#include "ui/accessibility/platform/ax_mode_observer.h"
#include "ui/accessibility/platform/ax_platform.h"
#include "ui/gfx/native_ui_types.h"
#include "ui/views/controls/native/native_view_host.h"
#include "ui/views/controls/webview/webview_export.h"
#include "ui/views/metadata/view_factory.h"
#include "ui/views/view.h"
#include "ui/views/view_tracker.h"
class GURL;
namespace content {
class BrowserContext;
class WebContents;
}
namespace views {
class WEBVIEW_EXPORT WebView : public View,
public content::WebContentsDelegate,
public content::WebContentsObserver,
public ui::AXModeObserver {
METADATA_HEADER(WebView, View)
public:
enum class HttpsUpgradePolicy {
kAllowUpgrade,
kNoUpgrade,
};
using WebContentsAttachedCallback = base::RepeatingCallback<void(WebView*)>;
using WebContentsDetachedCallback = base::RepeatingCallback<void(WebView*)>;
using WebContentsFocusedCallback = base::RepeatingCallback<void(WebView*)>;
explicit WebView(content::BrowserContext* browser_context = nullptr);
WebView(const WebView&) = delete;
WebView& operator=(const WebView&) = delete;
~WebView() override;
static bool IsWebViewContents(const content::WebContents* web_contents);
content::WebContents* GetWebContents(
const GURL& url = GURL(),
base::Location creator_location = base::Location::Current());
virtual void SetWebContents(content::WebContents* web_contents);
content::BrowserContext* GetBrowserContext();
void SetBrowserContext(content::BrowserContext* browser_context);
void LoadInitialURL(
const GURL& url,
HttpsUpgradePolicy https_upgrade_policy =
HttpsUpgradePolicy::kAllowUpgrade,
base::Location invoke_location = base::Location::Current());
void SetFastResize(bool fast_resize);
bool GetFastResize() const;
void EnableSizingFromWebContents(const gfx::Size& min_size,
const gfx::Size& max_size);
void SetResizeBackgroundColor(SkColor resize_background_color);
void SetCrashedOverlayView(View* crashed_overlay_view);
base::CallbackListSubscription AddWebContentsAttachedCallback(
WebContentsAttachedCallback callback);
base::CallbackListSubscription AddWebContentsDetachedCallback(
WebContentsDetachedCallback callback);
base::CallbackListSubscription AddWebContentsFocusedCallback(
WebContentsFocusedCallback callback);
void set_is_primary_web_contents_for_window(bool is_primary) {
is_primary_web_contents_for_window_ = is_primary;
}
void set_allow_accelerators(bool allow_accelerators) {
allow_accelerators_ = allow_accelerators;
}
void set_lock_child_ax_tree_id_override(bool lock) {
lock_child_ax_tree_id_override_ = lock;
}
void ResizeDueToAutoResize(content::WebContents* source,
const gfx::Size& new_size) override;
NativeViewHost* holder() { return holder_; }
using WebContentsCreator =
base::RepeatingCallback<std::unique_ptr<content::WebContents>(
content::BrowserContext*)>;
class WEBVIEW_EXPORT ScopedWebContentsCreatorForTesting {
public:
explicit ScopedWebContentsCreatorForTesting(WebContentsCreator creator);
ScopedWebContentsCreatorForTesting(
const ScopedWebContentsCreatorForTesting&) = delete;
ScopedWebContentsCreatorForTesting& operator=(
const ScopedWebContentsCreatorForTesting&) = delete;
~ScopedWebContentsCreatorForTesting();
};
protected:
virtual void OnLetterboxingChanged() {}
bool is_letterboxing() const { return is_letterboxing_; }
const gfx::Size& min_size() const { return min_size_; }
const gfx::Size& max_size() const { return max_size_; }
void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
void ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) override;
bool SkipDefaultKeyEventProcessing(const ui::KeyEvent& event) override;
bool OnMousePressed(const ui::MouseEvent& event) override;
void OnFocus() override;
void AboutToRequestFocusFromTabTraversal(bool reverse) override;
gfx::NativeViewAccessible GetNativeViewAccessible() override;
void AddedToWidget() override;
void RemovedFromWidget() override;
void RenderFrameCreated(content::RenderFrameHost* render_frame_host) override;
void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override;
void RenderFrameHostChanged(content::RenderFrameHost* old_host,
content::RenderFrameHost* new_host) override;
void DidToggleFullscreenModeForTab(bool entered_fullscreen,
bool will_cause_resize) override;
void OnWebContentsFocused(
content::RenderWidgetHost* render_widget_host) override;
void AXTreeIDForMainFrameHasChanged() override;
void WebContentsDestroyed() override;
void OnAXModeAdded(ui::AXMode mode) override;
private:
friend class WebViewUnitTest;
void AttachWebContentsNativeView();
void DetachWebContentsNativeView();
void UpdateCrashedOverlayView();
void NotifyAccessibilityWebContentsChanged();
void SetUpNewMainFrame(content::RenderFrameHost* frame_host);
void LostMainFrame();
void MaybeEnableAutoResize(content::RenderFrameHost* frame_host);
std::unique_ptr<content::WebContents> CreateWebContents(
content::BrowserContext* browser_context,
const GURL& url,
base::Location creator_location);
const raw_ptr<NativeViewHost> holder_ =
AddChildView(std::make_unique<NativeViewHost>());
base::ScopedObservation<ui::AXPlatform, ui::AXModeObserver>
ax_mode_observation_{this};
std::unique_ptr<content::WebContents> wc_owner_;
bool is_letterboxing_ = false;
raw_ptr<content::BrowserContext> browser_context_;
bool allow_accelerators_ = false;
ViewTracker crashed_overlay_view_;
bool is_primary_web_contents_for_window_ = false;
bool lock_child_ax_tree_id_override_ = false;
gfx::Size min_size_;
gfx::Size max_size_;
base::RepeatingCallbackList<void(WebView*)> web_contents_attached_callbacks_;
base::RepeatingCallbackList<void(WebView*)> web_contents_detached_callbacks_;
base::RepeatingCallbackList<void(WebView*)> web_contents_focused_callbacks_;
};
BEGIN_VIEW_BUILDER(WEBVIEW_EXPORT, WebView, View)
VIEW_BUILDER_PROPERTY(content::BrowserContext*, BrowserContext)
VIEW_BUILDER_PROPERTY(content::WebContents*, WebContents)
VIEW_BUILDER_PROPERTY(bool, FastResize)
VIEW_BUILDER_METHOD(EnableSizingFromWebContents,
const gfx::Size&,
const gfx::Size&)
VIEW_BUILDER_PROPERTY(View*, CrashedOverlayView)
VIEW_BUILDER_METHOD(set_is_primary_web_contents_for_window, bool)
VIEW_BUILDER_METHOD(set_allow_accelerators, bool)
END_VIEW_BUILDER
}
DEFINE_VIEW_BUILDER(WEBVIEW_EXPORT, WebView)
#endif