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
|
// Copyright (c) 2012 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.
#include "chrome/browser/ui/browser_instant_controller.h"
#include "chrome/browser/browser_shutdown.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/instant/instant_controller.h"
#include "chrome/browser/instant/instant_unload_handler.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/omnibox/location_bar.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/tab_contents/tab_contents.h"
#include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/web_contents.h"
namespace chrome {
////////////////////////////////////////////////////////////////////////////////
// BrowserInstantController, public:
BrowserInstantController::BrowserInstantController(Browser* browser)
: browser_(browser) {
profile_pref_registrar_.Init(browser_->profile()->GetPrefs());
profile_pref_registrar_.Add(prefs::kInstantEnabled, this);
CreateInstantIfNecessary();
browser_->tab_strip_model()->AddObserver(this);
}
BrowserInstantController::~BrowserInstantController() {
browser_->tab_strip_model()->RemoveObserver(this);
}
bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) {
if (!instant() || !instant()->PrepareForCommit() ||
disposition == NEW_BACKGROUND_TAB) {
// NEW_BACKGROUND_TAB results in leaving the omnibox open, so we don't
// attempt to use the instant preview.
return false;
}
if (disposition == CURRENT_TAB) {
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_INSTANT_COMMITTED,
content::Source<TabContents>(instant()->CommitCurrentPreview(
INSTANT_COMMIT_PRESSED_ENTER)),
content::NotificationService::NoDetails());
return true;
}
if (disposition == NEW_FOREGROUND_TAB) {
TabContents* preview_contents = instant()->ReleasePreviewContents(
INSTANT_COMMIT_PRESSED_ENTER, NULL);
// HideInstant is invoked after release so that InstantController is not
// active when HideInstant asks it for its state.
HideInstant();
preview_contents->web_contents()->GetController().PruneAllButActive();
browser_->tab_strip_model()->AddTabContents(
preview_contents,
-1,
instant()->last_transition_type(),
TabStripModel::ADD_ACTIVE);
instant()->CompleteRelease(preview_contents);
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_INSTANT_COMMITTED,
content::Source<TabContents>(preview_contents),
content::NotificationService::NoDetails());
return true;
}
// The omnibox currently doesn't use other dispositions, so we don't attempt
// to handle them. If you hit this NOTREACHED file a bug and I'll (sky) add
// support for the new disposition.
NOTREACHED();
return false;
}
////////////////////////////////////////////////////////////////////////////////
// BrowserInstantController, InstantControllerDelegate implementation:
void BrowserInstantController::ShowInstant(TabContents* preview_contents) {
browser_->window()->ShowInstant(preview_contents);
// TODO(beng): investigate if we can avoid this and instead rely on the
// visibility of the WebContentsView
chrome::GetActiveWebContents(browser_)->WasHidden();
preview_contents->web_contents()->WasRestored();
}
void BrowserInstantController::HideInstant() {
browser_->window()->HideInstant();
if (chrome::GetActiveWebContents(browser_))
chrome::GetActiveWebContents(browser_)->WasRestored();
if (instant_->GetPreviewContents())
instant_->GetPreviewContents()->web_contents()->WasHidden();
}
void BrowserInstantController::CommitInstant(TabContents* preview_contents) {
TabContents* tab_contents = chrome::GetActiveTabContents(browser_);
int index = browser_->tab_strip_model()->GetIndexOfTabContents(tab_contents);
DCHECK_NE(TabStripModel::kNoTab, index);
// TabStripModel takes ownership of preview_contents.
browser_->tab_strip_model()->ReplaceTabContentsAt(index, preview_contents);
// InstantUnloadHandler takes ownership of tab_contents.
instant_unload_handler_->RunUnloadListenersOrDestroy(tab_contents, index);
GURL url = preview_contents->web_contents()->GetURL();
DCHECK(browser_->profile()->GetExtensionService());
if (browser_->profile()->GetExtensionService()->IsInstalledApp(url)) {
AppLauncherHandler::RecordAppLaunchType(
extension_misc::APP_LAUNCH_OMNIBOX_INSTANT);
}
}
void BrowserInstantController::SetSuggestedText(
const string16& text,
InstantCompleteBehavior behavior) {
if (browser_->window()->GetLocationBar())
browser_->window()->GetLocationBar()->SetSuggestedText(text, behavior);
}
gfx::Rect BrowserInstantController::GetInstantBounds() {
return browser_->window()->GetInstantBounds();
}
void BrowserInstantController::InstantPreviewFocused() {
// NOTE: This is only invoked on aura.
browser_->window()->WebContentsFocused(
instant_->GetPreviewContents()->web_contents());
}
TabContents* BrowserInstantController::GetInstantHostTabContents() const {
return chrome::GetActiveTabContents(browser_);
}
////////////////////////////////////////////////////////////////////////////////
// BrowserInstantController, content::NotificationObserver implementation:
void BrowserInstantController::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK(type == chrome::NOTIFICATION_PREF_CHANGED);
const std::string& pref_name =
*content::Details<std::string>(details).ptr();
DCHECK(pref_name == prefs::kInstantEnabled);
if (browser_shutdown::ShuttingDownWithoutClosingBrowsers() ||
!InstantController::IsEnabled(browser_->profile())) {
if (instant()) {
instant()->DestroyPreviewContents();
instant_.reset();
instant_unload_handler_.reset();
}
} else {
CreateInstantIfNecessary();
}
}
////////////////////////////////////////////////////////////////////////////////
// BrowserInstantController, TabStripModelObserver implementation:
void BrowserInstantController::TabDeactivated(TabContents* contents) {
if (instant())
instant()->Hide();
}
////////////////////////////////////////////////////////////////////////////////
// BrowserInstantController, private:
void BrowserInstantController::CreateInstantIfNecessary() {
if (browser_->is_type_tabbed() &&
InstantController::IsEnabled(browser_->profile()) &&
!browser_->profile()->IsOffTheRecord()) {
instant_.reset(new InstantController(this, InstantController::INSTANT));
instant_unload_handler_.reset(new InstantUnloadHandler(browser_));
}
}
} // namespace chrome
|