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
|
// 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/instant/instant_loader.h"
#include "chrome/browser/content_settings/tab_specific_content_settings.h"
#include "chrome/browser/history/history_types.h"
#include "chrome/browser/instant/instant_loader_delegate.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/tab_contents/thumbnail_generator.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/tab_contents/core_tab_helper.h"
#include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h"
#include "chrome/browser/ui/tab_contents/tab_contents.h"
#include "chrome/common/render_messages.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
// WebContentsDelegateImpl -----------------------------------------------------
class InstantLoader::WebContentsDelegateImpl
: public ConstrainedWindowTabHelperDelegate,
public CoreTabHelperDelegate,
public content::WebContentsDelegate,
public content::WebContentsObserver {
public:
explicit WebContentsDelegateImpl(InstantLoader* loader);
bool is_pointer_down_from_activate() const {
return is_pointer_down_from_activate_;
}
// ConstrainedWindowTabHelperDelegate:
virtual bool ShouldFocusConstrainedWindow() OVERRIDE;
// CoreTabHelperDelegate:
virtual void SwapTabContents(content::WebContents* old_contents,
content::WebContents* new_contents) OVERRIDE;
// 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 HandleMouseUp() OVERRIDE;
virtual void HandlePointerActivate() OVERRIDE;
virtual void HandleGestureBegin() OVERRIDE;
virtual void HandleGestureEnd() OVERRIDE;
virtual void DragEnded() OVERRIDE;
virtual bool OnGoToEntryOffset(int offset) OVERRIDE;
virtual bool ShouldAddNavigationToHistory(
const history::HistoryAddPageArgs& add_page_args,
content::NavigationType navigation_type) OVERRIDE;
// content::WebContentsObserver:
virtual void DidFinishLoad(
int64 frame_id,
const GURL& validated_url,
bool is_main_frame,
content::RenderViewHost* render_view_host) OVERRIDE;
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
private:
// Message from renderer indicating the page has suggestions.
void OnSetSuggestions(int page_id,
const std::vector<InstantSuggestion>& suggestions);
// Message from the renderer determining whether it supports the Instant API.
void OnInstantSupportDetermined(int page_id, bool result);
// Message from the renderer requesting the preview be resized.
void OnSetInstantPreviewHeight(int page_id,
int height,
InstantSizeUnits units);
void CommitFromPointerReleaseIfNecessary();
void MaybeSetAndNotifyInstantSupportDetermined(bool supports_instant);
InstantLoader* const loader_;
// True if the mouse or a touch pointer is down from an activate.
bool is_pointer_down_from_activate_;
DISALLOW_COPY_AND_ASSIGN(WebContentsDelegateImpl);
};
InstantLoader::WebContentsDelegateImpl::WebContentsDelegateImpl(
InstantLoader* loader)
: content::WebContentsObserver(loader->preview_contents_->web_contents()),
loader_(loader),
is_pointer_down_from_activate_(false) {
}
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 our
// |preview_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() {
CommitFromPointerReleaseIfNecessary();
}
void InstantLoader::WebContentsDelegateImpl::WebContentsFocused(
content::WebContents* contents) {
loader_->loader_delegate_->InstantLoaderContentsFocused(loader_);
}
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::HandleMouseUp() {
CommitFromPointerReleaseIfNecessary();
}
void InstantLoader::WebContentsDelegateImpl::HandlePointerActivate() {
is_pointer_down_from_activate_ = true;
}
void InstantLoader::WebContentsDelegateImpl::HandleGestureBegin() {
}
void InstantLoader::WebContentsDelegateImpl::HandleGestureEnd() {
CommitFromPointerReleaseIfNecessary();
}
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.
CommitFromPointerReleaseIfNecessary();
}
bool InstantLoader::WebContentsDelegateImpl::OnGoToEntryOffset(int offset) {
return false;
}
bool InstantLoader::WebContentsDelegateImpl::ShouldAddNavigationToHistory(
const history::HistoryAddPageArgs& add_page_args,
content::NavigationType navigation_type) {
loader_->last_navigation_ = add_page_args.Clone();
return false;
}
void InstantLoader::WebContentsDelegateImpl::DidFinishLoad(
int64 frame_id,
const GURL& validated_url,
bool is_main_frame,
content::RenderViewHost* render_view_host) {
if (is_main_frame) {
if (!loader_->supports_instant_)
Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id()));
loader_->loader_delegate_->InstantLoaderPreviewLoaded(loader_);
}
}
bool InstantLoader::WebContentsDelegateImpl::OnMessageReceived(
const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(WebContentsDelegateImpl, message)
IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions)
IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined,
OnInstantSupportDetermined)
IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetInstantPreviewHeight,
OnSetInstantPreviewHeight);
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void InstantLoader::WebContentsDelegateImpl::OnSetSuggestions(
int page_id,
const std::vector<InstantSuggestion>& suggestions) {
DCHECK(loader_->preview_contents() &&
loader_->preview_contents_->web_contents());
// TODO(sreeram): Remove this 'if' bandaid once bug 141875 is confirmed fixed.
if (!loader_->preview_contents() ||
!loader_->preview_contents_->web_contents())
return;
content::NavigationEntry* entry = loader_->preview_contents_->web_contents()->
GetController().GetActiveEntry();
if (entry && page_id == entry->GetPageID()) {
MaybeSetAndNotifyInstantSupportDetermined(true);
loader_->loader_delegate_->SetSuggestions(loader_, suggestions);
}
}
void InstantLoader::WebContentsDelegateImpl::OnInstantSupportDetermined(
int page_id,
bool result) {
DCHECK(loader_->preview_contents() &&
loader_->preview_contents_->web_contents());
// TODO(sreeram): Remove this 'if' bandaid once bug 141875 is confirmed fixed.
if (!loader_->preview_contents() ||
!loader_->preview_contents_->web_contents())
return;
content::NavigationEntry* entry = loader_->preview_contents_->web_contents()->
GetController().GetActiveEntry();
if (entry && page_id == entry->GetPageID())
MaybeSetAndNotifyInstantSupportDetermined(result);
}
void InstantLoader::WebContentsDelegateImpl::OnSetInstantPreviewHeight(
int page_id,
int height,
InstantSizeUnits units) {
DCHECK(loader_->preview_contents() &&
loader_->preview_contents_->web_contents());
// TODO(sreeram): Remove this 'if' bandaid once bug 141875 is confirmed fixed.
if (!loader_->preview_contents() ||
!loader_->preview_contents_->web_contents())
return;
content::NavigationEntry* entry = loader_->preview_contents_->web_contents()->
GetController().GetActiveEntry();
if (entry && page_id == entry->GetPageID()) {
MaybeSetAndNotifyInstantSupportDetermined(true);
loader_->loader_delegate_->SetInstantPreviewHeight(loader_, height, units);
}
}
void InstantLoader::WebContentsDelegateImpl
::CommitFromPointerReleaseIfNecessary() {
if (is_pointer_down_from_activate_) {
is_pointer_down_from_activate_ = false;
loader_->loader_delegate_->CommitInstantLoader(loader_);
}
}
void InstantLoader::WebContentsDelegateImpl
::MaybeSetAndNotifyInstantSupportDetermined(bool supports_instant) {
// If we already determined that the loader supports Instant, nothing to do.
if (loader_->supports_instant_)
return;
// If the page doesn't support the Instant API, InstantController schedules
// the loader for destruction. Stop sending the controller any more messages,
// by severing the connection from the WebContents to us (its delegate).
if (!supports_instant) {
loader_->preview_contents_->web_contents()->SetDelegate(NULL);
Observe(NULL);
}
loader_->supports_instant_ = supports_instant;
loader_->loader_delegate_->InstantSupportDetermined(loader_,
supports_instant);
}
// InstantLoader ---------------------------------------------------------------
InstantLoader::InstantLoader(InstantLoaderDelegate* delegate,
const std::string& instant_url,
const TabContents* tab_contents)
: loader_delegate_(delegate),
preview_contents_(
TabContents::Factory::CreateTabContents(
content::WebContents::CreateWithSessionStorage(
tab_contents->profile(), NULL, MSG_ROUTING_NONE,
tab_contents->web_contents(),
tab_contents->web_contents()->GetController().
GetSessionStorageNamespaceMap()))),
supports_instant_(false),
instant_url_(instant_url) {
}
InstantLoader::~InstantLoader() {
if (preview_contents())
preview_contents_->web_contents()->SetDelegate(NULL);
}
void InstantLoader::Init() {
SetupPreviewContents();
// This HTTP header and value are set on loads that originate from instant.
const char* const kInstantHeader = "X-Purpose: Instant";
preview_contents_->web_contents()->GetController().LoadURL(GURL(instant_url_),
content::Referrer(), content::PAGE_TRANSITION_GENERATED, kInstantHeader);
preview_contents_->web_contents()->WasHidden();
}
void InstantLoader::Update(const string16& user_text, bool verbatim) {
// TODO: Support real cursor position.
last_navigation_ = NULL;
content::RenderViewHost* rvh =
preview_contents_->web_contents()->GetRenderViewHost();
rvh->Send(new ChromeViewMsg_SearchBoxChange(rvh->GetRoutingID(), user_text,
verbatim, user_text.size(), user_text.size()));
}
void InstantLoader::SetOmniboxBounds(const gfx::Rect& bounds) {
content::RenderViewHost* rvh =
preview_contents_->web_contents()->GetRenderViewHost();
rvh->Send(new ChromeViewMsg_SearchBoxResize(rvh->GetRoutingID(), bounds));
}
void InstantLoader::SendAutocompleteResults(
const std::vector<InstantAutocompleteResult>& results) {
content::RenderViewHost* rvh =
preview_contents_->web_contents()->GetRenderViewHost();
rvh->Send(new ChromeViewMsg_SearchBoxAutocompleteResults(rvh->GetRoutingID(),
results));
}
void InstantLoader::OnUpOrDownKeyPressed(int count) {
content::RenderViewHost* rvh =
preview_contents_->web_contents()->GetRenderViewHost();
rvh->Send(new ChromeViewMsg_SearchBoxUpOrDownKeyPressed(rvh->GetRoutingID(),
count));
}
TabContents* InstantLoader::ReleasePreviewContents(InstantCommitType type,
const string16& text) {
content::RenderViewHost* rvh =
preview_contents_->web_contents()->GetRenderViewHost();
if (type == INSTANT_COMMIT_PRESSED_ENTER)
rvh->Send(new ChromeViewMsg_SearchBoxSubmit(rvh->GetRoutingID(), text));
else
rvh->Send(new ChromeViewMsg_SearchBoxCancel(rvh->GetRoutingID(), text));
CleanupPreviewContents();
return preview_contents_.release();
}
bool InstantLoader::IsPointerDownFromActivate() const {
return preview_delegate_->is_pointer_down_from_activate();
}
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 =
preview_contents_->web_contents()->GetRenderWidgetHostView())
rwhv->SetTakesFocusOnlyOnMouseDown(true);
return;
}
NOTREACHED();
#endif
}
void InstantLoader::SetupPreviewContents() {
content::WebContents* new_contents = preview_contents_->web_contents();
preview_delegate_.reset(new WebContentsDelegateImpl(this));
WebContentsDelegateImpl* new_delegate = preview_delegate_.get();
new_contents->SetDelegate(new_delegate);
// Disable popups and such (mainly to avoid losing focus and reverting the
// preview prematurely).
BlockedContentTabHelper::FromWebContents(new_contents)->
SetAllContentsBlocked(true);
preview_contents_->constrained_window_tab_helper()->
set_delegate(new_delegate);
preview_contents_->content_settings()->SetPopupsBlocked(true);
CoreTabHelper::FromWebContents(new_contents)->set_delegate(new_delegate);
if (ThumbnailGenerator* tg = preview_contents_->thumbnail_generator())
tg->set_enabled(false);
#if defined(OS_MACOSX)
// If |preview_contents_| does not currently have a RWHV, we will call
// SetTakesFocusOnlyOnMouseDown() as a result of the RENDER_VIEW_HOST_CHANGED
// notification.
if (content::RenderWidgetHostView* rwhv =
new_contents->GetRenderWidgetHostView())
rwhv->SetTakesFocusOnlyOnMouseDown(true);
registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
content::Source<content::NavigationController>(
&new_contents->GetController()));
#endif
}
void InstantLoader::CleanupPreviewContents() {
content::WebContents* old_contents = preview_contents_->web_contents();
old_contents->SetDelegate(NULL);
preview_delegate_.reset();
BlockedContentTabHelper::FromWebContents(old_contents)->
SetAllContentsBlocked(false);
preview_contents_->constrained_window_tab_helper()->set_delegate(NULL);
preview_contents_->content_settings()->SetPopupsBlocked(false);
CoreTabHelper::FromWebContents(old_contents)->set_delegate(NULL);
if (ThumbnailGenerator* tg = preview_contents_->thumbnail_generator())
tg->set_enabled(true);
#if defined(OS_MACOSX)
if (content::RenderWidgetHostView* rwhv =
old_contents->GetRenderWidgetHostView())
rwhv->SetTakesFocusOnlyOnMouseDown(false);
registrar_.Remove(this, content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED,
content::Source<content::NavigationController>(
&old_contents->GetController()));
#endif
}
void InstantLoader::ReplacePreviewContents(content::WebContents* old_contents,
content::WebContents* new_contents) {
DCHECK(old_contents == preview_contents_->web_contents());
CleanupPreviewContents();
// We release here without deleting so that the caller still has the
// responsibility for deleting the TabContents.
ignore_result(preview_contents_.release());
preview_contents_.reset(TabContents::FromWebContents(new_contents));
SetupPreviewContents();
loader_delegate_->SwappedTabContents(this);
}
|