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_page.h" | |
6 | |
7 #include "base/utf_string_conversions.h" | |
8 #include "chrome/common/render_messages.h" | |
9 #include "content/public/browser/web_contents.h" | |
10 #include "ui/base/resource/resource_bundle.h" | |
11 #include "ui/gfx/font.h" | |
12 | |
13 InstantPage::Delegate::~Delegate() { | |
14 } | |
15 | |
16 InstantPage::~InstantPage() { | |
17 } | |
18 | |
19 void InstantPage::Update(const string16& text, | |
20 size_t selection_start, | |
21 size_t selection_end, | |
22 bool verbatim) { | |
23 Send(new ChromeViewMsg_SearchBoxChange(routing_id(), text, verbatim, | |
24 selection_start, selection_end)); | |
25 } | |
26 | |
27 void InstantPage::Submit(const string16& text) { | |
28 Send(new ChromeViewMsg_SearchBoxSubmit(routing_id(), text)); | |
29 } | |
30 | |
31 void InstantPage::Cancel(const string16& text) { | |
32 Send(new ChromeViewMsg_SearchBoxCancel(routing_id(), text)); | |
33 } | |
34 | |
35 void InstantPage::SetPopupBounds(const gfx::Rect& bounds) { | |
36 Send(new ChromeViewMsg_SearchBoxPopupResize(routing_id(), bounds)); | |
37 } | |
38 | |
39 void InstantPage::SetOmniboxBounds(const gfx::Rect& bounds) { | |
40 Send(new ChromeViewMsg_SearchBoxMarginChange( | |
41 routing_id(), bounds.x(), bounds.width())); | |
42 } | |
43 | |
44 void InstantPage::InitializeFonts() { | |
45 // TODO(sail) Remove this once the Mac omnibox font size is updated. | |
46 #if defined(OS_MACOSX) | |
47 ui::ResourceBundle::FontStyle font_style = ui::ResourceBundle::BaseFont; | |
48 #else | |
49 ui::ResourceBundle::FontStyle font_style = ui::ResourceBundle::MediumFont; | |
50 #endif | |
51 const gfx::Font& omnibox_font = | |
52 ui::ResourceBundle::GetSharedInstance().GetFont(font_style); | |
53 string16 omnibox_font_name = UTF8ToUTF16(omnibox_font.GetFontName()); | |
54 size_t omnibox_font_size = omnibox_font.GetFontSize(); | |
55 Send(new ChromeViewMsg_SearchBoxFontInformation( | |
56 routing_id(), omnibox_font_name, omnibox_font_size)); | |
57 } | |
58 | |
59 void InstantPage::GrantChromeSearchAccessFromOrigin(const GURL& origin_url) { | |
60 DCHECK(origin_url.is_valid()); | |
61 Send(new ChromeViewMsg_SearchBoxGrantChromeSearchAccessFromOrigin( | |
62 routing_id(), origin_url)); | |
63 } | |
64 | |
65 void InstantPage::DetermineIfPageSupportsInstant() { | |
66 Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id())); | |
67 } | |
68 | |
69 void InstantPage::SendAutocompleteResults( | |
70 const std::vector<InstantAutocompleteResult>& results) { | |
71 Send(new ChromeViewMsg_SearchBoxAutocompleteResults(routing_id(), results)); | |
72 } | |
73 | |
74 void InstantPage::UpOrDownKeyPressed(int count) { | |
75 Send(new ChromeViewMsg_SearchBoxUpOrDownKeyPressed(routing_id(), count)); | |
76 } | |
77 | |
78 void InstantPage::CancelSelection(const string16& user_text) { | |
79 Send(new ChromeViewMsg_SearchBoxCancelSelection(routing_id(), user_text)); | |
80 } | |
81 | |
82 void InstantPage::SendThemeBackgroundInfo( | |
83 const ThemeBackgroundInfo& theme_info) { | |
84 Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info)); | |
85 } | |
86 | |
87 void InstantPage::SetDisplayInstantResults(bool display_instant_results) { | |
88 Send(new ChromeViewMsg_SearchBoxSetDisplayInstantResults( | |
89 routing_id(), display_instant_results)); | |
90 } | |
91 | |
92 void InstantPage::KeyCaptureChanged(bool is_key_capture_enabled) { | |
93 Send(new ChromeViewMsg_SearchBoxKeyCaptureChanged( | |
94 routing_id(), is_key_capture_enabled)); | |
95 } | |
96 | |
97 void InstantPage::SendMostVisitedItems( | |
98 const std::vector<InstantMostVisitedItem>& items) { | |
99 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items)); | |
100 } | |
101 | |
102 InstantPage::InstantPage(Delegate* delegate) | |
103 : delegate_(delegate), | |
104 supports_instant_(false) { | |
105 } | |
106 | |
107 void InstantPage::SetContents(content::WebContents* contents) { | |
108 Observe(contents); | |
109 } | |
110 | |
111 bool InstantPage::ShouldProcessRenderViewCreated() { | |
112 return false; | |
113 } | |
114 | |
115 bool InstantPage::ShouldProcessRenderViewGone() { | |
116 return false; | |
117 } | |
118 | |
119 bool InstantPage::ShouldProcessAboutToNavigateMainFrame() { | |
120 return false; | |
121 } | |
122 | |
123 bool InstantPage::ShouldProcessSetSuggestions() { | |
124 return false; | |
125 } | |
126 | |
127 bool InstantPage::ShouldProcessShowInstantOverlay() { | |
128 return false; | |
129 } | |
130 | |
131 bool InstantPage::ShouldProcessFocusOmnibox() { | |
132 return false; | |
133 } | |
134 | |
135 bool InstantPage::ShouldProcessStartCapturingKeyStrokes() { | |
136 return false; | |
137 } | |
138 | |
139 bool InstantPage::ShouldProcessStopCapturingKeyStrokes() { | |
140 return false; | |
141 } | |
142 | |
143 bool InstantPage::ShouldProcessNavigateToURL() { | |
144 return false; | |
145 } | |
146 | |
147 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) { | |
148 if (ShouldProcessRenderViewCreated()) | |
149 delegate_->InstantPageRenderViewCreated(contents()); | |
150 } | |
151 | |
152 void InstantPage::DidFinishLoad( | |
153 int64 /* frame_id */, | |
154 const GURL& /* validated_url */, | |
155 bool is_main_frame, | |
156 content::RenderViewHost* /* render_view_host */) { | |
157 if (is_main_frame && !supports_instant_) | |
158 DetermineIfPageSupportsInstant(); | |
159 } | |
160 | |
161 bool InstantPage::OnMessageReceived(const IPC::Message& message) { | |
162 bool handled = true; | |
163 IPC_BEGIN_MESSAGE_MAP(InstantPage, message) | |
164 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions) | |
165 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined, | |
166 OnInstantSupportDetermined) | |
167 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay, | |
168 OnShowInstantOverlay) | |
169 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox) | |
170 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_StartCapturingKeyStrokes, | |
171 OnStartCapturingKeyStrokes); | |
172 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_StopCapturingKeyStrokes, | |
173 OnStopCapturingKeyStrokes); | |
174 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate, | |
175 OnSearchBoxNavigate); | |
176 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem, | |
177 OnDeleteMostVisitedItem); | |
178 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion, | |
179 OnUndoMostVisitedDeletion); | |
180 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions, | |
181 OnUndoAllMostVisitedDeletions); | |
182 IPC_MESSAGE_UNHANDLED(handled = false) | |
183 IPC_END_MESSAGE_MAP() | |
184 return handled; | |
185 } | |
186 | |
187 void InstantPage::RenderViewGone(base::TerminationStatus /* status */) { | |
188 if (ShouldProcessRenderViewGone()) | |
189 delegate_->InstantPageRenderViewGone(contents()); | |
190 } | |
191 | |
192 void InstantPage::DidCommitProvisionalLoadForFrame( | |
193 int64 /* frame_id */, | |
194 bool is_main_frame, | |
195 const GURL& url, | |
196 content::PageTransition /* transition_type */, | |
197 content::RenderViewHost* /* render_view_host */) { | |
198 if (is_main_frame && ShouldProcessAboutToNavigateMainFrame()) | |
199 delegate_->InstantPageAboutToNavigateMainFrame(contents(), url); | |
200 } | |
201 | |
202 void InstantPage::OnSetSuggestions( | |
203 int page_id, | |
204 const std::vector<InstantSuggestion>& suggestions) { | |
205 if (contents()->IsActiveEntry(page_id)) { | |
206 OnInstantSupportDetermined(page_id, true); | |
207 if (ShouldProcessSetSuggestions()) | |
208 delegate_->SetSuggestions(contents(), suggestions); | |
209 } | |
210 } | |
211 | |
212 void InstantPage::OnInstantSupportDetermined(int page_id, | |
213 bool supports_instant) { | |
214 if (!contents()->IsActiveEntry(page_id) || supports_instant_) { | |
215 // Nothing to do if the page already supports Instant. | |
216 return; | |
217 } | |
218 | |
219 supports_instant_ = supports_instant; | |
220 delegate_->InstantSupportDetermined(contents(), supports_instant); | |
221 | |
222 // If the page doesn't support Instant, stop listening to it. | |
223 if (!supports_instant) | |
224 Observe(NULL); | |
225 } | |
226 | |
227 void InstantPage::OnShowInstantOverlay(int page_id, | |
228 int height, | |
229 InstantSizeUnits units) { | |
230 if (contents()->IsActiveEntry(page_id)) { | |
231 OnInstantSupportDetermined(page_id, true); | |
232 if (ShouldProcessShowInstantOverlay()) | |
233 delegate_->ShowInstantOverlay(contents(), height, units); | |
234 } | |
235 } | |
236 | |
237 void InstantPage::OnFocusOmnibox(int page_id) { | |
238 if (contents()->IsActiveEntry(page_id)) { | |
239 OnInstantSupportDetermined(page_id, true); | |
240 if (ShouldProcessFocusOmnibox()) | |
241 delegate_->FocusOmnibox(contents()); | |
242 } | |
243 } | |
244 | |
245 void InstantPage::OnStartCapturingKeyStrokes(int page_id) { | |
246 if (contents()->IsActiveEntry(page_id)) { | |
247 OnInstantSupportDetermined(page_id, true); | |
248 if (ShouldProcessStartCapturingKeyStrokes()) | |
249 delegate_->StartCapturingKeyStrokes(contents()); | |
250 } | |
251 } | |
252 | |
253 void InstantPage::OnStopCapturingKeyStrokes(int page_id) { | |
254 if (contents()->IsActiveEntry(page_id)) { | |
255 OnInstantSupportDetermined(page_id, true); | |
256 if (ShouldProcessStopCapturingKeyStrokes()) | |
257 delegate_->StopCapturingKeyStrokes(contents()); | |
258 } | |
259 } | |
260 | |
261 void InstantPage::OnSearchBoxNavigate(int page_id, | |
262 const GURL& url, | |
263 content::PageTransition transition, | |
264 WindowOpenDisposition disposition) { | |
265 if (contents()->IsActiveEntry(page_id)) { | |
266 OnInstantSupportDetermined(page_id, true); | |
267 if (ShouldProcessNavigateToURL()) | |
268 delegate_->NavigateToURL(contents(), url, transition, disposition); | |
269 } | |
270 } | |
271 | |
272 void InstantPage::OnDeleteMostVisitedItem(uint64 most_visited_item_id) { | |
273 delegate_->DeleteMostVisitedItem(most_visited_item_id); | |
274 } | |
275 | |
276 void InstantPage::OnUndoMostVisitedDeletion(uint64 most_visited_item_id) { | |
277 delegate_->UndoMostVisitedDeletion(most_visited_item_id); | |
278 } | |
279 | |
280 void InstantPage::OnUndoAllMostVisitedDeletions() { | |
281 delegate_->UndoAllMostVisitedDeletions(); | |
282 } | |
OLD | NEW |