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
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302

content / browser / web_contents / web_contents_android.h [blame]

// 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 CONTENT_BROWSER_WEB_CONTENTS_WEB_CONTENTS_ANDROID_H_
#define CONTENT_BROWSER_WEB_CONTENTS_WEB_CONTENTS_ANDROID_H_

#include <jni.h>

#include <memory>

#include "base/android/jni_android.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "cc/input/browser_controls_offset_tags_info.h"
#include "content/browser/android/render_widget_host_connector.h"
#include "content/browser/navigation_transitions/back_forward_transition_animator.h"
#include "content/browser/renderer_host/navigation_controller_android.h"
#include "content/browser/renderer_host/render_widget_host_view_android.h"
#include "content/common/content_export.h"
#include "content/public/browser/back_forward_transition_animation_manager.h"
#include "third_party/blink/public/mojom/input/input_handler.mojom-forward.h"

class GURL;

namespace ui {
struct AXTreeUpdate;
}

namespace content {

class WebContents;
class WebContentsImpl;

// Android wrapper around WebContents that provides safer passage from java and
// back to native and provides java with a means of communicating with its
// native counterpart.
class CONTENT_EXPORT WebContentsAndroid {
 public:
  explicit WebContentsAndroid(WebContentsImpl* web_contents);

  WebContentsAndroid(const WebContentsAndroid&) = delete;
  WebContentsAndroid& operator=(const WebContentsAndroid&) = delete;

  ~WebContentsAndroid();

  void Init();

  WebContentsImpl* web_contents() const { return web_contents_; }

  base::android::ScopedJavaLocalRef<jobject> GetJavaObject();

  // Ensure that the RenderFrameHost etc are ready to handle JS eval
  // (e.g. recover from a crashed state).
  bool InitializeRenderFrameForJavaScript();

  // Methods called from Java
  void ClearNativeReference(JNIEnv* env);
  base::android::ScopedJavaLocalRef<jobject> GetTopLevelNativeWindow(
      JNIEnv* env);
  void SetTopLevelNativeWindow(
      JNIEnv* env,
      const base::android::JavaParamRef<jobject>& jwindow_android);
  void SetViewAndroidDelegate(
      JNIEnv* env,
      const base::android::JavaParamRef<jobject>& jview_delegate);
  base::android::ScopedJavaLocalRef<jobject> GetMainFrame(JNIEnv* env) const;
  base::android::ScopedJavaLocalRef<jobject> GetFocusedFrame(JNIEnv* env) const;
  bool IsFocusedElementEditable(JNIEnv* env);
  base::android::ScopedJavaLocalRef<jobject> GetRenderFrameHostFromId(
      JNIEnv* env,
      jint render_process_id,
      jint render_frame_id) const;
  base::android::ScopedJavaLocalRef<jobjectArray> GetAllRenderFrameHosts(
      JNIEnv* env) const;
  base::android::ScopedJavaLocalRef<jstring> GetTitle(JNIEnv* env) const;
  base::android::ScopedJavaLocalRef<jobject> GetVisibleURL(JNIEnv* env) const;
  jint GetVirtualKeyboardMode(JNIEnv* env) const;

  bool IsLoading(JNIEnv* env) const;
  bool ShouldShowLoadingUI(JNIEnv* env) const;
  bool HasUncommittedNavigationInPrimaryMainFrame(JNIEnv* env) const;

  void DispatchBeforeUnload(JNIEnv* env, bool auto_cancel);

  void Stop(JNIEnv* env);
  void Cut(JNIEnv* env);
  void Copy(JNIEnv* env);
  void Paste(JNIEnv* env);
  void PasteAsPlainText(JNIEnv* env);
  void Replace(JNIEnv* env, const base::android::JavaParamRef<jstring>& jstr);
  void SelectAll(JNIEnv* env);
  void CollapseSelection(JNIEnv* env);
  jint GetBackgroundColor(JNIEnv* env);
  base::android::ScopedJavaLocalRef<jobject> GetLastCommittedURL(
      JNIEnv* env) const;
  jboolean IsIncognito(JNIEnv* env);

  void ResumeLoadingCreatedWebContents(JNIEnv* env);

