tab_impl.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // Copyright 2019 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. #ifndef WEBLAYER_BROWSER_TAB_IMPL_H_
  5. #define WEBLAYER_BROWSER_TAB_IMPL_H_
  6. #include <memory>
  7. #include <set>
  8. #include <string>
  9. #include "base/callback_forward.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/observer_list.h"
  13. #include "build/build_config.h"
  14. #include "cc/input/browser_controls_state.h"
  15. #include "components/find_in_page/find_result_observer.h"
  16. #include "content/public/browser/color_chooser.h"
  17. #include "content/public/browser/web_contents_delegate.h"
  18. #include "content/public/browser/web_contents_observer.h"
  19. #include "weblayer/browser/i18n_util.h"
  20. #include "weblayer/public/tab.h"
  21. #if BUILDFLAG(IS_ANDROID)
  22. #include "base/android/scoped_java_ref.h"
  23. #include "weblayer/browser/browser_controls_navigation_state_handler_delegate.h"
  24. #endif
  25. namespace js_injection {
  26. class JsCommunicationHost;
  27. }
  28. namespace blink {
  29. namespace web_pref {
  30. struct WebPreferences;
  31. }
  32. } // namespace blink
  33. namespace content {
  34. class RenderWidgetHostView;
  35. class WebContents;
  36. struct ContextMenuParams;
  37. } // namespace content
  38. namespace gfx {
  39. class Rect;
  40. class Size;
  41. } // namespace gfx
  42. namespace sessions {
  43. class SessionTabHelperDelegate;
  44. }
  45. namespace weblayer {
  46. class BrowserControlsNavigationStateHandler;
  47. class BrowserImpl;
  48. class FullscreenDelegate;
  49. class NavigationControllerImpl;
  50. class NewTabDelegate;
  51. class ProfileImpl;
  52. #if BUILDFLAG(IS_ANDROID)
  53. class BrowserControlsContainerView;
  54. enum class ControlsVisibilityReason;
  55. #endif
  56. class TabImpl : public Tab,
  57. public content::WebContentsDelegate,
  58. public content::WebContentsObserver,
  59. #if BUILDFLAG(IS_ANDROID)
  60. public BrowserControlsNavigationStateHandlerDelegate,
  61. #endif
  62. public find_in_page::FindResultObserver {
  63. public:
  64. enum class ScreenShotErrors {
  65. kNone = 0,
  66. kScaleOutOfRange,
  67. kTabNotActive,
  68. kWebContentsNotVisible,
  69. kNoSurface,
  70. kNoRenderWidgetHostView,
  71. kNoWindowAndroid,
  72. kEmptyViewport,
  73. kHiddenByControls,
  74. kScaledToEmpty,
  75. kCaptureFailed,
  76. kBitmapAllocationFailed,
  77. };
  78. class DataObserver {
  79. public:
  80. // Called when SetData() is called on |tab|.
  81. virtual void OnDataChanged(
  82. TabImpl* tab,
  83. const std::map<std::string, std::string>& data) = 0;
  84. };
  85. // TODO(sky): investigate a better way to not have so many ifdefs.
  86. #if BUILDFLAG(IS_ANDROID)
  87. TabImpl(ProfileImpl* profile,
  88. const base::android::JavaParamRef<jobject>& java_impl,
  89. std::unique_ptr<content::WebContents> web_contents);
  90. #endif
  91. explicit TabImpl(ProfileImpl* profile,
  92. std::unique_ptr<content::WebContents> web_contents,
  93. const std::string& guid = std::string());
  94. TabImpl(const TabImpl&) = delete;
  95. TabImpl& operator=(const TabImpl&) = delete;
  96. ~TabImpl() override;
  97. // Returns the TabImpl from the specified WebContents (which may be null), or
  98. // null if |web_contents| was not created by a TabImpl.
  99. static TabImpl* FromWebContents(content::WebContents* web_contents);
  100. static std::set<TabImpl*> GetAllTabImpl();
  101. ProfileImpl* profile() { return profile_; }
  102. void set_browser(BrowserImpl* browser) { browser_ = browser; }
  103. BrowserImpl* browser() { return browser_; }
  104. content::WebContents* web_contents() const { return web_contents_.get(); }
  105. bool has_new_tab_delegate() const { return new_tab_delegate_ != nullptr; }
  106. NewTabDelegate* new_tab_delegate() const { return new_tab_delegate_; }
  107. // Called from Browser when this Tab is gaining/losing active status.
  108. void OnGainedActive();
  109. void OnLosingActive();
  110. bool IsActive();
  111. void ShowContextMenu(const content::ContextMenuParams& params);
  112. #if BUILDFLAG(IS_ANDROID)
  113. base::android::ScopedJavaGlobalRef<jobject> GetJavaTab() {
  114. return java_impl_;
  115. }
  116. bool desktop_user_agent_enabled() { return desktop_user_agent_enabled_; }
  117. // Call this method to disable integration with the system-level Autofill
  118. // infrastructure. Useful in conjunction with InitializeAutofillForTests().
  119. // Should be called early in the lifetime of WebLayer, and in
  120. // particular, must be called before the TabImpl is attached to the browser
  121. // on the Java side to have the desired effect.
  122. static void DisableAutofillSystemIntegrationForTesting();
  123. base::android::ScopedJavaLocalRef<jobject> GetWebContents(JNIEnv* env);
  124. void SetBrowserControlsContainerViews(
  125. JNIEnv* env,
  126. jlong native_top_browser_controls_container_view,
  127. jlong native_bottom_browser_controls_container_view);
  128. void ExecuteScript(JNIEnv* env,
  129. const base::android::JavaParamRef<jstring>& script,
  130. bool use_separate_isolate,
  131. const base::android::JavaParamRef<jobject>& callback);
  132. void SetJavaImpl(JNIEnv* env,
  133. const base::android::JavaParamRef<jobject>& impl);
  134. // Invoked every time that the Java-side AutofillProvider instance is created,
  135. // the native side autofill might have been initialized in the case that
  136. // Android context is switched.
  137. void InitializeAutofillIfNecessary(JNIEnv* env);
  138. void UpdateBrowserControlsConstraint(JNIEnv* env,
  139. jint constraint,
  140. jboolean animate);
  141. base::android::ScopedJavaLocalRef<jstring> GetGuid(JNIEnv* env);
  142. void CaptureScreenShot(
  143. JNIEnv* env,
  144. jfloat scale,
  145. const base::android::JavaParamRef<jobject>& value_callback);
  146. jboolean SetData(JNIEnv* env,
  147. const base::android::JavaParamRef<jobjectArray>& data);
  148. base::android::ScopedJavaLocalRef<jobjectArray> GetData(JNIEnv* env);
  149. jboolean IsRendererControllingBrowserControlsOffsets(JNIEnv* env);
  150. base::android::ScopedJavaLocalRef<jstring> RegisterWebMessageCallback(
  151. JNIEnv* env,
  152. const base::android::JavaParamRef<jstring>& js_object_name,
  153. const base::android::JavaParamRef<jobjectArray>& origins,
  154. const base::android::JavaParamRef<jobject>& client);
  155. void UnregisterWebMessageCallback(
  156. JNIEnv* env,
  157. const base::android::JavaParamRef<jstring>& js_object_name);
  158. jboolean CanTranslate(JNIEnv* env);
  159. void ShowTranslateUi(JNIEnv* env);
  160. void RemoveTabFromBrowserBeforeDestroying(JNIEnv* env);
  161. void SetTranslateTargetLanguage(
  162. JNIEnv* env,
  163. const base::android::JavaParamRef<jstring>& translate_target_lang);
  164. void SetDesktopUserAgentEnabled(JNIEnv* env, jboolean enable);
  165. jboolean IsDesktopUserAgentEnabled(JNIEnv* env);
  166. void Download(JNIEnv* env, jlong native_context_menu_params);
  167. #endif
  168. ErrorPageDelegate* error_page_delegate() { return error_page_delegate_; }
  169. void AddDataObserver(DataObserver* observer);
  170. void RemoveDataObserver(DataObserver* observer);
  171. GoogleAccountsDelegate* google_accounts_delegate() {
  172. return google_accounts_delegate_;
  173. }
  174. // Tab:
  175. Browser* GetBrowser() override;
  176. void SetErrorPageDelegate(ErrorPageDelegate* delegate) override;
  177. void SetFullscreenDelegate(FullscreenDelegate* delegate) override;
  178. void SetNewTabDelegate(NewTabDelegate* delegate) override;
  179. void SetGoogleAccountsDelegate(GoogleAccountsDelegate* delegate) override;
  180. void AddObserver(TabObserver* observer) override;
  181. void RemoveObserver(TabObserver* observer) override;
  182. NavigationController* GetNavigationController() override;
  183. void ExecuteScript(const std::u16string& script,
  184. bool use_separate_isolate,
  185. JavaScriptResultCallback callback) override;
  186. const std::string& GetGuid() override;
  187. void SetData(const std::map<std::string, std::string>& data) override;
  188. const std::map<std::string, std::string>& GetData() override;
  189. std::u16string AddWebMessageHostFactory(
  190. std::unique_ptr<WebMessageHostFactory> factory,
  191. const std::u16string& js_object_name,
  192. const std::vector<std::string>& js_origins) override;
  193. void RemoveWebMessageHostFactory(
  194. const std::u16string& js_object_name) override;
  195. std::unique_ptr<FaviconFetcher> CreateFaviconFetcher(
  196. FaviconFetcherDelegate* delegate) override;
  197. void SetTranslateTargetLanguage(
  198. const std::string& translate_target_lang) override;
  199. #if !BUILDFLAG(IS_ANDROID)
  200. void AttachToView(views::WebView* web_view) override;
  201. #endif
  202. void WebPreferencesChanged();
  203. void SetWebPreferences(blink::web_pref::WebPreferences* prefs);
  204. // Executes |script| with a user gesture.
  205. void ExecuteScriptWithUserGestureForTests(const std::u16string& script);
  206. #if BUILDFLAG(IS_ANDROID)
  207. // Initializes the autofill system for tests.
  208. void InitializeAutofillForTests();
  209. #endif // BUILDFLAG(IS_ANDROID)
  210. private:
  211. // content::WebContentsDelegate:
  212. content::WebContents* OpenURLFromTab(
  213. content::WebContents* source,
  214. const content::OpenURLParams& params) override;
  215. void ShowRepostFormWarningDialog(content::WebContents* source) override;
  216. void NavigationStateChanged(content::WebContents* source,
  217. content::InvalidateTypes changed_flags) override;
  218. content::JavaScriptDialogManager* GetJavaScriptDialogManager(
  219. content::WebContents* web_contents) override;
  220. #if BUILDFLAG(IS_ANDROID)
  221. std::unique_ptr<content::ColorChooser> OpenColorChooser(
  222. content::WebContents* web_contents,
  223. SkColor color,
  224. const std::vector<blink::mojom::ColorSuggestionPtr>& suggestions)
  225. override;
  226. #endif
  227. void RunFileChooser(content::RenderFrameHost* render_frame_host,
  228. scoped_refptr<content::FileSelectListener> listener,
  229. const blink::mojom::FileChooserParams& params) override;
  230. void CreateSmsPrompt(content::RenderFrameHost*,
  231. const std::vector<url::Origin>&,
  232. const std::string& one_time_code,
  233. base::OnceClosure on_confirm,
  234. base::OnceClosure on_cancel) override;
  235. int GetTopControlsHeight() override;
  236. int GetTopControlsMinHeight() override;
  237. int GetBottomControlsHeight() override;
  238. bool DoBrowserControlsShrinkRendererSize(
  239. content::WebContents* web_contents) override;
  240. bool OnlyExpandTopControlsAtPageTop() override;
  241. bool ShouldAnimateBrowserControlsHeightChanges() override;
  242. bool IsBackForwardCacheSupported() override;
  243. void RequestMediaAccessPermission(
  244. content::WebContents* web_contents,
  245. const content::MediaStreamRequest& request,
  246. content::MediaResponseCallback callback) override;
  247. bool CheckMediaAccessPermission(content::RenderFrameHost* render_frame_host,
  248. const GURL& security_origin,
  249. blink::mojom::MediaStreamType type) override;
  250. void EnterFullscreenModeForTab(
  251. content::RenderFrameHost* requesting_frame,
  252. const blink::mojom::FullscreenOptions& options) override;
  253. void ExitFullscreenModeForTab(content::WebContents* web_contents) override;
  254. bool IsFullscreenForTabOrPending(
  255. const content::WebContents* web_contents) override;
  256. blink::mojom::DisplayMode GetDisplayMode(
  257. const content::WebContents* web_contents) override;
  258. void AddNewContents(content::WebContents* source,
  259. std::unique_ptr<content::WebContents> new_contents,
  260. const GURL& target_url,
  261. WindowOpenDisposition disposition,
  262. const gfx::Rect& initial_rect,
  263. bool user_gesture,
  264. bool* was_blocked) override;
  265. void CloseContents(content::WebContents* source) override;
  266. void FindReply(content::WebContents* web_contents,
  267. int request_id,
  268. int number_of_matches,
  269. const gfx::Rect& selection_rect,
  270. int active_match_ordinal,
  271. bool final_update) override;
  272. #if BUILDFLAG(IS_ANDROID)
  273. void FindMatchRectsReply(content::WebContents* web_contents,
  274. int version,
  275. const std::vector<gfx::RectF>& rects,
  276. const gfx::RectF& active_rect) override;
  277. // Pointer arguments are outputs. Check the preconditions for capturing a
  278. // screenshot and either set all outputs, or return an error code, in which
  279. // case the state of output arguments is undefined.
  280. ScreenShotErrors PrepareForCaptureScreenShot(
  281. float scale,
  282. content::RenderWidgetHostView** rwhv,
  283. gfx::Rect* src_rect,
  284. gfx::Size* output_size);
  285. void UpdateBrowserControlsState(cc::BrowserControlsState new_state,
  286. bool animate);
  287. #endif
  288. // content::WebContentsObserver:
  289. void PrimaryMainFrameRenderProcessGone(
  290. base::TerminationStatus status) override;
  291. void DidChangeVisibleSecurityState() override;
  292. // find_in_page::FindResultObserver:
  293. void OnFindResultAvailable(content::WebContents* web_contents) override;
  294. #if BUILDFLAG(IS_ANDROID)
  295. // BrowserControlsNavigationStateHandlerDelegate:
  296. void OnBrowserControlsStateStateChanged(
  297. ControlsVisibilityReason reason,
  298. cc::BrowserControlsState state) override;
  299. void OnUpdateBrowserControlsStateBecauseOfProcessSwitch(
  300. bool did_commit) override;
  301. #endif
  302. // Called from closure supplied to delegate to exit fullscreen.
  303. void OnExitFullscreen();
  304. void UpdateRendererPrefs(bool should_sync_prefs);
  305. // Returns the FindTabHelper for the page, or null if none exists.
  306. find_in_page::FindTabHelper* GetFindTabHelper();
  307. static sessions::SessionTabHelperDelegate* GetSessionServiceTabHelperDelegate(
  308. content::WebContents* web_contents);
  309. #if BUILDFLAG(IS_ANDROID)
  310. void InitializeAutofillDriver();
  311. void SetBrowserControlsConstraint(ControlsVisibilityReason reason,
  312. cc::BrowserControlsState constraint);
  313. #endif
  314. void UpdateBrowserVisibleSecurityStateIfNecessary();
  315. bool SetDataInternal(const std::map<std::string, std::string>& data);
  316. void EnterFullscreenImpl();
  317. raw_ptr<BrowserImpl> browser_ = nullptr;
  318. raw_ptr<ErrorPageDelegate> error_page_delegate_ = nullptr;
  319. raw_ptr<FullscreenDelegate> fullscreen_delegate_ = nullptr;
  320. raw_ptr<NewTabDelegate> new_tab_delegate_ = nullptr;
  321. raw_ptr<GoogleAccountsDelegate> google_accounts_delegate_ = nullptr;
  322. raw_ptr<ProfileImpl> profile_;
  323. std::unique_ptr<content::WebContents> web_contents_;
  324. std::unique_ptr<NavigationControllerImpl> navigation_controller_;
  325. base::ObserverList<TabObserver>::Unchecked observers_;
  326. base::CallbackListSubscription locale_change_subscription_;
  327. #if BUILDFLAG(IS_ANDROID)
  328. raw_ptr<BrowserControlsContainerView> top_controls_container_view_ = nullptr;
  329. raw_ptr<BrowserControlsContainerView> bottom_controls_container_view_ =
  330. nullptr;
  331. base::android::ScopedJavaGlobalRef<jobject> java_impl_;
  332. std::unique_ptr<BrowserControlsNavigationStateHandler>
  333. browser_controls_navigation_state_handler_;
  334. // Last value supplied to UpdateBrowserControlsConstraint(). This *constraint*
  335. // can be SHOWN, if for example a modal dialog is forcing the controls to be
  336. // visible, HIDDEN, if for example fullscreen is forcing the controls to be
  337. // hidden, or BOTH, if either state is viable (e.g. during normal browsing).
  338. // When BOTH, the actual current state could be showing or hidden.
  339. cc::BrowserControlsState current_browser_controls_visibility_constraint_ =
  340. cc::BrowserControlsState::kShown;
  341. bool desktop_user_agent_enabled_ = false;
  342. #endif
  343. bool is_fullscreen_ = false;
  344. // Set to true doing EnterFullscreenModeForTab().
  345. bool processing_enter_fullscreen_ = false;
  346. // If true, the fullscreen delegate is called when the tab gains active.
  347. bool enter_fullscreen_on_gained_active_ = false;
  348. const std::string guid_;
  349. std::map<std::string, std::string> data_;
  350. base::ObserverList<DataObserver>::Unchecked data_observers_;
  351. std::u16string title_;
  352. std::unique_ptr<js_injection::JsCommunicationHost> js_communication_host_;
  353. base::WeakPtrFactory<TabImpl> weak_ptr_factory_for_fullscreen_exit_{this};
  354. };
  355. } // namespace weblayer
  356. #endif // WEBLAYER_BROWSER_TAB_IMPL_H_