summaryrefslogtreecommitdiffstats
path: root/chrome/browser/browser.h
blob: 510656fc470ee0da3021fb1526d70e1a4cc026f5 (plain)
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_BROWSER_H_
#define CHROME_BROWSER_BROWSER_H_

#include "chrome/browser/controller.h"
#include "chrome/browser/shell_dialogs.h"
#include "chrome/browser/browser_type.h"
#include "chrome/browser/session_id.h"
#include "chrome/browser/tab_contents_delegate.h"
#include "chrome/browser/tabs/tab_strip_model.h"
#include "chrome/browser/toolbar_model.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/pref_member.h"

class BrowserIdleTimer;
class BrowserWindow;
class DebuggerWindow;
class GoButton;
class LocationBarView;
class PrefService;
class Profile;
class StatusBubble;
struct TabNavigation;
class WebContents;
class WebApp;

class Browser : public TabStripModelDelegate,
                public TabStripModelObserver,
                public TabContentsDelegate,
                public CommandHandler,
                public NotificationObserver,
                public SelectFileDialog::Listener {
 public:
  // Constructors, Creation, Showing //////////////////////////////////////////

  // Creates a new browser with the given bounds. If the bounds are empty, the
  // system will try to find a saved size from a previous session, if none
  // exists, the operating system will be allowed to size the window.
  // |type| defines the kind of browser to create.
  //
  // Creating a browser does NOT show the window. You must manually call Show()
  // to display the window.
  Browser(const gfx::Rect& initial_bounds,
          int show_command,
          Profile* profile,
          BrowserType::Type browser_type,
          const std::wstring& app_name);
  ~Browser();

  // Shows the browser window. It is initially created hidden. It will be shown
  // with the show command passed to the constructor, or possibly another state
  // if it was overridden in the preferences.
  //
  // Ideally, this function is called after everything in the window is
  // initialized so that we do not have to repaint again.
  void Show();

  // Accessors ////////////////////////////////////////////////////////////////

  BrowserType::Type type() const { return type_; }
  Profile* profile() const { return profile_; }
  BrowserWindow* window() const { return window_; }
  ToolbarModel* toolbar_model() { return &toolbar_model_; }
  const SessionID& session_id() const { return session_id_; }
  CommandController* controller() { return &controller_; }

  // Browser Creation Helpers /////////////////////////////////////////////////

  // Opens a new browser window for the specified |profile|, shown according to
  // |show_command|
  static void OpenNewBrowserWindow(Profile* profile, int show_command);

  // Opens the specified URL in a new browser window in an incognito session.
  // If there is already an existing active incognito session for the specified
  // |profile|, that session is re-used.
  static void OpenURLOffTheRecord(Profile* profile, const GURL& url);

  // Opens the a new application window for the specified WebApp.
  static void OpenWebApplication(Profile* profile,
                                 WebApp* app,
                                 int show_command);

  // Command API //////////////////////////////////////////////////////////////

  // Please fix the incestuous nest that is */controller.h and eliminate the
  // need for this retarded hack.
  bool SupportsCommand(int id) const;
  bool IsCommandEnabled(int id) const;

  // DEPRECATED DEPRECATED DEPRECATED /////////////////////////////////////////

  // Returns the HWND of the top-level system window for this Browser.
  HWND GetTopLevelHWND() const;

  // State Storage and Retrieval for UI ///////////////////////////////////////

  // Save and restore the window position.
  void SaveWindowPosition(const gfx::Rect& bounds, bool maximized);
  void RestoreWindowPosition(gfx::Rect* bounds, bool* maximized);

  // Gets the FavIcon of the page in the selected tab.
  SkBitmap GetCurrentPageIcon() const;

  // Gets the title of the page in the selected tab.
  std::wstring GetCurrentPageTitle() const;

  // Prepares a title string for display (removes embedded newlines, etc).
  static void FormatTitleForDisplay(std::wstring* title);

  // OnBeforeUnload handling //////////////////////////////////////////////////

  // Gives beforeunload handlers the chance to cancel the close.
  bool ShouldCloseWindow();

  // Invoked when the window containing us is closing. Performs the necessary
  // cleanup.
  void OnWindowClosing();

  // TabStripModel pass-thrus /////////////////////////////////////////////////

  TabStripModel* tabstrip_model() const {
    return const_cast<TabStripModel*>(&tabstrip_model_);
  }

  int tab_count() const { return tabstrip_model_.count(); }
  int selected_index() const { return tabstrip_model_.selected_index(); }
  int GetIndexOfController(const NavigationController* controller) const {
    return tabstrip_model_.GetIndexOfController(controller);
  }
  TabContents* GetTabContentsAt(int index) const {
    return tabstrip_model_.GetTabContentsAt(index);
  }
  TabContents* GetSelectedTabContents() const {
    return tabstrip_model_.GetSelectedTabContents();
  }
  NavigationController* GetSelectedNavigationController() const;
  void SelectTabContentsAt(int index, bool user_gesture) {
    tabstrip_model_.SelectTabContentsAt(index, user_gesture);
  }
  TabContents* AddBlankTab(bool foreground) {
    return tabstrip_model_.AddBlankTab(foreground);
  }
  void CloseAllTabs() {
    tabstrip_model_.CloseAllTabs();
  }

  // Tab adding/showing functions /////////////////////////////////////////////

  // Add a new tab with the specified URL. If instance is not null, its process
  // will be used to render the tab.
  TabContents* AddTabWithURL(
      const GURL& url, const GURL& referrer,
      PageTransition::Type transition, bool foreground,
      SiteInstance* instance);

  // Add a new application tab for the specified URL. If lazy is true, the tab
  // won't be selected. Further, the initial web page load will only take place
  // when the tab is first selected.
  TabContents* AddWebApplicationTab(Profile* profile,
                                    WebApp* web_app,
                                    bool lazy);

  // Add a new tab, given a NavigationController. A TabContents appropriate to
  // display the last committed entry is created and returned.
  TabContents* AddTabWithNavigationController(NavigationController* ctrl,
                                              PageTransition::Type type);

  // Add a tab with its session history restored from the SessionRestore
  // system. If select is true, the tab is selected. Returns the created
  // NavigationController. |tab_index| gives the index to insert the tab at.
  // |selected_navigation| is the index of the TabNavigation in |navigations|
  // to select.
  NavigationController* AddRestoredTab(
      const std::vector<TabNavigation>& navigations,
      int tab_index,
      int selected_navigation,
      bool select);

  // Replaces the state of the currently selected tab with the session
  // history restored from the SessionRestore system.
  void ReplaceRestoredTab(
      const std::vector<TabNavigation>& navigations,
      int selected_navigation);

  // Show a native UI tab given a URL. If a tab with the same URL is already
  // visible in this browser, it becomes selected. Otherwise a new tab is
  // created.
  void ShowNativeUITab(const GURL& url);

  // Assorted browser commands ////////////////////////////////////////////////

  // Navigation commands
  void GoBack();
  void GoForward();
  void Reload();
  void Stop();
  void Home();
  void Go();
  void OpenCurrentURL();

  // Window management commands
  void NewTab();
  void CloseTab();
  void CloseApp();
  void NewWindow();
  void NewIncognitoWindow();
  void CloseWindow();
  void SelectNextTab();
  void SelectPreviousTab();
  void SelectNumberedTab(int index);
  void SelectLastTab();
  void DuplicateTab();
  void RestoreTab();
  void ConvertPopupToTabbedBrowser();
  void Exit();

  // Clipboard commands
  void Cut();
  void Copy();
  void CopyCurrentPageURL();
  void Paste();

  // Opens the FindInPage window for the currently open tab.
  void Find();
  void FindNext();
  void FindPrevious();

  // Zoom
  void ZoomIn();
  void ZoomOut();
  void ZoomReset();

  // Sets focus to various bits of UI.
  void FocusLocationBar();
  void FocusSearch();
  void FocusToolbar();

  // Page-related commands.
  void BookmarkCurrentPage();
  void ViewSource();
  void ClosePopups();
  void Print();
  void SavePage();
  void ToggleEncodingAutoDetect();
  void OverrideEncoding(int encoding_id);

  // Show various bits of UI.
  void OpenKeywordEditor();
  void OpenClearBrowsingDataDialog();
  void OpenImportSettingsDialog();
  void OpenBugReportDialog();
  void OpenDebuggerWindow();
  void OpenJavaScriptConsole();
  void OpenCreateShortcutsDialog();
  void OpenPasswordManager();
  void OpenAboutChromeDialog();
  void OpenFile();
  void OpenTaskManager();
  void OpenOptionsDialog();
  void OpenHelpTab();
  void ShowHistoryTab();
  void ShowDownloadsTab();
  void OpenBookmarksManager();
  void ToggleBookmarksBar();

  /////////////////////////////////////////////////////////////////////////////

  static void RegisterPrefs(PrefService* prefs);
  static void RegisterUserPrefs(PrefService* prefs);

  // Returns the Browser which contains the tab with the given
  // NavigationController, also filling in |index| (if valid) with the tab's
  // index in the tab strip.
  // Returns NULL if not found.
  // This call is O(N) in the number of tabs.
  static Browser* GetBrowserForController(
      const NavigationController* controller, int* index);

  // Interface implementations ////////////////////////////////////////////////

  // Overridden from CommandHandler:
  virtual bool GetContextualLabel(int id, std::wstring* out) const {
    return false;
  }
  virtual void ExecuteCommand(int id);

  // Overridden from TabStripModelDelegate:
  virtual void CreateNewStripWithContents(TabContents* detached_contents,
                                          const gfx::Point& drop_point);
  virtual int GetDragActions() const;
  // Construct a TabContents for a given URL, profile and transition type.
  // If instance is not null, its process will be used to render the tab.
  // TODO(beng): remove this from TabStripDelegate, it's only used by
  //             TabStripModel::AddBlankTab*, which should really live here
  //             on Browser.
  virtual TabContents* CreateTabContentsForURL(
      const GURL& url,
      const GURL& referrer,
      Profile* profile,
      PageTransition::Type transition,
      bool defer_load,
      SiteInstance* instance) const;
  virtual bool CanDuplicateContentsAt(int index);
  virtual void DuplicateContentsAt(int index);
  virtual void ValidateLoadingAnimations();
  virtual void CloseFrameAfterDragSession();

  // Overridden from TabStripModelObserver:
  virtual void TabInsertedAt(TabContents* contents,
                             int index,
                             bool foreground);
  virtual void TabClosingAt(TabContents* contents, int index);
  virtual void TabDetachedAt(TabContents* contents, int index);
  virtual void TabSelectedAt(TabContents* old_contents,
                             TabContents* new_contents,
                             int index,
                             bool user_gesture);
  virtual void TabMoved(TabContents* contents,
                        int from_index,
                        int to_index);
  virtual void TabStripEmpty();

  // Overridden from TabContentsDelegate:
  virtual void OpenURLFromTab(TabContents* source,
                             const GURL& url, const GURL& referrer,
                             WindowOpenDisposition disposition,
                             PageTransition::Type transition);
  virtual void NavigationStateChanged(const TabContents* source,
                                      unsigned changed_flags);
  virtual void ReplaceContents(TabContents* source, TabContents* new_contents);
  virtual void AddNewContents(TabContents* source,
                              TabContents* new_contents,
                              WindowOpenDisposition disposition,
                              const gfx::Rect& initial_pos,
                              bool user_gesture);
  virtual void ActivateContents(TabContents* contents);
  virtual void LoadingStateChanged(TabContents* source);
  virtual void CloseContents(TabContents* source);
  virtual void MoveContents(TabContents* source, const gfx::Rect& pos);
  virtual bool IsPopup(TabContents* source);
  virtual void ToolbarSizeChanged(TabContents* source, bool is_animating);
  virtual void URLStarredChanged(TabContents* source, bool starred);

  virtual void ContentsMouseEvent(TabContents* source, UINT message);
  virtual void UpdateTargetURL(TabContents* source, const GURL& url);

  virtual void ContentsZoomChange(bool zoom_in);
  virtual bool IsApplication() const;
  virtual void ConvertContentsToApplication(TabContents* source);
  virtual void ContentsStateChanged(TabContents* source);
  virtual bool ShouldDisplayURLField();
  virtual void BeforeUnloadFired(TabContents* source,
                                 bool proceed, 
                                 bool* proceed_to_fire_unload);
  virtual void ShowHtmlDialog(HtmlDialogContentsDelegate* delegate,
                              HWND parent_hwnd);

  // Overridden from SelectFileDialog::Listener:
  virtual void FileSelected(const std::wstring& path, void* params);

  // Overridden from NotificationObserver:
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details);

 private:
  // Command and state updating ///////////////////////////////////////////////

  // Initialize state for all browser commands.
  void InitCommandState();

  // Update commands that drive the NavigationController to reflect changes in
  // the NavigationController's state (Back, Forward, etc).
  void UpdateNavigationCommands();

  // Change the "starred" button display to starred/unstarred.
  // TODO(evanm): migrate this to the commands framework.
  void SetStarredButtonToggled(bool starred);

  // UI update coalescing and handling ////////////////////////////////////////

  // Asks the toolbar (and as such the location bar) to update its state to
  // reflect the current tab's current URL, security state, etc.
  // If |should_restore_state| is true, we're switching (back?) to this tab and
  // should restore any previous location bar state (such as user editing) as
  // well.
  void UpdateToolbar(bool should_restore_state);

  // Adds an update to the update queue and schedules an update if necessary.
  // These are subsequently processed by ProcessPendingUIUpdates.
  // |changed_flags| is a bitfield of TabContents::INVALIDATE_* values.
  void ScheduleUIUpdate(const TabContents* source, unsigned changed_flags);

  // Processes all pending updates to the UI that have been queued by
  // ScheduleUIUpdate in scheduled_updates_.
  void ProcessPendingUIUpdates();

  // Removes all entries from scheduled_updates_ whose source is contents.
  void RemoveScheduledUpdatesFor(TabContents* contents);

  // Getters for UI ///////////////////////////////////////////////////////////

  // TODO(beng): remove, and provide AutomationProvider a better way to access
  //             the LocationBarView's edit.
  friend class AutomationProvider;

  // Getters for the location bar and go button.
  LocationBarView* GetLocationBarView() const;
  GoButton* GetGoButton();

  // Returns the StatusBubble from the current toolbar. It is possible for
  // this to return NULL if called before the toolbar has initialized.
  // TODO(beng): remove this.
  StatusBubble* GetStatusBubble();

  // Session restore functions ////////////////////////////////////////////////

  // Notifies the history database of the index for all tabs whose index is
  // >= index.
  void SyncHistoryWithTabs(int index);

  // Called from AddRestoredTab and ReplaceRestoredTab to build a
  // NavigationController from an incoming vector of TabNavigations.
  // Caller takes ownership of the returned NavigationController.
  NavigationController* BuildRestoredNavigationController(
      const std::vector<TabNavigation>& navigations,
      int selected_navigation);

  // OnBeforeUnload handling //////////////////////////////////////////////////

  typedef std::vector<TabContents*> UnloadListenerVector;

  // Processes the next tab that needs it's beforeunload/unload event fired.
  void ProcessPendingTabs();

  // Whether we've completed firing all the tabs' beforeunload/unload events.
  bool HasCompletedUnloadProcessing();

  // Clears all the state associated with processing tabs' beforeunload/unload
  // events since the user cancelled closing the window.
  void CancelWindowClose();

  // Removes the tab from the associated vector. Returns whether the tab
  // was in the vector in the first place.
  // TODO(beng): this method needs a better name!
  bool RemoveFromVector(UnloadListenerVector* vector, TabContents* tab);

  // Cleans up state appropriately when we are trying to close the browser and
  // the tab has finished firing it's unload handler. We also use this in the 
  // cases where a tab crashes or hangs even if the beforeunload/unload haven't
  // successfully fired.
  void ClearUnloadState(TabContents* tab);

  // Assorted utility functions ///////////////////////////////////////////////

  // Retrieve the last active tabbed browser with the same profile as the
  // receiving Browser. Creates a new Browser if none are available.
  Browser* GetOrCreateTabbedBrowser();

  // Creates a new popup window with its own Browser object with the
  // incoming sizing information. |initial_pos|'s origin() is the
  // window origin, and its size() is the size of the content area.
  void BuildPopupWindow(TabContents* source,
                        TabContents* new_contents,
                        const gfx::Rect& initial_pos);

  // Returns what the user's home page is, or the new tab page if the home page
  // has not been set.
  GURL GetHomePage();

  // Advance the find selection by one. Direction is either forward or
  // backwards depending on parameter passed in.
  void AdvanceFindSelection(bool forward_direction);

  // Closes the frame.
  // TODO(beng): figure out if we need this now that the frame itself closes
  //             after a return to the message loop.
  void CloseFrame();

  // Compute a deterministic name based on the URL. We use this pseudo name
  // as a key to store window location per application URLs.
  static std::wstring ComputeApplicationNameFromURL(const GURL& url);

  // Create a preference dictionary for the provided application name. This is
  // done only once per application name / per session.
  static void RegisterAppPrefs(const std::wstring& app_name);

  // Data members /////////////////////////////////////////////////////////////

  // This Browser's type.
  BrowserType::Type type_;

  // This Browser's profile.
  Profile* profile_;

  // This Browser's window.
  BrowserWindow* window_;

  // Controls how the window will appear when Show() is called. This is one
  // of the SW_* constants passed to ShowWindow, and will be initialized in the
  // constructor.
  //
  // After the first call to Show() succeeds, this is set to -1, indicating that
  // subsequent calls to Show() should be ignored.
  // TODO(beng): This should be removed (http://crbug.com/3557) and put into
  //             BrowserView, or some more likely place.
  int initial_show_command_;

  // This Browser's TabStripModel.
  TabStripModel tabstrip_model_;

  // The Controller that updates all browser commands.
  CommandController controller_;

  // An optional application name which is used to retrieve and save window
  // positions.
  std::wstring app_name_;

  // Unique identifier of this browser for session restore. This id is only
  // unique within the current session, and is not guaranteed to be unique
  // across sessions.
  SessionID session_id_;

  // TODO(beng): should be combined with ToolbarModel now that this is the only
  //             implementation.
  class BrowserToolbarModel : public ToolbarModel {
  public:
    explicit BrowserToolbarModel(Browser* browser) : browser_(browser) { }
    virtual ~BrowserToolbarModel() { }

    // ToolbarModel implementation.
    virtual NavigationController* GetNavigationController() {
      return browser_->GetSelectedNavigationController();
    }

  private:
    Browser* browser_;

    DISALLOW_EVIL_CONSTRUCTORS(BrowserToolbarModel);
  };

  // The model for the toolbar view.
  BrowserToolbarModel toolbar_model_;

  // UI update coalescing and handling ////////////////////////////////////////

  // Tracks invalidates to the UI, see the declaration in the .cc file.
  struct UIUpdate;
  typedef std::vector<UIUpdate> UpdateVector;

  // Lists all UI updates that are pending. We don't update things like the
  // URL or tab title right away to avoid flickering and extra painting.
  // See ScheduleUIUpdate and ProcessPendingUIUpdates.
  UpdateVector scheduled_updates_;

  // The following factory is used for chrome update coalescing.
  ScopedRunnableMethodFactory<Browser> chrome_updater_factory_;

  // OnBeforeUnload handling //////////////////////////////////////////////////

  // Tracks tabs that need there beforeunload event fired before we can
  // close the browser. Only gets populated when we try to close the browser.
  UnloadListenerVector tabs_needing_before_unload_fired_;

  // Tracks tabs that need there unload event fired before we can
  // close the browser. Only gets populated when we try to close the browser.
  UnloadListenerVector tabs_needing_unload_fired_;

  // Whether we are processing the beforeunload and unload events of each tab
  // in preparation for closing the browser.
  bool is_attempting_to_close_browser_;

  /////////////////////////////////////////////////////////////////////////////

  // The following factory is used to close the frame at a later time.
  ScopedRunnableMethodFactory<Browser> method_factory_;

  // Debugger Window, created lazily
  scoped_refptr<DebuggerWindow> debugger_window_;

  // Dialog box used for opening and saving files.
  scoped_refptr<SelectFileDialog> select_file_dialog_;

  // The browser idle task helps cleanup unused memory resources when idle.
  scoped_ptr<BrowserIdleTimer> idle_task_;

  // Keep track of the encoding auto detect pref.
  BooleanPrefMember encoding_auto_detect_;

  DISALLOW_COPY_AND_ASSIGN(Browser);
};

#endif  // CHROME_BROWSER_BROWSER_H_