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
|
// Copyright 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/instant/instant_loader.h"
#include "chrome/browser/content_settings/tab_specific_content_settings.h"
#include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h"
#include "chrome/browser/favicon/favicon_tab_helper.h"
#include "chrome/browser/history/history_tab_helper.h"
#include "chrome/browser/instant/instant_controller.h"
#include "chrome/browser/safe_browsing/safe_browsing_tab_observer.h"
#include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h"
#include "chrome/browser/ui/constrained_window_tab_helper.h"
#include "chrome/browser/ui/constrained_window_tab_helper_delegate.h"
#include "chrome/browser/ui/search/search_tab_helper.h"
#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
#include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents_delegate.h"
#include "ipc/ipc_message.h"
namespace {
int kUserDataKey;
class InstantLoaderUserData : public base::SupportsUserData::Data {
public:
explicit InstantLoaderUserData(InstantLoader* loader) : loader_(loader) {}
InstantLoader* loader() const { return loader_; }
private:
~InstantLoaderUserData() {}
InstantLoader* const loader_;
DISALLOW_COPY_AND_ASSIGN(InstantLoaderUserData);
};
}
// WebContentsDelegateImpl -----------------------------------------------------
class InstantLoader::WebContentsDelegateImpl
: public ConstrainedWindowTabHelperDelegate,
public CoreTabHelperDelegate,
public content::WebContentsDelegate {
public:
explicit WebContentsDelegateImpl(InstantLoader* loader);
private:
// Overridden from ConstrainedWindowTabHelperDelegate:
virtual bool ShouldFocusConstrainedWindow() OVERRIDE;
// Overridden from CoreTabHelperDelegate:
virtual void SwapTabContents(content::WebContents* old_contents,
content::WebContents* new_contents) OVERRIDE;
// Overridden from content::WebContentsDelegate:
virtual bool ShouldSuppressDialogs() OVERRIDE;
virtual bool ShouldFocusPageAfterCrash() OVERRIDE;
virtual void LostCapture() OVERRIDE;
virtual void WebContentsFocused(content::WebContents* contents) OVERRIDE;
virtual bool CanDownload(content::RenderViewHost* render_view_host,
int request_id,
const std::string& request_method) OVERRIDE;
virtual void HandleMouseDown() OVERRIDE;
virtual void HandleMouseUp() OVERRIDE;
virtual void HandlePointerActivate() OVERRIDE;
virtual void HandleGestureEnd() OVERRIDE;
virtual void DragEnded() OVERRIDE;
virtual bool OnGoToEntryOffset(int offset) OVERRIDE;
virtual content::WebContents* OpenURLFromTab(
content::WebContents* source,
const content::OpenURLParams& params) OVERRIDE;
void MaybeCommitFromPointerRelease();
InstantLoader* const loader_;
DISALLOW_COPY_AND_ASSIGN(WebContentsDelegateImpl);
};
InstantLoader::WebContentsDelegateImpl::WebContentsDelegateImpl(
InstantLoader* loader)
: loader_(loader) {
}
bool InstantLoader::WebContentsDelegateImpl::ShouldFocusConstrainedWindow() {
// Return false so that constrained windows are not initially focused. If we
// did otherwise the preview would prematurely get committed when focus goes
// to the constrained window.
return false;
}
void InstantLoader::WebContentsDelegateImpl::SwapTabContents(
content::WebContents* old_contents,
content::WebContents* new_contents) {
// If this is being called, something is swapping in to loader's |contents_|
// before we've added it to the tab strip.
loader_->ReplacePreviewContents(old_contents, new_contents);
}
bool InstantLoader::WebContentsDelegateImpl::ShouldSuppressDialogs() {
// Any message shown during Instant cancels Instant, so we suppress them.
return true;
}
bool InstantLoader::WebContentsDelegateImpl::ShouldFocusPageAfterCrash() {
return false;
}
void InstantLoader::WebContentsDelegateImpl::LostCapture() {
MaybeCommitFromPointerRelease();
}
void InstantLoader::WebContentsDelegateImpl::WebContentsFocused(
content::WebContents* /* contents */) {
// The preview is getting focus. Equivalent to it being clicked.
bool tmp = loader_->is_pointer_down_from_activate_;
loader_->is_pointer_down_from_activate_ = true;
loader_->controller_->InstantLoaderContentsFocused();
loader_->is_pointer_down_from_activate_ = tmp;
}
bool InstantLoader::WebContentsDelegateImpl::CanDownload(
content::RenderViewHost* /* render_view_host */,
int /* request_id */,
const std::string& /* request_method */) {
// Downloads are disabled.
return false;
}
void InstantLoader::WebContentsDelegateImpl::HandleMouseDown() {
loader_->is_pointer_down_from_activate_ = true;
}
void InstantLoader::WebContentsDelegateImpl::HandleMouseUp() {
MaybeCommitFromPointerRelease();
}
void InstantLoader::WebContentsDelegateImpl::HandlePointerActivate() {
loader_->is_pointer_down_from_activate_ = true;
}
void InstantLoader::WebContentsDelegateImpl::HandleGestureEnd() {
MaybeCommitFromPointerRelease();
}
void InstantLoader::WebContentsDelegateImpl::DragEnded() {
// If the user drags, we won't get a mouse up (at least on Linux). Commit the
// Instant result when the drag ends, so that during the drag the page won't
// move around.
MaybeCommitFromPointerRelease();
}
bool InstantLoader::WebContentsDelegateImpl::OnGoToEntryOffset(int offset) {
return false;
}
content::WebContents* InstantLoader::WebContentsDelegateImpl::OpenURLFromTab(
content::WebContents* source,
const content::OpenURLParams& params) {
content::WebContents* preview = loader_->contents_.get();
if (loader_->controller_->CommitIfPossible(INSTANT_COMMIT_NAVIGATED))
return preview->GetDelegate()->OpenURLFromTab(source, params);
return NULL;
}
void InstantLoader::WebContentsDelegateImpl::MaybeCommitFromPointerRelease() {
if (loader_->is_pointer_down_from_activate_) {
loader_->is_pointer_down_from_activate_ = false;
loader_->controller_->CommitIfPossible(INSTANT_COMMIT_FOCUS_LOST);
}
}
// InstantLoader ---------------------------------------------------------------
// static
InstantLoader* InstantLoader::FromWebContents(
const content::WebContents* web_contents) {
InstantLoaderUserData* data = static_cast<InstantLoaderUserData*>(
web_contents->GetUserData(&kUserDataKey));
return data ? data->loader() : NULL;
}
InstantLoader::InstantLoader(InstantController* controller,
const std::string& instant_url)
: client_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
controller_(controller),
delegate_(new WebContentsDelegateImpl(
ALLOW_THIS_IN_INITIALIZER_LIST(this))),
instant_url_(instant_url),
supports_instant_(false),
is_pointer_down_from_activate_(false) {
}
InstantLoader::~InstantLoader() {
}
void InstantLoader::InitContents(const content::WebContents* active_tab) {
content::WebContents::CreateParams create_params(
active_tab->GetBrowserContext());
create_params.base_web_contents = active_tab;
contents_.reset(content::WebContents::CreateWithSessionStorage(
create_params,
active_tab->GetController().GetSessionStorageNamespaceMap()));
SetupPreviewContents();
// This HTTP header and value are set on loads that originate from Instant.
const char kInstantHeader[] = "X-Purpose: Instant";
DVLOG(1) << "LoadURL: " << instant_url_;
contents_->GetController().LoadURL(GURL(instant_url_), content::Referrer(),
content::PAGE_TRANSITION_GENERATED, kInstantHeader);
contents_->WasHidden();
}
content::WebContents* InstantLoader::ReleaseContents() {
CleanupPreviewContents();
return contents_.release();
}
void InstantLoader::DidNavigate(
const history::HistoryAddPageArgs& add_page_args) {
last_navigation_ = add_page_args;
}
void InstantLoader::Update(const string16& text,
size_t selection_start,
size_t selection_end,
bool verbatim) {
last_navigation_ = history::HistoryAddPageArgs();
client_.Update(text, selection_start, selection_end, verbatim);
}
void InstantLoader::Submit(const string16& text) {
client_.Submit(text);
}
void InstantLoader::Cancel(const string16& text) {
client_.Cancel(text);
}
void InstantLoader::SetOmniboxBounds(const gfx::Rect& bounds) {
client_.SetOmniboxBounds(bounds);
}
void InstantLoader::SendAutocompleteResults(
const std::vector<InstantAutocompleteResult>& results) {
client_.SendAutocompleteResults(results);
}
void InstantLoader::UpOrDownKeyPressed(int count) {
client_.UpOrDownKeyPressed(count);
}
void InstantLoader::SearchModeChanged(const chrome::search::Mode& mode) {
client_.SearchModeChanged(mode);
}
void InstantLoader::SendThemeBackgroundInfo(
const ThemeBackgroundInfo& theme_info) {
client_.SendThemeBackgroundInfo(theme_info);
}
void InstantLoader::SendThemeAreaHeight(int height) {
client_.SendThemeAreaHeight(height);
}
void InstantLoader::SetDisplayInstantResults(bool display_instant_results) {
client_.SetDisplayInstantResults(display_instant_results);
}
void InstantLoader::SetSuggestions(
const std::vector<InstantSuggestion>& suggestions) {
InstantSupportDetermined(true);
controller_->SetSuggestions(contents(), suggestions);
}
void InstantLoader::InstantSupportDetermined(bool supports_instant) {
// If we had already determined that the page supports Instant, nothing to do.
if (supports_instant_)
return;
supports_instant_ = supports_instant;
controller_->InstantSupportDetermined(contents(), supports_instant);
}
void InstantLoader::ShowInstantPreview(InstantShownReason reason,
int height,
InstantSizeUnits units) {
InstantSupportDetermined(true);
controller_->ShowInstantPreview(reason, height, units);
}
void InstantLoader::StartCapturingKeyStrokes() {
InstantSupportDetermined(true);
controller_->StartCapturingKeyStrokes();
}
void InstantLoader::StopCapturingKeyStrokes() {
InstantSupportDetermined(true);
// NOTE(samarth): the current implementation of the key capturing (invisible
// focus) doesn't require doing anything explicitly here.
}
void InstantLoader::RenderViewGone() {
controller_->InstantLoaderRenderViewGone();
}
void InstantLoader::AboutToNavigateMainFrame(const GURL& url) {
controller_->InstantLoaderAboutToNavigateMainFrame(url);
}
void InstantLoader::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
#if defined(OS_MACOSX)
if (type == content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED) {
if (content::RenderWidgetHostView* rwhv =
contents_->GetRenderWidgetHostView())
rwhv->SetTakesFocusOnlyOnMouseDown(true);
return;
}
NOTREACHED();
#endif
}
void InstantLoader::SetupPreviewContents() {
client_.SetContents(contents());
contents_->SetUserData(&kUserDataKey, new InstantLoaderUserData(this));
contents_->SetDelegate(delegate_.get());
// Set up various tab helpers. The rest will get attached when (if) the
// contents is added to the tab strip.
// Tab helpers to control popups.
BlockedContentTabHelper::CreateForWebContents(contents());
BlockedContentTabHelper::FromWebContents(contents())->
SetAllContentsBlocked(true);
TabSpecificContentSettings::CreateForWebContents(contents());
TabSpecificContentSettings::FromWebContents(contents())->
SetPopupsBlocked(true);
// A tab helper to control constrained windows.
ConstrainedWindowTabHelper::CreateForWebContents(contents());
ConstrainedWindowTabHelper::FromWebContents(contents())->
set_delegate(delegate_.get());
// A tab helper to catch prerender content swapping shenanigans.
CoreTabHelper::CreateForWebContents(contents());
CoreTabHelper::FromWebContents(contents())->set_delegate(delegate_.get());
// Tab helpers used when committing a preview.
chrome::search::SearchTabHelper::CreateForWebContents(contents());
HistoryTabHelper::CreateForWebContents(contents());
// Observers.
extensions::WebNavigationTabObserver::CreateForWebContents(contents());
// Favicons, required by the Task Manager.
FaviconTabHelper::CreateForWebContents(contents());
// And some flat-out paranoia.
safe_browsing::SafeBrowsingTabObserver::CreateForWebContents(contents());
#if defined(OS_MACOSX)
// If |contents_| doesn't yet have a RWHV, SetTakesFocusOnlyOnMouseDown() will
// be called later, when NOTIFICATION_RENDER_VIEW_HOST_CHANGED is received.
if (content::RenderWidgetHostView* rwhv =
contents_->GetRenderWidgetHostView())
rwhv->SetTakesFocusOnlyOnMouseDown(true);
registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
content::Source<content::NavigationController>(
&contents_->GetController()));
#endif
}
void InstantLoader::CleanupPreviewContents() {
client_.SetContents(NULL);
contents_->RemoveUserData(&kUserDataKey);
contents_->SetDelegate(NULL);
// Undo tab helper work done in SetupPreviewContents().
BlockedContentTabHelper::FromWebContents(contents())->
SetAllContentsBlocked(false);
TabSpecificContentSettings::FromWebContents(contents())->
SetPopupsBlocked(false);
ConstrainedWindowTabHelper::FromWebContents(contents())->
set_delegate(NULL);
CoreTabHelper::FromWebContents(contents())->set_delegate(NULL);
#if defined(OS_MACOSX)
if (content::RenderWidgetHostView* rwhv =
contents_->GetRenderWidgetHostView())
rwhv->SetTakesFocusOnlyOnMouseDown(false);
registrar_.Remove(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
content::Source<content::NavigationController>(
&contents_->GetController()));
#endif
}
void InstantLoader::ReplacePreviewContents(content::WebContents* old_contents,
content::WebContents* new_contents) {
DCHECK_EQ(old_contents, contents());
CleanupPreviewContents();
// We release here without deleting so that the caller still has the
// responsibility for deleting the WebContents.
ignore_result(contents_.release());
contents_.reset(new_contents);
SetupPreviewContents();
controller_->SwappedWebContents();
}
|