OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 #include "chrome/browser/instant/instant_overlay.h" | |
6 | |
7 #include "base/auto_reset.h" | |
8 #include "base/supports_user_data.h" | |
9 #include "chrome/browser/instant/search.h" | |
10 #include "chrome/common/url_constants.h" | |
11 #include "content/public/browser/web_contents.h" | |
12 | |
13 namespace { | |
14 | |
15 int kUserDataKey; | |
16 | |
17 class InstantOverlayUserData : public base::SupportsUserData::Data { | |
18 public: | |
19 explicit InstantOverlayUserData(InstantOverlay* overlay) | |
20 : overlay_(overlay) {} | |
21 | |
22 virtual InstantOverlay* overlay() const { return overlay_; } | |
23 | |
24 private: | |
25 virtual ~InstantOverlayUserData() {} | |
26 | |
27 InstantOverlay* const overlay_; | |
28 | |
29 DISALLOW_COPY_AND_ASSIGN(InstantOverlayUserData); | |
30 }; | |
31 | |
32 } // namespace | |
33 | |
34 // static | |
35 InstantOverlay* InstantOverlay::FromWebContents( | |
36 const content::WebContents* web_contents) { | |
37 InstantOverlayUserData* data = static_cast<InstantOverlayUserData*>( | |
38 web_contents->GetUserData(&kUserDataKey)); | |
39 return data ? data->overlay() : NULL; | |
40 } | |
41 | |
42 InstantOverlay::InstantOverlay(InstantController* controller, | |
43 const std::string& instant_url) | |
44 : InstantPage(controller), | |
45 loader_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
46 instant_url_(instant_url), | |
47 is_stale_(false), | |
48 is_pointer_down_from_activate_(false) { | |
49 } | |
50 | |
51 InstantOverlay::~InstantOverlay() { | |
52 } | |
53 | |
54 void InstantOverlay::InitContents(Profile* profile, | |
55 const content::WebContents* active_tab) { | |
56 loader_.Init(GURL(instant_url_), profile, active_tab, | |
57 base::Bind(&InstantOverlay::HandleStalePage, | |
58 base::Unretained(this))); | |
59 SetContents(loader_.contents()); | |
60 contents()->SetUserData(&kUserDataKey, new InstantOverlayUserData(this)); | |
61 loader_.Load(); | |
62 } | |
63 | |
64 scoped_ptr<content::WebContents> InstantOverlay::ReleaseContents() { | |
65 contents()->RemoveUserData(&kUserDataKey); | |
66 SetContents(NULL); | |
67 return loader_.ReleaseContents(); | |
68 } | |
69 | |
70 void InstantOverlay::DidNavigate( | |
71 const history::HistoryAddPageArgs& add_page_args) { | |
72 last_navigation_ = add_page_args; | |
73 } | |
74 | |
75 bool InstantOverlay::IsUsingLocalOverlay() const { | |
76 return instant_url_ == chrome::kChromeSearchLocalOmniboxPopupURL; | |
77 } | |
78 | |
79 void InstantOverlay::Update(const string16& text, | |
80 size_t selection_start, | |
81 size_t selection_end, | |
82 bool verbatim) { | |
83 last_navigation_ = history::HistoryAddPageArgs(); | |
84 InstantPage::Update(text, selection_start, selection_end, verbatim); | |
85 } | |
86 | |
87 bool InstantOverlay::ShouldProcessRenderViewCreated() { | |
88 return true; | |
89 } | |
90 | |
91 bool InstantOverlay::ShouldProcessRenderViewGone() { | |
92 return true; | |
93 } | |
94 | |
95 bool InstantOverlay::ShouldProcessAboutToNavigateMainFrame() { | |
96 return true; | |
97 } | |
98 | |
99 bool InstantOverlay::ShouldProcessSetSuggestions() { | |
100 return true; | |
101 } | |
102 | |
103 bool InstantOverlay::ShouldProcessShowInstantOverlay() { | |
104 return true; | |
105 } | |
106 | |
107 bool InstantOverlay::ShouldProcessNavigateToURL() { | |
108 return true; | |
109 } | |
110 | |
111 void InstantOverlay::OnSwappedContents() { | |
112 contents()->RemoveUserData(&kUserDataKey); | |
113 SetContents(loader_.contents()); | |
114 contents()->SetUserData(&kUserDataKey, new InstantOverlayUserData(this)); | |
115 instant_controller()->SwappedOverlayContents(); | |
116 } | |
117 | |
118 void InstantOverlay::OnFocus() { | |
119 // The overlay is getting focus. Equivalent to it being clicked. | |
120 base::AutoReset<bool> reset(&is_pointer_down_from_activate_, true); | |
121 instant_controller()->FocusedOverlayContents(); | |
122 } | |
123 | |
124 void InstantOverlay::OnMouseDown() { | |
125 is_pointer_down_from_activate_ = true; | |
126 } | |
127 | |
128 void InstantOverlay::OnMouseUp() { | |
129 if (is_pointer_down_from_activate_) { | |
130 is_pointer_down_from_activate_ = false; | |
131 instant_controller()->CommitIfPossible(INSTANT_COMMIT_FOCUS_LOST); | |
132 } | |
133 } | |
134 | |
135 content::WebContents* InstantOverlay::OpenURLFromTab( | |
136 content::WebContents* source, | |
137 const content::OpenURLParams& params) { | |
138 if (!supports_instant()) { | |
139 // If the page doesn't yet support Instant, it hasn't fully loaded. | |
140 // This is a redirect that we should allow. http://crbug.com/177948 | |
141 content::NavigationController::LoadURLParams load_params(params.url); | |
142 load_params.transition_type = params.transition; | |
143 load_params.referrer = params.referrer; | |
144 load_params.extra_headers = params.extra_headers; | |
145 load_params.is_renderer_initiated = params.is_renderer_initiated; | |
146 load_params.transferred_global_request_id = | |
147 params.transferred_global_request_id; | |
148 load_params.is_cross_site_redirect = params.is_cross_site_redirect; | |
149 | |
150 contents()->GetController().LoadURLWithParams(load_params); | |
151 return contents(); | |
152 } | |
153 | |
154 // We will allow the navigate to continue if we are able to commit the | |
155 // overlay. | |
156 // | |
157 // First, cache the overlay contents since committing it will cause the | |
158 // contents to be released (and be set to NULL). | |
159 content::WebContents* overlay = contents(); | |
160 if (instant_controller()->CommitIfPossible(INSTANT_COMMIT_NAVIGATED)) { | |
161 // If the commit was successful, the overlay's delegate should be the tab | |
162 // strip, which will be able to handle the navigation. | |
163 DCHECK_NE(&loader_, overlay->GetDelegate()); | |
164 return overlay->GetDelegate()->OpenURLFromTab(source, params); | |
165 } | |
166 return NULL; | |
167 } | |
168 | |
169 void InstantOverlay::LoadCompletedMainFrame() { | |
170 instant_controller()->OverlayLoadCompletedMainFrame(); | |
171 } | |
172 | |
173 void InstantOverlay::HandleStalePage() { | |
174 is_stale_ = true; | |
175 instant_controller()->ReloadOverlayIfStale(); | |
176 } | |
OLD | NEW |