  void SetImportance(JNIEnv* env, jint importance);
  void SuspendAllMediaPlayers(JNIEnv* env);
  void SetAudioMuted(JNIEnv* env, jboolean mute);
  jboolean IsAudioMuted(JNIEnv* env);

  jboolean FocusLocationBarByDefault(JNIEnv* env);
  bool IsFullscreenForCurrentTab(JNIEnv* env);
  void ExitFullscreen(JNIEnv* env);
  void ScrollFocusedEditableNodeIntoView(JNIEnv* env);
  void SelectAroundCaret(JNIEnv* env,
                         jint granularity,
                         jboolean should_show_handle,
                         jboolean should_show_context_menu,
                         jint startOffset,
                         jint endOffset,
                         jint surroundingTextLength);
  void AdjustSelectionByCharacterOffset(JNIEnv* env,
                                        jint start_adjust,
                                        jint end_adjust,
                                        jboolean show_selection_menu);
  void EvaluateJavaScript(JNIEnv* env,
                          const base::android::JavaParamRef<jstring>& script,
                          const base::android::JavaParamRef<jobject>& callback);
  void EvaluateJavaScriptForTests(
      JNIEnv* env,
      const base::android::JavaParamRef<jstring>& script,
      const base::android::JavaParamRef<jobject>& callback);

  void AddMessageToDevToolsConsole(
      JNIEnv* env,
      jint level,
      const base::android::JavaParamRef<jstring>& message);

  void PostMessageToMainFrame(
      JNIEnv* env,
      const base::android::JavaParamRef<jobject>& jmessage,
      const base::android::JavaParamRef<jstring>& jsource_origin,
      const base::android::JavaParamRef<jstring>& jtarget_origin,
      const base::android::JavaParamRef<jobjectArray>& jports);

  jboolean HasAccessedInitialDocument(JNIEnv* env);

  jboolean HasViewTransitionOptIn(JNIEnv* env);

  // No theme color is represented by SK_ColorTRANSPARENT.
  jint GetThemeColor(JNIEnv* env);

  jfloat GetLoadProgress(JNIEnv* env);

  void RequestSmartClipExtract(
      JNIEnv* env,
      const base::android::JavaParamRef<jobject>& callback,
      jint x,
      jint y,
      jint width,
      jint height);

  void RequestAccessibilitySnapshot(
      JNIEnv* env,
      const base::android::JavaParamRef<jobject>& view_structure_root,
      const base::android::JavaParamRef<jobject>& view_structure_builder,
      const base::android::JavaParamRef<jobject>& callback);

  base::android::ScopedJavaLocalRef<jstring> GetEncoding(JNIEnv* env) const;

  void SetOverscrollRefreshHandler(
      JNIEnv* env,
      const base::android::JavaParamRef<jobject>& overscroll_refresh_handler);

  void SetSpatialNavigationDisabled(JNIEnv* env, bool disabled);

  void SetStylusHandwritingEnabled(JNIEnv* env, bool enabled);

  int DownloadImage(JNIEnv* env,
                    const base::android::JavaParamRef<jobject>& url,
                    jboolean is_fav_icon,
                    jint max_bitmap_size,
                    jboolean bypass_cache,
                    const base::android::JavaParamRef<jobject>& jcallback);
  void SetHasPersistentVideo(JNIEnv* env, jboolean value);
  bool HasActiveEffectivelyFullscreenVideo(JNIEnv* env);
  bool IsPictureInPictureAllowedForFullscreenVideo(JNIEnv* env);

  base::android::ScopedJavaLocalRef<jobject> GetFullscreenVideoSize(
      JNIEnv* env);
  void SetSize(JNIEnv* env, jint width, jint height);
  int GetWidth(JNIEnv* env);
  int GetHeight(JNIEnv* env);

  base::android::ScopedJavaLocalRef<jobject> GetOrCreateEventForwarder(
      JNIEnv* env);

  void SendOrientationChangeEvent(JNIEnv* env, jint orientation);

  void OnScaleFactorChanged(JNIEnv* env);
  void SetFocus(JNIEnv* env, jboolean focused);
  bool IsBeingDestroyed(JNIEnv* env);

