browser_instant_controller.h revision ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16
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_INSTANT_CONTROLLER_H_
6#define CHROME_BROWSER_UI_BROWSER_INSTANT_CONTROLLER_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/compiler_specific.h"
12#include "base/prefs/pref_change_registrar.h"
13#include "chrome/browser/ui/search/instant_controller.h"
14#include "chrome/browser/ui/search/instant_unload_handler.h"
15#include "chrome/browser/ui/search/search_model_observer.h"
16#include "ui/base/window_open_disposition.h"
17
18class Browser;
19struct InstantSuggestion;
20class Profile;
21
22namespace content {
23class WebContents;
24}
25
26namespace gfx {
27class Rect;
28}
29
30class BrowserInstantController : public SearchModelObserver {
31 public:
32  explicit BrowserInstantController(Browser* browser);
33  virtual ~BrowserInstantController();
34
35  // If |url| is the new tab page URL, set |target_contents| to the preloaded
36  // NTP contents from InstantController. If |source_contents| is not NULL, we
37  // replace it with the new |target_contents| in the tabstrip and delete
38  // |source_contents|. Otherwise, the caller owns |target_contents| and is
39  // responsible for inserting it into the tabstrip.
40  //
41  // Returns true if and only if we update |target_contents|.
42  bool MaybeSwapInInstantNTPContents(
43      const GURL& url,
44      content::WebContents* source_contents,
45      content::WebContents** target_contents);
46
47  // Commits the current Instant, returning true on success. This is intended
48  // for use from OpenCurrentURL.
49  bool OpenInstant(WindowOpenDisposition disposition, const GURL& url);
50
51  // Returns the Profile associated with the Browser that owns this object.
52  Profile* profile() const;
53
54  // Returns the InstantController or NULL if there is no InstantController for
55  // this BrowserInstantController.
56  InstantController* instant() { return &instant_; }
57
58  // Invoked by |instant_| to change the omnibox focus.
59  void FocusOmnibox(OmniboxFocusState state);
60
61  // Invoked by |instant_| to get the currently active tab.
62  content::WebContents* GetActiveWebContents() const;
63
64  // Invoked by |browser_| when the active tab changes.
65  void ActiveTabChanged();
66
67  // Invoked by |browser_| when the active tab is about to be deactivated.
68  void TabDeactivated(content::WebContents* contents);
69
70  // Invoked by the InstantController when it wants to open a URL.
71  void OpenURL(const GURL& url,
72               content::PageTransition transition,
73               WindowOpenDisposition disposition);
74
75  // Invoked by |instant_| to paste the |text| (or clipboard content if text is
76  // empty) into the omnibox. It will set focus to the omnibox if the omnibox is
77  // not focused.
78  void PasteIntoOmnibox(const string16& text);
79
80  // Sets the stored omnibox bounds.
81  void SetOmniboxBounds(const gfx::Rect& bounds);
82
83  // Notifies |instant_| to toggle voice search.
84  void ToggleVoiceSearch();
85
86 private:
87  // Overridden from search::SearchModelObserver:
88  virtual void ModelChanged(const SearchModel::State& old_state,
89                            const SearchModel::State& new_state) OVERRIDE;
90
91  // Called when the default search provider changes. Revokes the searchbox API
92  // privileges for any existing WebContents (that belong to the erstwhile
93  // default search provider) by simply reloading all such WebContents. This
94  // ensures that they are reloaded in a non-privileged renderer process.
95  void OnDefaultSearchProviderChanged(const std::string& pref_name);
96
97  // Replaces the contents at tab |index| with |new_contents| and deletes the
98  // existing contents.
99  void ReplaceWebContentsAt(int index,
100                            scoped_ptr<content::WebContents> new_contents);
101
102  Browser* const browser_;
103
104  InstantController instant_;
105  InstantUnloadHandler instant_unload_handler_;
106
107  PrefChangeRegistrar profile_pref_registrar_;
108
109  DISALLOW_COPY_AND_ASSIGN(BrowserInstantController);
110};
111
112#endif  // CHROME_BROWSER_UI_BROWSER_INSTANT_CONTROLLER_H_
113