Home | History | Annotate | Download | only in ui
      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 CHROME_BROWSER_UI_BROWSER_H_
      6 #define CHROME_BROWSER_UI_BROWSER_H_
      7 
      8 #include <map>
      9 #include <set>
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "base/basictypes.h"
     14 #include "base/compiler_specific.h"
     15 #include "base/gtest_prod_util.h"
     16 #include "base/memory/scoped_ptr.h"
     17 #include "base/memory/weak_ptr.h"
     18 #include "base/prefs/pref_change_registrar.h"
     19 #include "base/prefs/pref_member.h"
     20 #include "base/strings/string16.h"
     21 #include "chrome/browser/devtools/devtools_toggle_action.h"
     22 #include "chrome/browser/sessions/session_id.h"
     23 #include "chrome/browser/ui/bookmarks/bookmark_bar.h"
     24 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper_delegate.h"
     25 #include "chrome/browser/ui/browser_navigator.h"
     26 #include "chrome/browser/ui/chrome_web_modal_dialog_manager_delegate.h"
     27 #include "chrome/browser/ui/host_desktop.h"
     28 #include "chrome/browser/ui/search/search_tab_helper_delegate.h"
     29 #include "chrome/browser/ui/search_engines/search_engine_tab_helper_delegate.h"
     30 #include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h"
     31 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
     32 #include "chrome/browser/ui/toolbar/toolbar_model.h"
     33 #include "chrome/browser/ui/zoom/zoom_observer.h"
     34 #include "chrome/common/content_settings.h"
     35 #include "chrome/common/content_settings_types.h"
     36 #include "content/public/browser/notification_observer.h"
     37 #include "content/public/browser/notification_registrar.h"
     38 #include "content/public/browser/page_navigator.h"
     39 #include "content/public/browser/web_contents_delegate.h"
     40 #include "content/public/common/page_transition_types.h"
     41 #include "content/public/common/page_zoom.h"
     42 #include "ui/base/ui_base_types.h"
     43 #include "ui/base/window_open_disposition.h"
     44 #include "ui/gfx/rect.h"
     45 #include "ui/shell_dialogs/select_file_dialog.h"
     46 
     47 class BrowserContentSettingBubbleModelDelegate;
     48 class BrowserContentTranslateDriverObserver;
     49 class BrowserInstantController;
     50 class BrowserSyncedWindowDelegate;
     51 class BrowserToolbarModelDelegate;
     52 class BrowserTabRestoreServiceDelegate;
     53 class BrowserWindow;
     54 class FindBarController;
     55 class FullscreenController;
     56 class PrefService;
     57 class Profile;
     58 class SearchDelegate;
     59 class SearchModel;
     60 class StatusBubble;
     61 class TabStripModel;
     62 class TabStripModelDelegate;
     63 struct WebApplicationInfo;
     64 
     65 namespace chrome {
     66 class BrowserCommandController;
     67 class FastUnloadController;
     68 class UnloadController;
     69 class ValidationMessageBubble;
     70 }
     71 
     72 namespace content {
     73 class NavigationController;
     74 class PageState;
     75 class SessionStorageNamespace;
     76 }
     77 
     78 namespace extensions {
     79 class Extension;
     80 class WindowController;
     81 }
     82 
     83 namespace gfx {
     84 class Image;
     85 class Point;
     86 }
     87 
     88 namespace ui {
     89 struct SelectedFileInfo;
     90 class WebDialogDelegate;
     91 }
     92 
     93 namespace web_modal {
     94 class WebContentsModalDialogHost;
     95 }
     96 
     97 class Browser : public TabStripModelObserver,
     98                 public content::WebContentsDelegate,
     99                 public CoreTabHelperDelegate,
    100                 public SearchEngineTabHelperDelegate,
    101                 public SearchTabHelperDelegate,
    102                 public ChromeWebModalDialogManagerDelegate,
    103                 public BookmarkTabHelperDelegate,
    104                 public ZoomObserver,
    105                 public content::PageNavigator,
    106                 public content::NotificationObserver,
    107                 public ui::SelectFileDialog::Listener {
    108  public:
    109   // SessionService::WindowType mirrors these values.  If you add to this
    110   // enum, look at SessionService::WindowType to see if it needs to be
    111   // updated.
    112   enum Type {
    113     // If you add a new type, consider updating the test
    114     // BrowserTest.StartMaximized.
    115     TYPE_TABBED = 1,
    116     TYPE_POPUP = 2
    117   };
    118 
    119   // Possible elements of the Browser window.
    120   enum WindowFeature {
    121     FEATURE_NONE = 0,
    122     FEATURE_TITLEBAR = 1,
    123     FEATURE_TABSTRIP = 2,
    124     FEATURE_TOOLBAR = 4,
    125     FEATURE_LOCATIONBAR = 8,
    126     FEATURE_BOOKMARKBAR = 16,
    127     FEATURE_INFOBAR = 32,
    128     FEATURE_DOWNLOADSHELF = 64
    129   };
    130 
    131   // The context for a download blocked notification from
    132   // OkToCloseWithInProgressDownloads.
    133   enum DownloadClosePreventionType {
    134     // Browser close is not blocked by download state.
    135     DOWNLOAD_CLOSE_OK,
    136 
    137     // The browser is shutting down and there are active downloads
    138     // that would be cancelled.
    139     DOWNLOAD_CLOSE_BROWSER_SHUTDOWN,
    140 
    141     // There are active downloads associated with this incognito profile
    142     // that would be canceled.
    143     DOWNLOAD_CLOSE_LAST_WINDOW_IN_INCOGNITO_PROFILE,
    144   };
    145 
    146   struct CreateParams {
    147     CreateParams(Profile* profile, chrome::HostDesktopType host_desktop_type);
    148     CreateParams(Type type,
    149                  Profile* profile,
    150                  chrome::HostDesktopType host_desktop_type);
    151 
    152     static CreateParams CreateForApp(const std::string& app_name,
    153                                      bool trusted_source,
    154                                      const gfx::Rect& window_bounds,
    155                                      Profile* profile,
    156                                      chrome::HostDesktopType host_desktop_type);
    157 
    158     static CreateParams CreateForDevTools(
    159         Profile* profile,
    160         chrome::HostDesktopType host_desktop_type);
    161 
    162     // The browser type.
    163     Type type;
    164 
    165     // The associated profile.
    166     Profile* profile;
    167 
    168     // The host desktop the browser is created on.
    169     chrome::HostDesktopType host_desktop_type;
    170 
    171     // Specifies the browser is_trusted_source_ value.
    172     bool trusted_source;
    173 
    174     // The bounds of the window to open.
    175     gfx::Rect initial_bounds;
    176 
    177     ui::WindowShowState initial_show_state;
    178 
    179     bool is_session_restore;
    180 
    181     // Supply a custom BrowserWindow implementation, to be used instead of the
    182     // default. Intended for testing.
    183     BrowserWindow* window;
    184 
    185    private:
    186     friend class Browser;
    187 
    188     // The application name that is also the name of the window to the shell.
    189     // Do not set this value directly, use CreateForApp.
    190     // This name will be set for:
    191     // 1) v1 applications launched via an application shortcut or extension API.
    192     // 2) undocked devtool windows.
    193     // 3) popup windows spawned from v1 applications.
    194     std::string app_name;
    195   };
    196 
    197   // Constructors, Creation, Showing //////////////////////////////////////////
    198 
    199   explicit Browser(const CreateParams& params);
    200   virtual ~Browser();
    201 
    202   // Set overrides for the initial window bounds and maximized state.
    203   void set_override_bounds(const gfx::Rect& bounds) {
    204     override_bounds_ = bounds;
    205   }
    206   ui::WindowShowState initial_show_state() const { return initial_show_state_; }
    207   void set_initial_show_state(ui::WindowShowState initial_show_state) {
    208     initial_show_state_ = initial_show_state;
    209   }
    210   // Return true if the initial window bounds have been overridden.
    211   bool bounds_overridden() const {
    212     return !override_bounds_.IsEmpty();
    213   }
    214   // Set indicator that this browser is being created via session restore.
    215   // This is used on the Mac (only) to determine animation style when the
    216   // browser window is shown.
    217   void set_is_session_restore(bool is_session_restore) {
    218     is_session_restore_ = is_session_restore;
    219   }
    220   bool is_session_restore() const {
    221     return is_session_restore_;
    222   }
    223   chrome::HostDesktopType host_desktop_type() const {
    224     return host_desktop_type_;
    225   }
    226 
    227   // Accessors ////////////////////////////////////////////////////////////////
    228 
    229   Type type() const { return type_; }
    230   const std::string& app_name() const { return app_name_; }
    231   bool is_trusted_source() const { return is_trusted_source_; }
    232   Profile* profile() const { return profile_; }
    233   gfx::Rect override_bounds() const { return override_bounds_; }
    234 
    235   // |window()| will return NULL if called before |CreateBrowserWindow()|
    236   // is done.
    237   BrowserWindow* window() const { return window_; }
    238   ToolbarModel* toolbar_model() { return toolbar_model_.get(); }
    239   const ToolbarModel* toolbar_model() const { return toolbar_model_.get(); }
    240 #if defined(UNIT_TEST)
    241   void swap_toolbar_models(scoped_ptr<ToolbarModel>* toolbar_model) {
    242     toolbar_model->swap(toolbar_model_);
    243   }
    244 #endif
    245   TabStripModel* tab_strip_model() const { return tab_strip_model_.get(); }
    246   chrome::BrowserCommandController* command_controller() {
    247     return command_controller_.get();
    248   }
    249   SearchModel* search_model() { return search_model_.get(); }
    250   const SearchModel* search_model() const {
    251       return search_model_.get();
    252   }
    253   SearchDelegate* search_delegate() {
    254     return search_delegate_.get();
    255   }
    256   const SessionID& session_id() const { return session_id_; }
    257   BrowserContentSettingBubbleModelDelegate*
    258       content_setting_bubble_model_delegate() {
    259     return content_setting_bubble_model_delegate_.get();
    260   }
    261   BrowserTabRestoreServiceDelegate* tab_restore_service_delegate() {
    262     return tab_restore_service_delegate_.get();
    263   }
    264   BrowserSyncedWindowDelegate* synced_window_delegate() {
    265     return synced_window_delegate_.get();
    266   }
    267   BrowserInstantController* instant_controller() {
    268     return instant_controller_.get();
    269   }
    270 
    271   // Get the FindBarController for this browser, creating it if it does not
    272   // yet exist.
    273   FindBarController* GetFindBarController();
    274 
    275   // Returns true if a FindBarController exists for this browser.
    276   bool HasFindBarController() const;
    277 
    278   // Returns the state of the bookmark bar.
    279   BookmarkBar::State bookmark_bar_state() const { return bookmark_bar_state_; }
    280 
    281   // State Storage and Retrieval for UI ///////////////////////////////////////
    282 
    283   // Gets the Favicon of the page in the selected tab.
    284   gfx::Image GetCurrentPageIcon() const;
    285 
    286   // Gets the title of the window based on the selected tab's title.
    287   base::string16 GetWindowTitleForCurrentTab() const;
    288 
    289   // Prepares a title string for display (removes embedded newlines, etc).
    290   static void FormatTitleForDisplay(base::string16* title);
    291 
    292   // OnBeforeUnload handling //////////////////////////////////////////////////
    293 
    294   // Gives beforeunload handlers the chance to cancel the close. Returns whether
    295   // to proceed with the close. If called while the process begun by
    296   // CallBeforeUnloadHandlers is in progress, returns false without taking
    297   // action.
    298   bool ShouldCloseWindow();
    299 
    300   // Begins the process of confirming whether the associated browser can be
    301   // closed. If there are no tabs with beforeunload handlers it will immediately
    302   // return false. Otherwise, it starts prompting the user, returns true and
    303   // will call |on_close_confirmed| with the result of the user's decision.
    304   // After calling this function, if the window will not be closed, call
    305   // ResetBeforeUnloadHandlers() to reset all beforeunload handlers; calling
    306   // this function multiple times without an intervening call to
    307   // ResetBeforeUnloadHandlers() will run only the beforeunload handlers
    308   // registered since the previous call.
    309   bool CallBeforeUnloadHandlers(
    310       const base::Callback<void(bool)>& on_close_confirmed);
    311 
    312   // Clears the results of any beforeunload confirmation dialogs triggered by a
    313   // CallBeforeUnloadHandlers call.
    314   void ResetBeforeUnloadHandlers();
    315 
    316   // Figure out if there are tabs that have beforeunload handlers.
    317   // It starts beforeunload/unload processing as a side-effect.
    318   bool TabsNeedBeforeUnloadFired();
    319 
    320   // Returns true if all tabs' beforeunload/unload events have fired.
    321   bool HasCompletedUnloadProcessing() const;
    322 
    323   bool IsAttemptingToCloseBrowser() const;
    324 
    325   // Invoked when the window containing us is closing. Performs the necessary
    326   // cleanup.
    327   void OnWindowClosing();
    328 
    329   // In-progress download termination handling /////////////////////////////////
    330 
    331   // Called when the user has decided whether to proceed or not with the browser
    332   // closure.  |cancel_downloads| is true if the downloads should be canceled
    333   // and the browser closed, false if the browser should stay open and the
    334   // downloads running.
    335   void InProgressDownloadResponse(bool cancel_downloads);
    336 
    337   // Indicates whether or not this browser window can be closed, or
    338   // would be blocked by in-progress downloads.
    339   // If executing downloads would be cancelled by this window close,
    340   // then |*num_downloads_blocking| is updated with how many downloads
    341   // would be canceled if the close continued.
    342   DownloadClosePreventionType OkToCloseWithInProgressDownloads(
    343       int* num_downloads_blocking) const;
    344 
    345   // External state change handling ////////////////////////////////////////////
    346 
    347   // Invoked when the fullscreen state of the window changes.
    348   // BrowserWindow::EnterFullscreen invokes this after the window has become
    349   // fullscreen.
    350   void WindowFullscreenStateChanged();
    351 
    352   // Assorted browser commands ////////////////////////////////////////////////
    353 
    354   // NOTE: Within each of the following sections, the IDs are ordered roughly by
    355   // how they appear in the GUI/menus (left to right, top to bottom, etc.).
    356 
    357   // See the description of
    358   // FullscreenController::ToggleFullscreenModeWithExtension.
    359   void ToggleFullscreenModeWithExtension(const GURL& extension_url);
    360 
    361 #if defined(OS_WIN)
    362   // See the description of FullscreenController::ToggleMetroSnapMode.
    363   void SetMetroSnapMode(bool enable);
    364 #endif
    365 
    366   // Returns true if the Browser supports the specified feature. The value of
    367   // this varies during the lifetime of the browser. For example, if the window
    368   // is fullscreen this may return a different value. If you only care about
    369   // whether or not it's possible for the browser to support a particular
    370   // feature use |CanSupportWindowFeature|.
    371   bool SupportsWindowFeature(WindowFeature feature) const;
    372 
    373   // Returns true if the Browser can support the specified feature. See comment
    374   // in |SupportsWindowFeature| for details on this.
    375   bool CanSupportWindowFeature(WindowFeature feature) const;
    376 
    377   // TODO(port): port these, and re-merge the two function declaration lists.
    378   // Page-related commands.
    379   void ToggleEncodingAutoDetect();
    380   void OverrideEncoding(int encoding_id);
    381 
    382   // Show various bits of UI
    383   void OpenFile();
    384 
    385   void UpdateDownloadShelfVisibility(bool visible);
    386 
    387   /////////////////////////////////////////////////////////////////////////////
    388 
    389   // Called by chrome::Navigate() when a navigation has occurred in a tab in
    390   // this Browser. Updates the UI for the start of this navigation.
    391   void UpdateUIForNavigationInTab(content::WebContents* contents,
    392                                   content::PageTransition transition,
    393                                   bool user_initiated);
    394 
    395   // Interface implementations ////////////////////////////////////////////////
    396 
    397   // Overridden from content::PageNavigator:
    398   virtual content::WebContents* OpenURL(
    399       const content::OpenURLParams& params) OVERRIDE;
    400 
    401   // Overridden from TabStripModelObserver:
    402   virtual void TabInsertedAt(content::WebContents* contents,
    403                              int index,
    404                              bool foreground) OVERRIDE;
    405   virtual void TabClosingAt(TabStripModel* tab_strip_model,
    406                             content::WebContents* contents,
    407                             int index) OVERRIDE;
    408   virtual void TabDetachedAt(content::WebContents* contents,
    409                              int index) OVERRIDE;
    410   virtual void TabDeactivated(content::WebContents* contents) OVERRIDE;
    411   virtual void ActiveTabChanged(content::WebContents* old_contents,
    412                                 content::WebContents* new_contents,
    413                                 int index,
    414                                 int reason) OVERRIDE;
    415   virtual void TabMoved(content::WebContents* contents,
    416                         int from_index,
    417                         int to_index) OVERRIDE;
    418   virtual void TabReplacedAt(TabStripModel* tab_strip_model,
    419                              content::WebContents* old_contents,
    420                              content::WebContents* new_contents,
    421                              int index) OVERRIDE;
    422   virtual void TabPinnedStateChanged(content::WebContents* contents,
    423                                      int index) OVERRIDE;
    424   virtual void TabStripEmpty() OVERRIDE;
    425 
    426   // Overridden from content::WebContentsDelegate:
    427   virtual bool CanOverscrollContent() const OVERRIDE;
    428   virtual bool ShouldPreserveAbortedURLs(content::WebContents* source) OVERRIDE;
    429   virtual bool PreHandleKeyboardEvent(
    430       content::WebContents* source,
    431       const content::NativeWebKeyboardEvent& event,
    432       bool* is_keyboard_shortcut) OVERRIDE;
    433   virtual void HandleKeyboardEvent(
    434       content::WebContents* source,
    435       const content::NativeWebKeyboardEvent& event) OVERRIDE;
    436   virtual void OverscrollUpdate(int delta_y) OVERRIDE;
    437   virtual void ShowValidationMessage(content::WebContents* web_contents,
    438                                      const gfx::Rect& anchor_in_root_view,
    439                                      const base::string16& main_text,
    440                                      const base::string16& sub_text) OVERRIDE;
    441   virtual void HideValidationMessage(
    442       content::WebContents* web_contents) OVERRIDE;
    443   virtual void MoveValidationMessage(
    444       content::WebContents* web_contents,
    445       const gfx::Rect& anchor_in_root_view) OVERRIDE;
    446   virtual bool PreHandleGestureEvent(
    447       content::WebContents* source,
    448       const blink::WebGestureEvent& event) OVERRIDE;
    449 
    450   bool is_type_tabbed() const { return type_ == TYPE_TABBED; }
    451   bool is_type_popup() const { return type_ == TYPE_POPUP; }
    452 
    453   bool is_app() const;
    454   bool is_devtools() const;
    455 
    456   // True when the mouse cursor is locked.
    457   bool IsMouseLocked() const;
    458 
    459   // Called each time the browser window is shown.
    460   void OnWindowDidShow();
    461 
    462   // Show the first run search engine bubble on the location bar.
    463   void ShowFirstRunBubble();
    464 
    465   // Show a download on the download shelf.
    466   void ShowDownload(content::DownloadItem* download);
    467 
    468   FullscreenController* fullscreen_controller() const {
    469     return fullscreen_controller_.get();
    470   }
    471 
    472   extensions::WindowController* extension_window_controller() const {
    473     return extension_window_controller_.get();
    474   }
    475 
    476  private:
    477   friend class BrowserTest;
    478   friend class FullscreenControllerInteractiveTest;
    479   friend class FullscreenControllerTest;
    480   FRIEND_TEST_ALL_PREFIXES(AppModeTest, EnableAppModeTest);
    481   FRIEND_TEST_ALL_PREFIXES(BrowserCommandControllerTest,
    482                            IsReservedCommandOrKeyIsApp);
    483   FRIEND_TEST_ALL_PREFIXES(BrowserCommandControllerTest, AppFullScreen);
    484   FRIEND_TEST_ALL_PREFIXES(BrowserTest, NoTabsInPopups);
    485   FRIEND_TEST_ALL_PREFIXES(BrowserTest, ConvertTabToAppShortcut);
    486   FRIEND_TEST_ALL_PREFIXES(BrowserTest, OpenAppWindowLikeNtp);
    487   FRIEND_TEST_ALL_PREFIXES(BrowserTest, AppIdSwitch);
    488   FRIEND_TEST_ALL_PREFIXES(BrowserTest, ShouldShowLocationBar);
    489   FRIEND_TEST_ALL_PREFIXES(FullscreenControllerTest,
    490                            TabEntersPresentationModeFromWindowed);
    491   FRIEND_TEST_ALL_PREFIXES(FullscreenExitBubbleControllerTest,
    492                            DenyExitsFullscreen);
    493   FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest, OpenAppShortcutNoPref);
    494   FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest,
    495                            OpenAppShortcutWindowPref);
    496   FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest, OpenAppShortcutTabPref);
    497 
    498   class InterstitialObserver;
    499 
    500   // Used to describe why a tab is being detached. This is used by
    501   // TabDetachedAtImpl.
    502   enum DetachType {
    503     // Result of TabDetachedAt.
    504     DETACH_TYPE_DETACH,
    505 
    506     // Result of TabReplacedAt.
    507     DETACH_TYPE_REPLACE,
    508 
    509     // Result of the tab strip not having any significant tabs.
    510     DETACH_TYPE_EMPTY
    511   };
    512 
    513   // Describes where the bookmark bar state change originated from.
    514   enum BookmarkBarStateChangeReason {
    515     // From the constructor.
    516     BOOKMARK_BAR_STATE_CHANGE_INIT,
    517 
    518     // Change is the result of the active tab changing.
    519     BOOKMARK_BAR_STATE_CHANGE_TAB_SWITCH,
    520 
    521     // Change is the result of the bookmark bar pref changing.
    522     BOOKMARK_BAR_STATE_CHANGE_PREF_CHANGE,
    523 
    524     // Change is the result of a state change in the active tab.
    525     BOOKMARK_BAR_STATE_CHANGE_TAB_STATE,
    526 
    527     // Change is the result of window toggling in/out of fullscreen mode.
    528     BOOKMARK_BAR_STATE_CHANGE_TOGGLE_FULLSCREEN,
    529   };
    530 
    531   // Overridden from content::WebContentsDelegate:
    532   virtual content::WebContents* OpenURLFromTab(
    533       content::WebContents* source,
    534       const content::OpenURLParams& params) OVERRIDE;
    535   virtual void NavigationStateChanged(const content::WebContents* source,
    536                                       unsigned changed_flags) OVERRIDE;
    537   virtual void VisibleSSLStateChanged(
    538       const content::WebContents* source) OVERRIDE;
    539   virtual void AddNewContents(content::WebContents* source,
    540                               content::WebContents* new_contents,
    541                               WindowOpenDisposition disposition,
    542                               const gfx::Rect& initial_pos,
    543                               bool user_gesture,
    544                               bool* was_blocked) OVERRIDE;
    545   virtual void ActivateContents(content::WebContents* contents) OVERRIDE;
    546   virtual void DeactivateContents(content::WebContents* contents) OVERRIDE;
    547   virtual void LoadingStateChanged(content::WebContents* source,
    548                                    bool to_different_document) OVERRIDE;
    549   virtual void CloseContents(content::WebContents* source) OVERRIDE;
    550   virtual void MoveContents(content::WebContents* source,
    551                             const gfx::Rect& pos) OVERRIDE;
    552   virtual bool IsPopupOrPanel(
    553       const content::WebContents* source) const OVERRIDE;
    554   virtual void UpdateTargetURL(content::WebContents* source, int32 page_id,
    555                                const GURL& url) OVERRIDE;
    556   virtual void ContentsMouseEvent(content::WebContents* source,
    557                                   const gfx::Point& location,
    558                                   bool motion) OVERRIDE;
    559   virtual void ContentsZoomChange(bool zoom_in) OVERRIDE;
    560   virtual void WebContentsFocused(content::WebContents* content) OVERRIDE;
    561   virtual bool TakeFocus(content::WebContents* source, bool reverse) OVERRIDE;
    562   virtual gfx::Rect GetRootWindowResizerRect() const OVERRIDE;
    563   virtual void BeforeUnloadFired(content::WebContents* source,
    564                                  bool proceed,
    565                                  bool* proceed_to_fire_unload) OVERRIDE;
    566   virtual bool ShouldFocusLocationBarByDefault(
    567       content::WebContents* source) OVERRIDE;
    568   virtual void SetFocusToLocationBar(bool select_all) OVERRIDE;
    569   virtual int GetExtraRenderViewHeight() const OVERRIDE;
    570   virtual void ViewSourceForTab(content::WebContents* source,
    571                                 const GURL& page_url) OVERRIDE;
    572   virtual void ViewSourceForFrame(
    573       content::WebContents* source,
    574       const GURL& frame_url,
    575       const content::PageState& frame_page_state) OVERRIDE;
    576   virtual void ShowRepostFormWarningDialog(
    577       content::WebContents* source) OVERRIDE;
    578   virtual bool ShouldCreateWebContents(
    579       content::WebContents* web_contents,
    580       int route_id,
    581       WindowContainerType window_container_type,
    582       const base::string16& frame_name,
    583       const GURL& target_url,
    584       const std::string& partition_id,
    585       content::SessionStorageNamespace* session_storage_namespace) OVERRIDE;
    586   virtual void WebContentsCreated(content::WebContents* source_contents,
    587                                   int opener_render_frame_id,
    588                                   const base::string16& frame_name,
    589                                   const GURL& target_url,
    590                                   content::WebContents* new_contents) OVERRIDE;
    591   virtual void RendererUnresponsive(content::WebContents* source) OVERRIDE;
    592   virtual void RendererResponsive(content::WebContents* source) OVERRIDE;
    593   virtual void WorkerCrashed(content::WebContents* source) OVERRIDE;
    594   virtual void DidNavigateMainFramePostCommit(
    595       content::WebContents* web_contents) OVERRIDE;
    596   virtual void DidNavigateToPendingEntry(
    597       content::WebContents* web_contents) OVERRIDE;
    598   virtual content::JavaScriptDialogManager*
    599       GetJavaScriptDialogManager() OVERRIDE;
    600   virtual content::ColorChooser* OpenColorChooser(
    601       content::WebContents* web_contents,
    602       SkColor color,
    603       const std::vector<content::ColorSuggestion>& suggestions) OVERRIDE;
    604   virtual void RunFileChooser(
    605       content::WebContents* web_contents,
    606       const content::FileChooserParams& params) OVERRIDE;
    607   virtual void EnumerateDirectory(content::WebContents* web_contents,
    608                                   int request_id,
    609                                   const base::FilePath& path) OVERRIDE;
    610   virtual bool EmbedsFullscreenWidget() const OVERRIDE;
    611   virtual void ToggleFullscreenModeForTab(content::WebContents* web_contents,
    612       bool enter_fullscreen) OVERRIDE;
    613   virtual bool IsFullscreenForTabOrPending(
    614       const content::WebContents* web_contents) const OVERRIDE;
    615   virtual void RegisterProtocolHandler(content::WebContents* web_contents,
    616                                        const std::string& protocol,
    617                                        const GURL& url,
    618                                        bool user_gesture) OVERRIDE;
    619   virtual void UpdatePreferredSize(content::WebContents* source,
    620                                    const gfx::Size& pref_size) OVERRIDE;
    621   virtual void ResizeDueToAutoResize(content::WebContents* source,
    622                                      const gfx::Size& new_size) OVERRIDE;
    623   virtual void FindReply(content::WebContents* web_contents,
    624                          int request_id,
    625                          int number_of_matches,
    626                          const gfx::Rect& selection_rect,
    627                          int active_match_ordinal,
    628                          bool final_update) OVERRIDE;
    629   virtual void RequestToLockMouse(content::WebContents* web_contents,
    630                                   bool user_gesture,
    631                                   bool last_unlocked_by_target) OVERRIDE;
    632   virtual void LostMouseLock() OVERRIDE;
    633   virtual void RequestMediaAccessPermission(
    634       content::WebContents* web_contents,
    635       const content::MediaStreamRequest& request,
    636       const content::MediaResponseCallback& callback) OVERRIDE;
    637   virtual bool RequestPpapiBrokerPermission(
    638       content::WebContents* web_contents,
    639       const GURL& url,
    640       const base::FilePath& plugin_path,
    641       const base::Callback<void(bool)>& callback) OVERRIDE;
    642   virtual gfx::Size GetSizeForNewRenderView(
    643       content::WebContents* web_contents) const OVERRIDE;
    644 
    645   // Overridden from CoreTabHelperDelegate:
    646   // Note that the caller is responsible for deleting |old_contents|.
    647   virtual void SwapTabContents(content::WebContents* old_contents,
    648                                content::WebContents* new_contents,
    649                                bool did_start_load,
    650                                bool did_finish_load) OVERRIDE;
    651   virtual bool CanReloadContents(
    652       content::WebContents* web_contents) const OVERRIDE;
    653   virtual bool CanSaveContents(
    654       content::WebContents* web_contents) const OVERRIDE;
    655 
    656   // Overridden from SearchEngineTabHelperDelegate:
    657   virtual void ConfirmAddSearchProvider(TemplateURL* template_url,
    658                                         Profile* profile) OVERRIDE;
    659 
    660   // Overridden from SearchTabHelperDelegate:
    661   virtual void NavigateOnThumbnailClick(
    662       const GURL& url,
    663       WindowOpenDisposition disposition,
    664       content::WebContents* source_contents) OVERRIDE;
    665   virtual void OnWebContentsInstantSupportDisabled(
    666       const content::WebContents* web_contents) OVERRIDE;
    667   virtual OmniboxView* GetOmniboxView() OVERRIDE;
    668   virtual std::set<std::string> GetOpenUrls() OVERRIDE;
    669 
    670   // Overridden from WebContentsModalDialogManagerDelegate:
    671   virtual void SetWebContentsBlocked(content::WebContents* web_contents,
    672                                      bool blocked) OVERRIDE;
    673   virtual web_modal::WebContentsModalDialogHost*
    674       GetWebContentsModalDialogHost() OVERRIDE;
    675 
    676   // Overridden from BookmarkTabHelperDelegate:
    677   virtual void URLStarredChanged(content::WebContents* web_contents,
    678                                  bool starred) OVERRIDE;
    679 
    680   // Overridden from ZoomObserver:
    681   virtual void OnZoomChanged(content::WebContents* source,
    682                              bool can_show_bubble) OVERRIDE;
    683 
    684   // Overridden from SelectFileDialog::Listener:
    685   virtual void FileSelected(const base::FilePath& path,
    686                             int index,
    687                             void* params) OVERRIDE;
    688   virtual void FileSelectedWithExtraInfo(
    689       const ui::SelectedFileInfo& file_info,
    690       int index,
    691       void* params) OVERRIDE;
    692 
    693   // Overridden from content::NotificationObserver:
    694   virtual void Observe(int type,
    695                        const content::NotificationSource& source,
    696                        const content::NotificationDetails& details) OVERRIDE;
    697 
    698   // Command and state updating ///////////////////////////////////////////////
    699 
    700   // Handle changes to kDevTools preference.
    701   void OnDevToolsDisabledChanged();
    702 
    703   // UI update coalescing and handling ////////////////////////////////////////
    704 
    705   // Asks the toolbar (and as such the location bar) to update its state to
    706   // reflect the current tab's current URL, security state, etc.
    707   // If |should_restore_state| is true, we're switching (back?) to this tab and
    708   // should restore any previous location bar state (such as user editing) as
    709   // well.
    710   void UpdateToolbar(bool should_restore_state);
    711 
    712   // Does one or both of the following for each bit in |changed_flags|:
    713   // . If the update should be processed immediately, it is.
    714   // . If the update should processed asynchronously (to avoid lots of ui
    715   //   updates), then scheduled_updates_ is updated for the |source| and update
    716   //   pair and a task is scheduled (assuming it isn't running already)
    717   //   that invokes ProcessPendingUIUpdates.
    718   void ScheduleUIUpdate(const content::WebContents* source,
    719                         unsigned changed_flags);
    720 
    721   // Processes all pending updates to the UI that have been scheduled by
    722   // ScheduleUIUpdate in scheduled_updates_.
    723   void ProcessPendingUIUpdates();
    724 
    725   // Removes all entries from scheduled_updates_ whose source is contents.
    726   void RemoveScheduledUpdatesFor(content::WebContents* contents);
    727 
    728   // Getters for UI ///////////////////////////////////////////////////////////
    729 
    730   // TODO(beng): remove, and provide AutomationProvider a better way to access
    731   //             the LocationBarView's edit.
    732   friend class AutomationProvider;
    733   friend class BrowserProxy;
    734   friend class TestingAutomationProvider;
    735 
    736   // Returns the StatusBubble from the current toolbar. It is possible for
    737   // this to return NULL if called before the toolbar has initialized.
    738   // TODO(beng): remove this.
    739   StatusBubble* GetStatusBubble();
    740 
    741   // Session restore functions ////////////////////////////////////////////////
    742 
    743   // Notifies the history database of the index for all tabs whose index is
    744   // >= index.
    745   void SyncHistoryWithTabs(int index);
    746 
    747   // In-progress download termination handling /////////////////////////////////
    748 
    749   // Called when the window is closing to check if potential in-progress
    750   // downloads should prevent it from closing.
    751   // Returns true if the window can close, false otherwise.
    752   bool CanCloseWithInProgressDownloads();
    753 
    754   // Assorted utility functions ///////////////////////////////////////////////
    755 
    756   // Sets the specified browser as the delegate of the WebContents and all the
    757   // associated tab helpers that are needed.
    758   void SetAsDelegate(content::WebContents* web_contents, Browser* delegate);
    759 
    760   // Shows the Find Bar, optionally selecting the next entry that matches the
    761   // existing search string for that Tab. |forward_direction| controls the
    762   // search direction.
    763   void FindInPage(bool find_next, bool forward_direction);
    764 
    765   // Closes the frame.
    766   // TODO(beng): figure out if we need this now that the frame itself closes
    767   //             after a return to the message loop.
    768   void CloseFrame();
    769 
    770   void TabDetachedAtImpl(content::WebContents* contents,
    771                          int index,
    772                          DetachType type);
    773 
    774   // Shared code between Reload() and ReloadIgnoringCache().
    775   void ReloadInternal(WindowOpenDisposition disposition, bool ignore_cache);
    776 
    777   // Returns true if the Browser window should show the location bar.
    778   bool ShouldShowLocationBar() const;
    779 
    780   // Implementation of SupportsWindowFeature and CanSupportWindowFeature. If
    781   // |check_fullscreen| is true, the set of features reflect the actual state of
    782   // the browser, otherwise the set of features reflect the possible state of
    783   // the browser.
    784   bool SupportsWindowFeatureImpl(WindowFeature feature,
    785                                  bool check_fullscreen) const;
    786 
    787   // Resets |bookmark_bar_state_| based on the active tab. Notifies the
    788   // BrowserWindow if necessary.
    789   void UpdateBookmarkBarState(BookmarkBarStateChangeReason reason);
    790 
    791   bool ShouldHideUIForFullscreen() const;
    792 
    793   // Creates a BackgroundContents if appropriate; return true if one was
    794   // created.
    795   bool MaybeCreateBackgroundContents(
    796       int route_id,
    797       content::WebContents* opener_web_contents,
    798       const base::string16& frame_name,
    799       const GURL& target_url,
    800       const std::string& partition_id,
    801       content::SessionStorageNamespace* session_storage_namespace);
    802 
    803   // Data members /////////////////////////////////////////////////////////////
    804 
    805   std::vector<InterstitialObserver*> interstitial_observers_;
    806 
    807   content::NotificationRegistrar registrar_;
    808 
    809   PrefChangeRegistrar profile_pref_registrar_;
    810 
    811   // This Browser's type.
    812   const Type type_;
    813 
    814   // This Browser's profile.
    815   Profile* const profile_;
    816 
    817   // This Browser's window.
    818   BrowserWindow* window_;
    819 
    820   scoped_ptr<TabStripModelDelegate> tab_strip_model_delegate_;
    821   scoped_ptr<TabStripModel> tab_strip_model_;
    822 
    823   // The application name that is also the name of the window to the shell.
    824   // This name should be set when:
    825   // 1) we launch an application via an application shortcut or extension API.
    826   // 2) we launch an undocked devtool window.
    827   std::string app_name_;
    828 
    829   // True if the source is trusted (i.e. we do not need to show the URL in a
    830   // a popup window). Also used to determine which app windows to save and
    831   // restore on Chrome OS.
    832   bool is_trusted_source_;
    833 
    834   // Unique identifier of this browser for session restore. This id is only
    835   // unique within the current session, and is not guaranteed to be unique
    836   // across sessions.
    837   const SessionID session_id_;
    838 
    839   // The model for the toolbar view.
    840   scoped_ptr<ToolbarModel> toolbar_model_;
    841 
    842   // The model for the "active" search state.  There are per-tab search models
    843   // as well.  When a tab is active its model is kept in sync with this one.
    844   // When a new tab is activated its model state is propagated to this active
    845   // model.  This way, observers only have to attach to this single model for
    846   // updates, and don't have to worry about active tab changes directly.
    847   scoped_ptr<SearchModel> search_model_;
    848 
    849   // UI update coalescing and handling ////////////////////////////////////////
    850 
    851   typedef std::map<const content::WebContents*, int> UpdateMap;
    852 
    853   // Maps from WebContents to pending UI updates that need to be processed.
    854   // We don't update things like the URL or tab title right away to avoid
    855   // flickering and extra painting.
    856   // See ScheduleUIUpdate and ProcessPendingUIUpdates.
    857   UpdateMap scheduled_updates_;
    858 
    859   // In-progress download termination handling /////////////////////////////////
    860 
    861   enum CancelDownloadConfirmationState {
    862     NOT_PROMPTED,          // We have not asked the user.
    863     WAITING_FOR_RESPONSE,  // We have asked the user and have not received a
    864                            // reponse yet.
    865     RESPONSE_RECEIVED      // The user was prompted and made a decision already.
    866   };
    867 
    868   // State used to figure-out whether we should prompt the user for confirmation
    869   // when the browser is closed with in-progress downloads.
    870   CancelDownloadConfirmationState cancel_download_confirmation_state_;
    871 
    872   /////////////////////////////////////////////////////////////////////////////
    873 
    874   // Override values for the bounds of the window and its maximized or minimized
    875   // state.
    876   // These are supplied by callers that don't want to use the default values.
    877   // The default values are typically loaded from local state (last session),
    878   // obtained from the last window of the same type, or obtained from the
    879   // shell shortcut's startup info.
    880   gfx::Rect override_bounds_;
    881   ui::WindowShowState initial_show_state_;
    882 
    883   // Tracks when this browser is being created by session restore.
    884   bool is_session_restore_;
    885 
    886   const chrome::HostDesktopType host_desktop_type_;
    887 
    888   scoped_ptr<chrome::UnloadController> unload_controller_;
    889   scoped_ptr<chrome::FastUnloadController> fast_unload_controller_;
    890 
    891   // The Find Bar. This may be NULL if there is no Find Bar, and if it is
    892   // non-NULL, it may or may not be visible.
    893   scoped_ptr<FindBarController> find_bar_controller_;
    894 
    895   // Dialog box used for opening and saving files.
    896   scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
    897 
    898   // Keep track of the encoding auto detect pref.
    899   BooleanPrefMember encoding_auto_detect_;
    900 
    901   // Helper which implements the ContentSettingBubbleModel interface.
    902   scoped_ptr<BrowserContentSettingBubbleModelDelegate>
    903       content_setting_bubble_model_delegate_;
    904 
    905   // Helper which implements the ToolbarModelDelegate interface.
    906   scoped_ptr<BrowserToolbarModelDelegate> toolbar_model_delegate_;
    907 
    908   // A delegate that handles the details of updating the "active"
    909   // |search_model_| state with the tab's state.
    910   scoped_ptr<SearchDelegate> search_delegate_;
    911 
    912   // Helper which implements the TabRestoreServiceDelegate interface.
    913   scoped_ptr<BrowserTabRestoreServiceDelegate> tab_restore_service_delegate_;
    914 
    915   // Helper which implements the SyncedWindowDelegate interface.
    916   scoped_ptr<BrowserSyncedWindowDelegate> synced_window_delegate_;
    917 
    918   scoped_ptr<BrowserInstantController> instant_controller_;
    919 
    920   BookmarkBar::State bookmark_bar_state_;
    921 
    922   scoped_ptr<FullscreenController> fullscreen_controller_;
    923 
    924   scoped_ptr<extensions::WindowController> extension_window_controller_;
    925 
    926   scoped_ptr<chrome::BrowserCommandController> command_controller_;
    927 
    928   // True if the browser window has been shown at least once.
    929   bool window_has_shown_;
    930 
    931   // The following factory is used for chrome update coalescing.
    932   base::WeakPtrFactory<Browser> chrome_updater_factory_;
    933 
    934   // The following factory is used to close the frame at a later time.
    935   base::WeakPtrFactory<Browser> weak_factory_;
    936 
    937   scoped_ptr<BrowserContentTranslateDriverObserver> translate_driver_observer_;
    938 
    939   scoped_ptr<chrome::ValidationMessageBubble> validation_message_bubble_;
    940 
    941   DISALLOW_COPY_AND_ASSIGN(Browser);
    942 };
    943 
    944 #endif  // CHROME_BROWSER_UI_BROWSER_H_
    945