  void SetDisplayCutoutSafeArea(JNIEnv* env,
                                int top,
                                int left,
                                int bottom,
                                int right);
  void NotifyRendererPreferenceUpdate(JNIEnv* env);

  void NotifyBrowserControlsHeightChanged(JNIEnv* env);

  bool NeedToFireBeforeUnloadOrUnloadEvents(JNIEnv* env);

  base::android::ScopedJavaLocalRef<jobject> GetRenderWidgetHostView(
      JNIEnv* env);

  jint GetVisibility(JNIEnv* env);

  void UpdateWebContentsVisibility(JNIEnv* env, jint visibility);

  void NotifyControlsConstraintsChanged(
      JNIEnv* env,
      const base::android::JavaParamRef<jobject>& jold_offset_tag_bundle,
      const base::android::JavaParamRef<jobject>& joffset_tag_bundle);

  RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid();

  class DestructionObserver : public base::CheckedObserver {
   public:
    // Invoked when the Java reference to the WebContents is being destroyed.
    virtual void WebContentsAndroidDestroyed(
        WebContentsAndroid* web_contents_android) = 0;
  };

  void AddDestructionObserver(DestructionObserver* observer);
  void RemoveDestructionObserver(DestructionObserver* observer);

  void OnContentForNavigationEntryShown(JNIEnv* env);
  jint GetCurrentBackForwardTransitionStage(JNIEnv* env);

  void CaptureContentAsBitmapForTesting(
      JNIEnv* env,
      const base::android::JavaParamRef<jobject>& jcallback);
  void OnFinishGetContentBitmapForTesting(
      const base::android::JavaRef<jobject>& callback,
      gfx::Image snapshot);

  void SetLongPressLinkSelectText(JNIEnv* env, jboolean enabled);

  // Adds a crash report, like DumpWithoutCrashing(), including the Java stack
  // trace from which `web_contents` was created. This is meant to help debug
  // cases where BrowserContext is destroyed before its WebContents.
  static void ReportDanglingPtrToBrowserContext(JNIEnv* env,
                                                WebContents* web_contents);

 private:
  void OnFinishDownloadImage(const base::android::JavaRef<jobject>& obj,
                             const base::android::JavaRef<jobject>& callback,
                             int id,
                             int http_status_code,
                             const GURL& url,
                             const std::vector<SkBitmap>& bitmaps,
                             const std::vector<gfx::Size>& sizes);
  void SelectAroundCaretAck(int startOffset,
                            int endOffset,
                            int surroundingTextLength,
                            blink::mojom::SelectAroundCaretResultPtr result);
  // Walks over the AXTreeUpdate and creates a light weight snapshot.
  void AXTreeSnapshotCallback(
      const base::android::JavaRef<jobject>& view_structure_root,
      const base::android::JavaRef<jobject>& view_structure_builder,
      const base::android::JavaRef<jobject>& callback,
      ui::AXTreeUpdate& result);

  raw_ptr<WebContentsImpl> web_contents_;

  NavigationControllerAndroid navigation_controller_;
  base::android::ScopedJavaGlobalRef<jobject> obj_;

  base::ObserverList<DestructionObserver> destruction_observers_;

  class BrowserControlsOffsetTagMediator : public RenderWidgetHostConnector {
   public:
    explicit BrowserControlsOffsetTagMediator(WebContents* web_contents);
    ~BrowserControlsOffsetTagMediator() override;

    void SetOffsetTagsInfo(
        const cc::BrowserControlsOffsetTagsInfo& new_offset_tags_info);

    void UpdateRenderProcessConnection(
        RenderWidgetHostViewAndroid* old_rwhva,
        RenderWidgetHostViewAndroid* new_rhwva) override;

   private:
    raw_ptr<RenderWidgetHostViewAndroid> rwhva_ = nullptr;
    cc::BrowserControlsOffsetTagsInfo offset_tags_info_;
  };

  raw_ptr<BrowserControlsOffsetTagMediator> offset_tag_mediator_ = nullptr;

  base::WeakPtrFactory<WebContentsAndroid> weak_factory_{this};
};

}  // namespace content

#endif  // CONTENT_BROWSER_WEB_CONTENTS_WEB_CONTENTS_ANDROID_H_