1 // Copyright 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 CHROME_BROWSER_UI_BROWSER_WINDOW_H_
6 #define CHROME_BROWSER_UI_BROWSER_WINDOW_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include "base/callback_forward.h"
13 #include "base/optional.h"
14 #include "build/build_config.h"
15 #include "chrome/browser/apps/intent_helper/apps_navigation_types.h"
16 #include "chrome/browser/lifetime/browser_close_manager.h"
17 #include "chrome/browser/sharing/sharing_dialog_data.h"
18 #include "chrome/browser/signin/chrome_signin_helper.h"
19 #include "chrome/browser/translate/chrome_translate_client.h"
20 #include "chrome/browser/ui/bookmarks/bookmark_bar.h"
21 #include "chrome/browser/ui/browser.h"
22 #include "chrome/browser/ui/browser_dialogs.h"
23 #include "chrome/browser/ui/exclusive_access/exclusive_access_bubble_type.h"
24 #include "chrome/browser/ui/page_action/page_action_icon_type.h"
25 #include "chrome/browser/ui/user_education/in_product_help.h"
26 #include "chrome/common/buildflags.h"
27 #include "components/content_settings/core/common/content_settings_types.h"
28 #include "components/translate/core/common/translate_errors.h"
29 #include "ui/base/base_window.h"
30 #include "ui/base/window_open_disposition.h"
31 #include "ui/gfx/native_widget_types.h"
32 #include "url/origin.h"
33 
34 #if defined(OS_ANDROID)
35 #error This file should only be included on desktop.
36 #endif
37 
38 class Browser;
39 class SharingDialog;
40 class DownloadShelf;
41 class ExclusiveAccessContext;
42 class ExtensionsContainer;
43 class FeaturePromoController;
44 class FindBar;
45 class GURL;
46 class LocationBar;
47 class StatusBubble;
48 
49 namespace autofill {
50 class AutofillBubbleHandler;
51 }  // namespace autofill
52 
53 namespace content {
54 class WebContents;
55 struct NativeWebKeyboardEvent;
56 enum class KeyboardEventProcessingResult;
57 }  // namespace content
58 
59 namespace gfx {
60 class Size;
61 }
62 
63 namespace qrcode_generator {
64 class QRCodeGeneratorBubbleController;
65 class QRCodeGeneratorBubbleView;
66 }  // namespace qrcode_generator
67 
68 namespace signin_metrics {
69 enum class AccessPoint;
70 }
71 
72 namespace send_tab_to_self {
73 class SendTabToSelfBubbleController;
74 class SendTabToSelfBubbleView;
75 }  // namespace send_tab_to_self
76 
77 namespace web_modal {
78 class WebContentsModalDialogHost;
79 }
80 
81 enum class ShowTranslateBubbleResult {
82   // The translate bubble was successfully shown.
83   SUCCESS,
84 
85   // The various reasons for which the translate bubble could fail to be shown.
86   BROWSER_WINDOW_NOT_VALID,
87   BROWSER_WINDOW_MINIMIZED,
88   BROWSER_WINDOW_NOT_ACTIVE,
89   WEB_CONTENTS_NOT_ACTIVE,
90   EDITABLE_FIELD_IS_ACTIVE,
91 };
92 
93 enum class BrowserThemeChangeType {
94   // User changes the browser theme.
95   kBrowserTheme,
96   // User changes the OS native theme.
97   kNativeTheme,
98   // A web app sets a theme color at launch, or changes theme color.
99   kWebAppTheme
100 };
101 
102 ////////////////////////////////////////////////////////////////////////////////
103 // BrowserWindow interface
104 //  An interface implemented by the "view" of the Browser window.
105 //  This interface includes ui::BaseWindow methods as well as Browser window
106 //  specific methods.
107 //
108 // NOTE: All getters may return NULL.
109 //
110 class BrowserWindow : public ui::BaseWindow {
111  public:
~BrowserWindow()112   virtual ~BrowserWindow() {}
113 
114   //////////////////////////////////////////////////////////////////////////////
115   // ui::BaseWindow interface notes:
116 
117   // Closes the window as soon as possible. If the window is not in a drag
118   // session, it will close immediately; otherwise, it will move offscreen (so
119   // events are still fired) until the drag ends, then close. This assumes
120   // that the Browser is not immediately destroyed, but will be eventually
121   // destroyed by other means (eg, the tab strip going to zero elements).
122   // Bad things happen if the Browser dtor is called directly as a result of
123   // invoking this method.
124   // virtual void Close() = 0;
125 
126   // Browser::OnWindowDidShow should be called after showing the window.
127   // virtual void Show() = 0;
128 
129   //////////////////////////////////////////////////////////////////////////////
130   // Browser specific methods:
131 
132   // Returns true if the browser window is on the current workspace (a.k.a.
133   // virtual desktop) or if we can't tell. False otherwise.
134   //
135   // On Windows, it must not be called while application is dispatching an input
136   // synchronous call like SendMessage, because IsWindowOnCurrentVirtualDesktop
137   // will return an error.
138   virtual bool IsOnCurrentWorkspace() const = 0;
139 
140   // Sets the shown |ratio| of the browser's top controls (a.k.a. top-chrome) as
141   // a result of gesture scrolling in |web_contents|.
142   virtual void SetTopControlsShownRatio(content::WebContents* web_contents,
143                                         float ratio) = 0;
144 
145   // Whether or not the renderer's viewport size should be shrunk by the height
146   // of the browser's top controls.
147   // As top-chrome is slided up or down, we don't actually resize the web
148   // contents (for perf reasons) but we have to do a bunch of adjustments on the
149   // renderer side to make it appear to the user like we're resizing things
150   // smoothly:
151   //
152   // 1) Expose content beyond the web contents rect by expanding the clip.
153   // 2) Push bottom-fixed elements around until we get a resize. As top-chrome
154   //    hides, we push the fixed elements down by an equivalent amount so that
155   //    they appear to stay fixed to the viewport bottom.
156   //
157   // Only when the user releases their finger to finish the scroll do we
158   // actually resize the web contents and clear these adjustments. So web
159   // contents has two possible sizes, viewport filling and shrunk by the top
160   // controls.
161   //
162   // The GetTopControlsHeight is a static number that never changes (as long as
163   // the top-chrome slide with gesture scrolls feature is enabled). To get the
164   // actual "showing" height as the user sees, you multiply this by the shown
165   // ratio. However, it's not enough to know this value, the renderer also needs
166   // to know which direction it should be doing the above-mentioned adjustments.
167   // That's what the DoBrowserControlsShrinkRendererSize bit is for. It tells
168   // the renderer whether it's currently in the "viewport filling" or the
169   // "shrunk by top controls" state.
170   // The returned value should never change while sliding top-chrome is in
171   // progress (either due to an in-progress gesture scroll, or due to a
172   // renderer-initiated animation of the top controls shown ratio).
173   virtual bool DoBrowserControlsShrinkRendererSize(
174       const content::WebContents* contents) const = 0;
175 
176   // Returns the height of the browser's top controls. This height doesn't
177   // change with the current shown ratio above. Renderers will call this to
178   // calculate the top-chrome shown ratio from the gesture scroll offset.
179   //
180   // Note: This should always return 0 if hiding top-chrome with page gesture
181   // scrolls is disabled. This is needed so the renderer scrolls the page
182   // immediately rather than changing the shown ratio, thinking that top-chrome
183   // and the page's top edge are moving.
184   virtual int GetTopControlsHeight() const = 0;
185 
186   // Propagates to the browser that gesture scrolling has changed state.
187   virtual void SetTopControlsGestureScrollInProgress(bool in_progress) = 0;
188 
189   // Return the status bubble associated with the frame
190   virtual StatusBubble* GetStatusBubble() = 0;
191 
192   // Inform the frame that the selected tab favicon or title has changed. Some
193   // frames may need to refresh their title bar.
194   virtual void UpdateTitleBar() = 0;
195 
196   // Inform the frame that its color has changed.
197   virtual void UpdateFrameColor() = 0;
198 
199   // Invoked when the state of the bookmark bar changes. This is only invoked if
200   // the state changes for the current tab, it is not sent when switching tabs.
201   virtual void BookmarkBarStateChanged(
202       BookmarkBar::AnimateChangeType change_type) = 0;
203 
204   // Inform the frame that the dev tools window for the selected tab has
205   // changed.
206   virtual void UpdateDevTools() = 0;
207 
208   // Update any loading animations running in the window. |should_animate| is
209   // true if there are tabs loading and the animations should continue, false
210   // if there are no active loads and the animations should end.
211   virtual void UpdateLoadingAnimations(bool should_animate) = 0;
212 
213   // Sets the starred state for the current tab.
214   virtual void SetStarredState(bool is_starred) = 0;
215 
216   // Sets whether the translate icon is lit for the current tab.
217   virtual void SetTranslateIconToggled(bool is_lit) = 0;
218 
219   // Called when the active tab changes.  Subclasses which implement
220   // TabStripModelObserver should implement this instead of ActiveTabChanged();
221   // the Browser will call this method while processing that one.
222   virtual void OnActiveTabChanged(content::WebContents* old_contents,
223                                   content::WebContents* new_contents,
224                                   int index,
225                                   int reason) = 0;
226 
227   // Called when a tab is detached. Subclasses which implement
228   // TabStripModelObserver should implement this instead of processing this
229   // in OnTabStripModelChanged(); the Browser will call this method.
230   virtual void OnTabDetached(content::WebContents* contents,
231                              bool was_active) = 0;
232 
233   // Called when the user restores a tab. |command_id| may be IDC_RESTORE_TAB or
234   // the menu command, depending on whether the tab was restored via keyboard or
235   // main menu.
236   virtual void OnTabRestored(int command_id) = 0;
237 
238   // Called to force the zoom state to for the active tab to be recalculated.
239   // |can_show_bubble| is true when a user presses the zoom up or down keyboard
240   // shortcuts and will be false in other cases (e.g. switching tabs, "clicking"
241   // + or - in the app menu to change zoom).
242   virtual void ZoomChangedForActiveTab(bool can_show_bubble) = 0;
243 
244   // Windows and GTK remove the browser controls in fullscreen, but Mac and Ash
245   // keep the controls in a slide-down panel.
246   virtual bool ShouldHideUIForFullscreen() const = 0;
247 
248   // Returns true if the fullscreen bubble is visible.
249   virtual bool IsFullscreenBubbleVisible() const = 0;
250 
251   // Returns the size of WebContents in the browser. This may be called before
252   // the TabStripModel has an active tab.
253   virtual gfx::Size GetContentsSize() const = 0;
254 
255   // Resizes the window to fit a WebContents of a certain size. This should only
256   // be called after the TabStripModel has an active tab.
257   virtual void SetContentsSize(const gfx::Size& size) = 0;
258 
259   // Updates the visual state of the specified page action icon if present on
260   // the window.
261   virtual void UpdatePageActionIcon(PageActionIconType type) = 0;
262 
263   // Returns the AutofillBubbleHandler responsible for handling all
264   // Autofill-related bubbles.
265   virtual autofill::AutofillBubbleHandler* GetAutofillBubbleHandler() = 0;
266 
267   // Executes the action for the specified page action icon.
268   virtual void ExecutePageActionIconForTesting(PageActionIconType type) = 0;
269 
270   // Returns the location bar.
271   virtual LocationBar* GetLocationBar() const = 0;
272 
273   // Tries to focus the location bar.  Clears the window focus (to avoid
274   // inconsistent state) if this fails.
275   virtual void SetFocusToLocationBar(bool select_all) = 0;
276 
277   // Informs the view whether or not a load is in progress for the current tab.
278   // The view can use this notification to update the reload/stop button.
279   virtual void UpdateReloadStopState(bool is_loading, bool force) = 0;
280 
281   // Updates the toolbar with the state for the specified |contents|.
282   virtual void UpdateToolbar(content::WebContents* contents) = 0;
283 
284   // Updates whether or not the custom tab bar is visible. Animates the
285   // transition if |animate| is true.
286   virtual void UpdateCustomTabBarVisibility(bool visible, bool animate) = 0;
287 
288   // Resets the toolbar's tab state for |contents|.
289   virtual void ResetToolbarTabState(content::WebContents* contents) = 0;
290 
291   // Focuses the toolbar (for accessibility).
292   virtual void FocusToolbar() = 0;
293 
294   // Returns the ExtensionsContainer associated with the window, if any.
295   virtual ExtensionsContainer* GetExtensionsContainer() = 0;
296 
297   // Called from toolbar subviews during their show/hide animations.
298   virtual void ToolbarSizeChanged(bool is_animating) = 0;
299 
300   // Called when the associated window's tab dragging status changed.
301   virtual void TabDraggingStatusChanged(bool is_dragging) = 0;
302 
303   // Called when a link is opened in the window from a user gesture.
304   // Link will be opened with |disposition|.
305   // TODO(crbug.com/1129028): see if this can't be piped through TabStripModel
306   // events instead.
307   virtual void LinkOpeningFromGesture(WindowOpenDisposition disposition) = 0;
308 
309   // Focuses the app menu like it was a menu bar.
310   //
311   // Not used on the Mac, which has a "normal" menu bar.
312   virtual void FocusAppMenu() = 0;
313 
314   // Focuses the bookmarks toolbar (for accessibility).
315   virtual void FocusBookmarksToolbar() = 0;
316 
317   // Focuses a visible but inactive popup for accessibility.
318   virtual void FocusInactivePopupForAccessibility() = 0;
319 
320   // Moves keyboard focus to the next pane.
321   virtual void RotatePaneFocus(bool forwards) = 0;
322 
323   // Returns whether the bookmark bar is visible or not.
324   virtual bool IsBookmarkBarVisible() const = 0;
325 
326   // Returns whether the bookmark bar is animating or not.
327   virtual bool IsBookmarkBarAnimating() const = 0;
328 
329   // Returns whether the tab strip is editable (for extensions).
330   virtual bool IsTabStripEditable() const = 0;
331 
332   // Returns whether the toolbar is available or not. It's called "Visible()"
333   // to follow the name convention. But it does not indicate the visibility of
334   // the toolbar, i.e. toolbar may be hidden, and only visible when the mouse
335   // cursor is at a certain place.
336   // TODO(zijiehe): Rename Visible() functions into Available() to match their
337   // original meaning.
338   virtual bool IsToolbarVisible() const = 0;
339 
340   // Returns whether the toolbar is showing up on the screen.
341   // TODO(zijiehe): Rename this function into IsToolbarVisible() once other
342   // Visible() functions are renamed to Available().
343   virtual bool IsToolbarShowing() const = 0;
344 
345   // Shows the dialog for a sharing feature.
346   virtual SharingDialog* ShowSharingDialog(content::WebContents* contents,
347                                            SharingDialogData data) = 0;
348 
349   // Shows the Update Recommended dialog box.
350   virtual void ShowUpdateChromeDialog() = 0;
351 
352   // Shows the intent picker bubble. |app_info| contains the app candidates to
353   // display, if |show_stay_in_chrome| is false, the 'Stay in
354   // Chrome' (used for non-http(s) queries) button is hidden, if
355   // |show_remember_selection| is false, the "remember my choice" checkbox is
356   // hidden and |callback| helps to continue the flow back to either
357   // AppsNavigationThrottle or ArcExternalProtocolDialog capturing the user's
358   // decision and storing UMA metrics.
359   virtual void ShowIntentPickerBubble(
360       std::vector<apps::IntentPickerAppInfo> app_info,
361       bool show_stay_in_chrome,
362       bool show_remember_selection,
363       PageActionIconType icon_type,
364       const base::Optional<url::Origin>& initiating_origin,
365       IntentPickerResponse callback) = 0;
366 
367   // Shows the Bookmark bubble. |url| is the URL being bookmarked,
368   // |already_bookmarked| is true if the url is already bookmarked.
369   virtual void ShowBookmarkBubble(const GURL& url, bool already_bookmarked) = 0;
370 
371   // Shows the QR Code generator bubble. |url| is the URL for the initial code.
372   virtual qrcode_generator::QRCodeGeneratorBubbleView*
373   ShowQRCodeGeneratorBubble(
374       content::WebContents* contents,
375       qrcode_generator::QRCodeGeneratorBubbleController* controller,
376       const GURL& url) = 0;
377 
378   // Shows the "send tab to self" bubble.
379   virtual send_tab_to_self::SendTabToSelfBubbleView* ShowSendTabToSelfBubble(
380       content::WebContents* contents,
381       send_tab_to_self::SendTabToSelfBubbleController* controller,
382       bool is_user_gesture) = 0;
383 
384   // Shows the translate bubble.
385   //
386   // |is_user_gesture| is true when the bubble is shown on the user's deliberate
387   // action.
388   virtual ShowTranslateBubbleResult ShowTranslateBubble(
389       content::WebContents* contents,
390       translate::TranslateStep step,
391       const std::string& source_language,
392       const std::string& target_language,
393       translate::TranslateErrors::Type error_type,
394       bool is_user_gesture) = 0;
395 
396 #if BUILDFLAG(ENABLE_ONE_CLICK_SIGNIN)
397   // Shows the one-click sign in confirmation UI. |email| holds the full email
398   // address of the account that has signed in.
399   virtual void ShowOneClickSigninConfirmation(
400       const base::string16& email,
401       base::OnceCallback<void(bool)> confirmed_callback) = 0;
402 #endif
403 
404   // Whether or not the shelf view is visible.
405   virtual bool IsDownloadShelfVisible() const = 0;
406 
407   // Returns the DownloadShelf.
408   virtual DownloadShelf* GetDownloadShelf() = 0;
409 
410   // Shows the confirmation dialog box warning that the browser is closing with
411   // in-progress downloads.
412   // This method should call |callback| with the user's response.
413   virtual void ConfirmBrowserCloseWithPendingDownloads(
414       int download_count,
415       Browser::DownloadCloseType dialog_type,
416       const base::Callback<void(bool)>& callback) = 0;
417 
418   // ThemeService calls this when a user has changed their theme, indicating
419   // that it's time to redraw everything.
420   virtual void UserChangedTheme(BrowserThemeChangeType theme_change_type) = 0;
421 
422   // Shows the app menu (for accessibility).
423   virtual void ShowAppMenu() = 0;
424 
425   // Allows the BrowserWindow object to handle the specified keyboard event
426   // before sending it to the renderer.
427   virtual content::KeyboardEventProcessingResult PreHandleKeyboardEvent(
428       const content::NativeWebKeyboardEvent& event) = 0;
429 
430   // Allows the BrowserWindow object to handle the specified keyboard event,
431   // if the renderer did not process it.
432   virtual bool HandleKeyboardEvent(
433       const content::NativeWebKeyboardEvent& event) = 0;
434 
435   // Clipboard commands applied to the whole browser window.
436   virtual void CutCopyPaste(int command_id) = 0;
437 
438   // Construct a FindBar implementation for the |browser|.
439   virtual std::unique_ptr<FindBar> CreateFindBar() = 0;
440 
441   // Return the WebContentsModalDialogHost for use in positioning web contents
442   // modal dialogs within the browser window. This can sometimes be NULL (for
443   // instance during tab drag on Views/Win32).
444   virtual web_modal::WebContentsModalDialogHost*
445   GetWebContentsModalDialogHost() = 0;
446 
447   // Construct a BrowserWindow implementation for the specified |browser|.
448   static BrowserWindow* CreateBrowserWindow(std::unique_ptr<Browser> browser,
449                                             bool user_gesture,
450                                             bool in_tab_dragging);
451 
452   // Shows the avatar bubble on the window frame off of the avatar button with
453   // the given mode. The Service Type specified by GAIA is provided as well.
454   // |access_point| indicates the access point used to open the Gaia sign in
455   // page.
456   enum AvatarBubbleMode {
457     AVATAR_BUBBLE_MODE_DEFAULT,
458     AVATAR_BUBBLE_MODE_SIGNIN,
459     AVATAR_BUBBLE_MODE_ADD_ACCOUNT,
460     AVATAR_BUBBLE_MODE_REAUTH,
461     AVATAR_BUBBLE_MODE_CONFIRM_SIGNIN
462   };
463   virtual void ShowAvatarBubbleFromAvatarButton(
464       AvatarBubbleMode mode,
465       signin_metrics::AccessPoint access_point,
466       bool is_source_keyboard) = 0;
467 
468   // Shows User Happiness Tracking Survey's invitation bubble when possible
469   // (such as having the proper anchor view).
470   // |site_id| is the site identification of the survey the bubble leads to.
471   // Note: |success_callback| and |failure_callback| are discarded for HaTS v1
472   // surveys, which are deprecated (crbug.com/1143176).
473   virtual void ShowHatsBubble(const std::string& site_id,
474                               base::OnceClosure success_callback,
475                               base::OnceClosure failure_callback) = 0;
476 
477   // Returns object implementing ExclusiveAccessContext interface.
478   virtual ExclusiveAccessContext* GetExclusiveAccessContext() = 0;
479 
480   // Shows in-product help for the given feature.
481   virtual void ShowInProductHelpPromo(InProductHelpFeature iph_feature) = 0;
482 
483   // Returns the platform-specific ID of the workspace the browser window
484   // currently resides in.
485   virtual std::string GetWorkspace() const = 0;
486   virtual bool IsVisibleOnAllWorkspaces() const = 0;
487 
488   // Shows the platform specific emoji picker.
489   virtual void ShowEmojiPanel() = 0;
490 
491   // Opens the eye dropper.
492   virtual std::unique_ptr<content::EyeDropper> OpenEyeDropper(
493       content::RenderFrameHost* frame,
494       content::EyeDropperListener* listener) = 0;
495 
496   // Shows a confirmation dialog about enabling caret browsing.
497   virtual void ShowCaretBrowsingDialog() = 0;
498 
499   // Create and open the tab search bubble.
500   virtual void CreateTabSearchBubble() = 0;
501   // Closes the tab search bubble if open for the given browser instance.
502   virtual void CloseTabSearchBubble() = 0;
503 
504   // Gets the windows's FeaturePromoController which manages display of
505   // in-product help.
506   virtual FeaturePromoController* GetFeaturePromoController() = 0;
507 
508  protected:
509   friend class BrowserCloseManager;
510   friend class BrowserView;
511   virtual void DestroyBrowser() = 0;
512 };
513 
514 #endif  // CHROME_BROWSER_UI_BROWSER_WINDOW_H_
515