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::SetMarginSize(const int start, const int end) { | |
40 Send(new ChromeViewMsg_SearchBoxMarginChange(routing_id(), start, end)); | |
41 } | |
42 | |
43 void InstantPage::InitializeFonts() { | |
44 const gfx::Font& omnibox_font = | |
45 ui::ResourceBundle::GetSharedInstance().GetFont( | |
46 ui::ResourceBundle::MediumFont); | |
47 string16 omnibox_font_name = UTF8ToUTF16(omnibox_font.GetFontName()); | |
48 size_t omnibox_font_size = omnibox_font.GetFontSize(); | |
49 Send(new ChromeViewMsg_SearchBoxFontInformation( | |
50 routing_id(), omnibox_font_name, omnibox_font_size)); | |
51 } | |
52 | |
53 void InstantPage::DetermineIfPageSupportsInstant() { | |
54 Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id())); | |
55 } | |
56 | |
57 void InstantPage::SendAutocompleteResults( | |
58 const std::vector<InstantAutocompleteResult>& results) { | |
59 Send(new ChromeViewMsg_SearchBoxAutocompleteResults(routing_id(), results)); | |
60 } | |
61 | |
62 void InstantPage::UpOrDownKeyPressed(int count) { | |
63 Send(new ChromeViewMsg_SearchBoxUpOrDownKeyPressed(routing_id(), count)); | |
64 } | |
65 | |
66 void InstantPage::SearchModeChanged(const chrome::search::Mode& mode) { | |
67 Send(new ChromeViewMsg_SearchBoxModeChanged(routing_id(), mode)); | |
68 } | |
69 | |
70 void InstantPage::SendThemeBackgroundInfo( | |
71 const ThemeBackgroundInfo& theme_info) { | |
72 Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info)); | |
73 } | |
74 | |
75 void InstantPage::SendThemeAreaHeight(int height) { | |
76 Send(new ChromeViewMsg_SearchBoxThemeAreaHeightChanged(routing_id(), height)); | |
77 } | |
78 | |
79 void InstantPage::SetDisplayInstantResults(bool display_instant_results) { | |
80 Send(new ChromeViewMsg_SearchBoxSetDisplayInstantResults( | |
81 routing_id(), display_instant_results)); | |
82 } | |
83 | |
84 void InstantPage::KeyCaptureChanged(bool is_key_capture_enabled) { | |
85 Send(new ChromeViewMsg_SearchBoxKeyCaptureChanged( | |
86 routing_id(), is_key_capture_enabled)); | |
87 } | |
88 | |
89 InstantPage::InstantPage(Delegate* delegate) | |
90 : delegate_(delegate), | |
91 contents_(NULL), | |
92 supports_instant_(false) { | |
93 } | |
94 | |
95 void InstantPage::SetContents(content::WebContents* contents) { | |
96 Observe(contents); | |
97 contents_ = contents; | |
98 } | |
99 | |
100 bool InstantPage::ShouldProcessRenderViewCreated() { | |
101 return true; | |
102 } | |
103 | |
104 bool InstantPage::ShouldProcessRenderViewGone() { | |
105 return true; | |
106 } | |
107 | |
108 bool InstantPage::ShouldProcessAboutToNavigateMainFrame() { | |
109 return true; | |
110 } | |
111 | |
112 bool InstantPage::ShouldProcessSetSuggestions() { | |
113 return true; | |
114 } | |
115 | |
116 bool InstantPage::ShouldProcessShowInstantPreview() { | |
117 return true; | |
118 } | |
119 | |
120 bool InstantPage::ShouldProcessStartCapturingKeyStrokes() { | |
121 return true; | |
122 } | |
123 | |
124 bool InstantPage::ShouldProcessStopCapturingKeyStrokes() { | |
125 return true; | |
126 } | |
127 | |
128 bool InstantPage::ShouldProcessNavigateToURL() { | |
129 return true; | |
130 } | |
131 | |
132 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) { | |
133 if (ShouldProcessRenderViewCreated()) | |
134 delegate_->InstantPageRenderViewCreated(contents_); | |
135 } | |
136 | |
137 void InstantPage::DidFinishLoad( | |
138 int64 /* frame_id */, | |
139 const GURL& /* validated_url */, | |
140 bool is_main_frame, | |
141 content::RenderViewHost* /* render_view_host */) { | |
142 if (is_main_frame && !supports_instant_) | |
143 DetermineIfPageSupportsInstant(); | |
144 } | |
145 | |
146 bool InstantPage::OnMessageReceived(const IPC::Message& message) { | |
147 bool handled = true; | |
148 IPC_BEGIN_MESSAGE_MAP(InstantPage, message) | |
149 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions) | |
150 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined, | |
151 OnInstantSupportDetermined) | |
152 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantPreview, | |
153 OnShowInstantPreview) | |
154 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_StartCapturingKeyStrokes, | |
155 OnStartCapturingKeyStrokes); | |
156 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_StopCapturingKeyStrokes, | |
157 OnStopCapturingKeyStrokes); | |
158 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate, | |
159 OnSearchBoxNavigate); | |
160 IPC_MESSAGE_UNHANDLED(handled = false) | |
161 IPC_END_MESSAGE_MAP() | |
162 return handled; | |
163 } | |
164 | |
165 void InstantPage::RenderViewGone(base::TerminationStatus /* status */) { | |
166 if (ShouldProcessRenderViewGone()) | |
167 delegate_->InstantPageRenderViewGone(contents_); | |
168 } | |
169 | |
170 void InstantPage::DidCommitProvisionalLoadForFrame( | |
171 int64 /* frame_id */, | |
172 bool is_main_frame, | |
173 const GURL& url, | |
174 content::PageTransition /* transition_type */, | |
175 content::RenderViewHost* /* render_view_host */) { | |
176 if (is_main_frame && ShouldProcessAboutToNavigateMainFrame()) | |
177 delegate_->InstantPageAboutToNavigateMainFrame(contents_, url); | |
178 } | |
179 | |
180 void InstantPage::OnSetSuggestions( | |
181 int page_id, | |
182 const std::vector<InstantSuggestion>& suggestions) { | |
183 if (contents_ && contents_->IsActiveEntry(page_id)) { | |
sreeram
2013/02/07 22:09:54
No need for "if (contents_)" clause. There's no wa
samarth
2013/02/07 22:45:42
Done.
| |
184 OnInstantSupportDetermined(page_id, true); | |
185 if (ShouldProcessSetSuggestions()) | |
186 delegate_->SetSuggestions(contents_, suggestions); | |
187 } | |
188 } | |
189 | |
190 void InstantPage::OnInstantSupportDetermined(int page_id, | |
191 bool supports_instant) { | |
192 if (!contents_ || !contents_->IsActiveEntry(page_id) || supports_instant_) { | |
193 // Nothing to do if the page already supports Instant. | |
194 return; | |
195 } | |
196 | |
197 supports_instant_ = supports_instant; | |
198 delegate_->InstantSupportDetermined(contents_, supports_instant); | |
199 | |
200 // If the page doesn't support Instant, stop listening to it. | |
201 if (!supports_instant) | |
202 Observe(NULL); | |
sreeram
2013/02/07 22:09:54
This code above works as is now, but I'd like to m
samarth
2013/02/07 22:45:42
Done.
| |
203 } | |
204 | |
205 void InstantPage::OnShowInstantPreview(int page_id, | |
206 InstantShownReason reason, | |
207 int height, | |
208 InstantSizeUnits units) { | |
209 if (contents_ && contents_->IsActiveEntry(page_id)) { | |
210 OnInstantSupportDetermined(page_id, true); | |
211 if (ShouldProcessShowInstantPreview()) | |
212 delegate_->ShowInstantPreview(contents_, reason, height, units); | |
213 } | |
214 } | |
215 | |
216 void InstantPage::OnStartCapturingKeyStrokes(int page_id) { | |
217 if (contents_ && contents_->IsActiveEntry(page_id)) { | |
218 OnInstantSupportDetermined(page_id, true); | |
219 if (ShouldProcessStartCapturingKeyStrokes()) | |
220 delegate_->StartCapturingKeyStrokes(contents_); | |
221 } | |
222 } | |
223 | |
224 void InstantPage::OnStopCapturingKeyStrokes(int page_id) { | |
225 if (contents_ && contents_->IsActiveEntry(page_id)) { | |
226 OnInstantSupportDetermined(page_id, true); | |
227 if (ShouldProcessStopCapturingKeyStrokes()) | |
228 delegate_->StopCapturingKeyStrokes(contents_); | |
229 } | |
230 } | |
231 | |
232 void InstantPage::OnSearchBoxNavigate(int page_id, | |
233 const GURL& url, | |
234 content::PageTransition transition) { | |
235 if (contents_ && contents_->IsActiveEntry(page_id)) { | |
236 OnInstantSupportDetermined(page_id, true); | |
237 if (ShouldProcessNavigateToURL()) | |
238 delegate_->NavigateToURL(contents_, url, transition); | |
239 } | |
240 } | |
OLD | NEW |