1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_EMBEDDER_SUPPORT_ANDROID_DELEGATE_WEB_CONTENTS_DELEGATE_ANDROID_H_
6 #define COMPONENTS_EMBEDDER_SUPPORT_ANDROID_DELEGATE_WEB_CONTENTS_DELEGATE_ANDROID_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 
12 #include "base/android/jni_weak_ref.h"
13 #include "base/android/scoped_java_ref.h"
14 #include "base/compiler_specific.h"
15 #include "content/public/browser/web_contents_delegate.h"
16 #include "third_party/blink/public/mojom/frame/blocked_navigation_types.mojom.h"
17 
18 class GURL;
19 
20 namespace content {
21 class WebContents;
22 class WebContentsDelegate;
23 struct NativeWebKeyboardEvent;
24 struct OpenURLParams;
25 }  // namespace content
26 
27 namespace web_contents_delegate_android {
28 
29 enum WebContentsDelegateLogLevel {
30   // Equivalent of WebCore::WebConsoleMessage::LevelDebug.
31   WEB_CONTENTS_DELEGATE_LOG_LEVEL_DEBUG = 0,
32   // Equivalent of WebCore::WebConsoleMessage::LevelLog.
33   WEB_CONTENTS_DELEGATE_LOG_LEVEL_LOG = 1,
34   // Equivalent of WebCore::WebConsoleMessage::LevelWarning.
35   WEB_CONTENTS_DELEGATE_LOG_LEVEL_WARNING = 2,
36   // Equivalent of WebCore::WebConsoleMessage::LevelError.
37   WEB_CONTENTS_DELEGATE_LOG_LEVEL_ERROR = 3,
38 };
39 
40 // Native underpinnings of WebContentsDelegateAndroid.java. Provides a default
41 // delegate for WebContents to forward calls to the java peer. The embedding
42 // application may subclass and override methods on either the C++ or Java side
43 // as required.
44 class WebContentsDelegateAndroid : public content::WebContentsDelegate {
45  public:
46   WebContentsDelegateAndroid(JNIEnv* env, jobject obj);
47   ~WebContentsDelegateAndroid() override;
48 
49   // Overridden from WebContentsDelegate:
50   content::WebContents* OpenURLFromTab(
51       content::WebContents* source,
52       const content::OpenURLParams& params) override;
53   content::ColorChooser* OpenColorChooser(
54       content::WebContents* source,
55       SkColor color,
56       const std::vector<blink::mojom::ColorSuggestionPtr>& suggestions)
57       override;
58   void NavigationStateChanged(content::WebContents* source,
59                               content::InvalidateTypes changed_flags) override;
60   void VisibleSecurityStateChanged(content::WebContents* source) override;
61   void ActivateContents(content::WebContents* contents) override;
62   void LoadingStateChanged(content::WebContents* source,
63                            bool to_different_document) override;
64   void RendererUnresponsive(
65       content::WebContents* source,
66       content::RenderWidgetHost* render_widget_host,
67       base::RepeatingClosure hang_monitor_restarter) override;
68   void RendererResponsive(
69       content::WebContents* source,
70       content::RenderWidgetHost* render_widget_host) override;
71   void WebContentsCreated(content::WebContents* source_contents,
72                           int opener_render_process_id,
73                           int opener_render_frame_id,
74                           const std::string& frame_name,
75                           const GURL& target_url,
76                           content::WebContents* new_contents) override;
77   bool IsWebContentsCreationOverridden(
78       content::SiteInstance* source_site_instance,
79       content::mojom::WindowContainerType window_container_type,
80       const GURL& opener_url,
81       const std::string& frame_name,
82       const GURL& target_url) override;
83   void CloseContents(content::WebContents* source) override;
84   void SetContentsBounds(content::WebContents* source,
85                          const gfx::Rect& bounds) override;
86   bool DidAddMessageToConsole(content::WebContents* source,
87                               blink::mojom::ConsoleMessageLevel log_level,
88                               const base::string16& message,
89                               int32_t line_no,
90                               const base::string16& source_id) override;
91   void UpdateTargetURL(content::WebContents* source, const GURL& url) override;
92   bool HandleKeyboardEvent(
93       content::WebContents* source,
94       const content::NativeWebKeyboardEvent& event) override;
95   bool TakeFocus(content::WebContents* source, bool reverse) override;
96   void ShowRepostFormWarningDialog(content::WebContents* source) override;
97   bool ShouldBlockMediaRequest(const GURL& url) override;
98   void EnterFullscreenModeForTab(
99       content::WebContents* web_contents,
100       const GURL& origin,
101       const blink::mojom::FullscreenOptions& options) override;
102   void ExitFullscreenModeForTab(content::WebContents* web_contents) override;
103   bool IsFullscreenForTabOrPending(
104       const content::WebContents* web_contents) override;
105   void OnDidBlockNavigation(
106       content::WebContents* web_contents,
107       const GURL& blocked_url,
108       const GURL& initiator_url,
109       blink::mojom::NavigationBlockedReason reason) override;
110   int GetTopControlsHeight() override;
111   int GetTopControlsMinHeight() override;
112   int GetBottomControlsHeight() override;
113   int GetBottomControlsMinHeight() override;
114   bool ShouldAnimateBrowserControlsHeightChanges() override;
115   bool DoBrowserControlsShrinkRendererSize(
116       const content::WebContents* contents) override;
117 
118  protected:
119   base::android::ScopedJavaLocalRef<jobject> GetJavaDelegate(JNIEnv* env) const;
120 
121  private:
122   // We depend on the java side user of WebContentDelegateAndroid to hold a
123   // strong reference to that object as long as they want to receive callbacks
124   // on it. Using a weak ref here allows it to be correctly GCed.
125   JavaObjectWeakGlobalRef weak_java_delegate_;
126 };
127 
128 }  // namespace web_contents_delegate_android
129 
130 #endif  // COMPONENTS_EMBEDDER_SUPPORT_ANDROID_DELEGATE_WEB_CONTENTS_DELEGATE_ANDROID_H_
131