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
|
// Copyright 2013 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_overlay.h"
#include "base/auto_reset.h"
#include "base/supports_user_data.h"
#include "content/public/browser/web_contents.h"
namespace {
int kUserDataKey;
class InstantOverlayUserData : public base::SupportsUserData::Data {
public:
explicit InstantOverlayUserData(InstantOverlay* overlay)
: overlay_(overlay) {}
virtual InstantOverlay* overlay() const { return overlay_; }
private:
~InstantOverlayUserData() {}
InstantOverlay* const overlay_;
DISALLOW_COPY_AND_ASSIGN(InstantOverlayUserData);
};
} // namespace
// static
InstantOverlay* InstantOverlay::FromWebContents(
const content::WebContents* web_contents) {
InstantOverlayUserData* data = static_cast<InstantOverlayUserData*>(
web_contents->GetUserData(&kUserDataKey));
return data ? data->overlay() : NULL;
}
InstantOverlay::InstantOverlay(InstantController* controller,
const std::string& instant_url)
: InstantPage(controller),
loader_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
instant_url_(instant_url),
is_stale_(false),
is_pointer_down_from_activate_(false) {
}
InstantOverlay::~InstantOverlay() {
}
void InstantOverlay::InitContents(Profile* profile,
const content::WebContents* active_tab) {
loader_.Init(GURL(instant_url_), profile, active_tab,
base::Bind(&InstantOverlay::HandleStalePage,
base::Unretained(this)));
SetContents(loader_.contents());
contents()->SetUserData(&kUserDataKey, new InstantOverlayUserData(this));
loader_.Load();
}
scoped_ptr<content::WebContents> InstantOverlay::ReleaseContents() {
contents()->RemoveUserData(&kUserDataKey);
SetContents(NULL);
return loader_.ReleaseContents();
}
void InstantOverlay::DidNavigate(
const history::HistoryAddPageArgs& add_page_args) {
last_navigation_ = add_page_args;
}
bool InstantOverlay::IsUsingLocalPreview() const {
return instant_url_ == InstantController::kLocalOmniboxPopupURL;
}
void InstantOverlay::Update(const string16& text,
size_t selection_start,
size_t selection_end,
bool verbatim) {
last_navigation_ = history::HistoryAddPageArgs();
InstantPage::Update(text, selection_start, selection_end, verbatim);
}
bool InstantOverlay::ShouldProcessRenderViewCreated() {
return true;
}
bool InstantOverlay::ShouldProcessRenderViewGone() {
return true;
}
bool InstantOverlay::ShouldProcessAboutToNavigateMainFrame() {
return true;
}
bool InstantOverlay::ShouldProcessSetSuggestions() {
return true;
}
bool InstantOverlay::ShouldProcessShowInstantPreview() {
return true;
}
bool InstantOverlay::ShouldProcessNavigateToURL() {
return true;
}
void InstantOverlay::OnSwappedContents() {
contents()->RemoveUserData(&kUserDataKey);
SetContents(loader_.contents());
contents()->SetUserData(&kUserDataKey, new InstantOverlayUserData(this));
instant_controller()->SwappedOverlayContents();
}
void InstantOverlay::OnFocus() {
// The preview is getting focus. Equivalent to it being clicked.
base::AutoReset<bool> reset(&is_pointer_down_from_activate_, true);
instant_controller()->FocusedOverlayContents();
}
void InstantOverlay::OnMouseDown() {
is_pointer_down_from_activate_ = true;
}
void InstantOverlay::OnMouseUp() {
if (is_pointer_down_from_activate_) {
is_pointer_down_from_activate_ = false;
instant_controller()->CommitIfPossible(INSTANT_COMMIT_FOCUS_LOST);
}
}
content::WebContents* InstantOverlay::OpenURLFromTab(
content::WebContents* source,
const content::OpenURLParams& params) {
// We will allow the navigate to continue if we are able to commit the
// overlay.
//
// First, cache the overlay contents since committing it will cause the
// contents to be released (and be set to NULL).
content::WebContents* overlay = contents();
if (instant_controller()->CommitIfPossible(INSTANT_COMMIT_NAVIGATED)) {
// If the commit was successful, the overlay's delegate should be the tab
// strip, which will be able to handle the navigation.
DCHECK_NE(&loader_, overlay->GetDelegate());
return overlay->GetDelegate()->OpenURLFromTab(source, params);
}
return NULL;
}
void InstantOverlay::HandleStalePage() {
is_stale_ = true;
instant_controller()->ReloadOverlayIfStale();
}
|