OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/instant/instant_page.h" | 5 #include "chrome/browser/instant/instant_page.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "chrome/browser/instant/instant_service.h" |
8 #include "chrome/common/render_messages.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/common/instant_messages.h" |
| 10 #include "content/public/browser/render_process_host.h" |
9 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
10 #include "ui/base/resource/resource_bundle.h" | |
11 #include "ui/gfx/font.h" | |
12 | 12 |
13 InstantPage::Delegate::~Delegate() { | 13 InstantPage::Delegate::~Delegate() { |
14 } | 14 } |
15 | 15 |
| 16 InstantPage::InstantPage(Delegate* delegate, InstantService* service) |
| 17 : delegate_(delegate), |
| 18 service_(service), |
| 19 routing_id_(MSG_ROUTING_NONE), |
| 20 supports_instant_(false), |
| 21 navigated_after_change_(false) { |
| 22 } |
| 23 |
16 InstantPage::~InstantPage() { | 24 InstantPage::~InstantPage() { |
17 } | 25 } |
18 | 26 |
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<MostVisitedItem>& items) { | |
99 Send(new ChromeViewMsg_InstantMostVisitedItemsChanged(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) { | 27 void InstantPage::SetContents(content::WebContents* contents) { |
108 Observe(contents); | 28 Observe(contents); |
109 } | 29 } |
110 | 30 |
111 bool InstantPage::ShouldProcessRenderViewCreated() { | 31 void InstantPage::DetermineInstantSupport() { |
112 return false; | 32 SendMessage(scoped_ptr<IPC::Message>( |
113 } | 33 new ChromeViewMsg_SearchBoxDetermineInstantSupport(routing_id()))); |
114 | 34 } |
115 bool InstantPage::ShouldProcessRenderViewGone() { | 35 |
116 return false; | 36 void InstantPage::Change(const string16& query, |
117 } | 37 bool verbatim, |
118 | 38 size_t selection_start, |
119 bool InstantPage::ShouldProcessAboutToNavigateMainFrame() { | 39 size_t selection_end) { |
120 return false; | 40 navigated_after_change_ = false; |
121 } | 41 SendMessage(scoped_ptr<IPC::Message>(new ChromeViewMsg_SearchBoxChange( |
122 | 42 routing_id(), query, verbatim, selection_start, selection_end))); |
123 bool InstantPage::ShouldProcessSetSuggestions() { | 43 } |
124 return false; | 44 |
125 } | 45 void InstantPage::Submit(const string16& query) { |
126 | 46 SendMessage(scoped_ptr<IPC::Message>(new ChromeViewMsg_SearchBoxSubmit( |
127 bool InstantPage::ShouldProcessShowInstantOverlay() { | 47 routing_id(), query))); |
128 return false; | 48 } |
129 } | 49 |
130 | 50 void InstantPage::AutocompleteResults( |
131 bool InstantPage::ShouldProcessFocusOmnibox() { | 51 const std::vector<InstantAutocompleteResult>& results) { |
132 return false; | 52 SendMessage(scoped_ptr<IPC::Message>( |
133 } | 53 new ChromeViewMsg_SearchBoxAutocompleteResults(routing_id(), results))); |
134 | 54 } |
135 bool InstantPage::ShouldProcessStartCapturingKeyStrokes() { | 55 |
136 return false; | 56 void InstantPage::Select(int count) { |
137 } | 57 SendMessage(scoped_ptr<IPC::Message>(new ChromeViewMsg_SearchBoxSelect( |
138 | 58 routing_id(), count))); |
139 bool InstantPage::ShouldProcessStopCapturingKeyStrokes() { | 59 } |
140 return false; | 60 |
141 } | 61 void InstantPage::Cancel(const string16& query) { |
142 | 62 SendMessage(scoped_ptr<IPC::Message>(new ChromeViewMsg_SearchBoxCancel( |
143 bool InstantPage::ShouldProcessNavigateToURL() { | 63 routing_id(), query))); |
144 return false; | 64 } |
145 } | 65 |
146 | 66 void InstantPage::Blur(const string16& query) { |
147 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) { | 67 SendMessage(scoped_ptr<IPC::Message>(new ChromeViewMsg_SearchBoxBlur( |
148 if (ShouldProcessRenderViewCreated()) | 68 routing_id(), query))); |
149 delegate_->InstantPageRenderViewCreated(contents()); | 69 } |
| 70 |
| 71 void InstantPage::PopupBounds(const gfx::Rect& bounds) { |
| 72 SendMessage(scoped_ptr<IPC::Message>(new ChromeViewMsg_SearchBoxPopupBounds( |
| 73 routing_id(), bounds))); |
| 74 } |
| 75 |
| 76 void InstantPage::OmniboxBounds(const gfx::Rect& bounds) { |
| 77 SendMessage(scoped_ptr<IPC::Message>(new ChromeViewMsg_SearchBoxOmniboxBounds( |
| 78 routing_id(), bounds))); |
| 79 } |
| 80 |
| 81 void InstantPage::KeyCaptureChanged(bool is_key_capture_enabled) { |
| 82 SendMessage(scoped_ptr<IPC::Message>( |
| 83 new ChromeViewMsg_SearchBoxKeyCaptureChanged(routing_id(), |
| 84 is_key_capture_enabled))); |
| 85 } |
| 86 |
| 87 void InstantPage::DisplayInstantResults(bool display_instant_results) { |
| 88 SendMessage(scoped_ptr<IPC::Message>( |
| 89 new ChromeViewMsg_SearchBoxDisplayInstantResults( |
| 90 routing_id(), display_instant_results))); |
| 91 } |
| 92 |
| 93 void InstantPage::ThemeChanged(const ThemeBackgroundInfo& theme_info) { |
| 94 SendMessage(scoped_ptr<IPC::Message>(new ChromeViewMsg_SearchBoxThemeChanged( |
| 95 routing_id(), theme_info))); |
| 96 } |
| 97 |
| 98 void InstantPage::FontChanged(const string16& font_name, size_t font_size) { |
| 99 SendMessage(scoped_ptr<IPC::Message>(new ChromeViewMsg_SearchBoxFontChanged( |
| 100 routing_id(), font_name, font_size))); |
| 101 } |
| 102 |
| 103 void InstantPage::MostVisitedItems(const std::vector<MostVisitedItem>& items) { |
| 104 SendMessage(scoped_ptr<IPC::Message>( |
| 105 new ChromeViewMsg_SearchBoxMostVisitedItems(routing_id(), items))); |
| 106 } |
| 107 |
| 108 void InstantPage::GrantChromeSearchAccessFromOrigin(const GURL& origin_url) { |
| 109 SendMessage(scoped_ptr<IPC::Message>( |
| 110 new ChromeViewMsg_SearchBoxGrantChromeSearchAccessFromOrigin( |
| 111 routing_id(), origin_url))); |
| 112 } |
| 113 |
| 114 void InstantPage::RenderViewGone(base::TerminationStatus /* status */) { |
| 115 delegate_->RenderViewGone(contents()); |
| 116 } |
| 117 |
| 118 void InstantPage::DidNavigateMainFrame( |
| 119 const content::LoadCommittedDetails& /* details */, |
| 120 const content::FrameNavigateParams& /* params */) { |
| 121 navigated_after_change_ = true; |
| 122 if (routing_id_ != routing_id()) { |
| 123 routing_id_ = routing_id(); |
| 124 delegate_->InitSearchBox(contents()); |
| 125 } |
150 } | 126 } |
151 | 127 |
152 void InstantPage::DidFinishLoad( | 128 void InstantPage::DidFinishLoad( |
153 int64 /* frame_id */, | 129 int64 /* frame_id */, |
154 const GURL& /* validated_url */, | 130 const GURL& /* validated_url */, |
155 bool is_main_frame, | 131 bool is_main_frame, |
156 content::RenderViewHost* /* render_view_host */) { | 132 content::RenderViewHost* /* render_view_host */) { |
157 if (is_main_frame && !supports_instant_) | 133 if (is_main_frame && !supports_instant_) |
158 DetermineIfPageSupportsInstant(); | 134 DetermineInstantSupport(); |
159 } | 135 } |
160 | 136 |
161 bool InstantPage::OnMessageReceived(const IPC::Message& message) { | 137 bool InstantPage::OnMessageReceived(const IPC::Message& message) { |
162 bool handled = true; | 138 bool handled = true; |
163 IPC_BEGIN_MESSAGE_MAP(InstantPage, message) | 139 IPC_BEGIN_MESSAGE_MAP(InstantPage, message) |
164 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions) | 140 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxInstantSupportDetermined, |
165 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined, | |
166 OnInstantSupportDetermined) | 141 OnInstantSupportDetermined) |
167 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay, | 142 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxSetSuggestion, |
168 OnShowInstantOverlay) | 143 OnSetSuggestion) |
169 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox) | 144 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigateToURL, |
170 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_StartCapturingKeyStrokes, | 145 OnNavigateToURL) |
171 OnStartCapturingKeyStrokes); | 146 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxShowOverlay, OnShowOverlay) |
172 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_StopCapturingKeyStrokes, | 147 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxSetFocusState, |
173 OnStopCapturingKeyStrokes); | 148 OnSetFocusState) |
174 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate, | 149 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem, |
175 OnSearchBoxNavigate); | 150 OnDeleteMostVisitedItem) |
176 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantDeleteMostVisitedItem, | 151 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedItemDeletion, |
177 OnDeleteMostVisitedItem); | 152 OnUndoMostVisitedItemDeletion) |
178 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantUndoMostVisitedDeletion, | 153 IPC_MESSAGE_HANDLER( |
179 OnUndoMostVisitedDeletion); | 154 ChromeViewHostMsg_SearchBoxUndoAllMostVisitedItemDeletions, |
180 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantUndoAllMostVisitedDeletions, | 155 OnUndoAllMostVisitedItemDeletions) |
181 OnUndoAllMostVisitedDeletions); | |
182 IPC_MESSAGE_UNHANDLED(handled = false) | 156 IPC_MESSAGE_UNHANDLED(handled = false) |
183 IPC_END_MESSAGE_MAP() | 157 IPC_END_MESSAGE_MAP() |
184 return handled; | 158 return handled; |
185 } | 159 } |
186 | 160 |
187 void InstantPage::RenderViewGone(base::TerminationStatus /* status */) { | 161 void InstantPage::SendMessage(scoped_ptr<IPC::Message> message) { |
188 if (ShouldProcessRenderViewGone()) | 162 if (!contents()) |
189 delegate_->InstantPageRenderViewGone(contents()); | 163 return; |
190 } | 164 |
191 | 165 content::RenderProcessHost* process = contents()->GetRenderProcessHost(); |
192 void InstantPage::DidCommitProvisionalLoadForFrame( | 166 if (!process) |
193 int64 /* frame_id */, | 167 return; |
194 bool is_main_frame, | 168 |
195 const GURL& url, | 169 if (service_->IsInstantProcess(process->GetID())) |
196 content::PageTransition /* transition_type */, | 170 Send(message.release()); |
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 } | 171 } |
211 | 172 |
212 void InstantPage::OnInstantSupportDetermined(int page_id, | 173 void InstantPage::OnInstantSupportDetermined(int page_id, |
213 bool supports_instant) { | 174 bool supports_instant) { |
214 if (!contents()->IsActiveEntry(page_id) || supports_instant_) { | 175 if (contents()->IsActiveEntry(page_id) && !supports_instant_) { |
215 // Nothing to do if the page already supports Instant. | 176 supports_instant_ = supports_instant; |
216 return; | 177 delegate_->InstantSupportDetermined(contents()); |
217 } | 178 } |
218 | 179 } |
219 supports_instant_ = supports_instant; | 180 |
220 delegate_->InstantSupportDetermined(contents(), supports_instant); | 181 void InstantPage::OnSetSuggestion(int page_id, |
221 | 182 const InstantSuggestion& suggestion) { |
222 // If the page doesn't support Instant, stop listening to it. | 183 if (contents()->IsActiveEntry(page_id)) { |
223 if (!supports_instant) | 184 OnInstantSupportDetermined(page_id, true); |
224 Observe(NULL); | 185 delegate_->SetSuggestion(contents(), suggestion); |
225 } | 186 } |
226 | 187 } |
227 void InstantPage::OnShowInstantOverlay(int page_id, | 188 |
228 InstantShownReason reason, | 189 void InstantPage::OnNavigateToURL(int page_id, |
229 int height, | 190 const GURL& url, |
230 InstantSizeUnits units) { | 191 content::PageTransition transition, |
231 if (contents()->IsActiveEntry(page_id)) { | 192 WindowOpenDisposition disposition) { |
232 OnInstantSupportDetermined(page_id, true); | 193 if (contents()->IsActiveEntry(page_id)) { |
233 if (ShouldProcessShowInstantOverlay()) | 194 OnInstantSupportDetermined(page_id, true); |
234 delegate_->ShowInstantOverlay(contents(), reason, height, units); | 195 delegate_->NavigateToURL(contents(), url, transition, disposition); |
235 } | 196 } |
236 } | 197 } |
237 | 198 |
238 void InstantPage::OnFocusOmnibox(int page_id) { | 199 void InstantPage::OnShowOverlay(int page_id, |
239 if (contents()->IsActiveEntry(page_id)) { | 200 int height, |
240 OnInstantSupportDetermined(page_id, true); | 201 InstantSizeUnits height_units) { |
241 if (ShouldProcessFocusOmnibox()) | 202 if (contents()->IsActiveEntry(page_id)) { |
242 delegate_->FocusOmnibox(contents()); | 203 OnInstantSupportDetermined(page_id, true); |
243 } | 204 delegate_->ShowOverlay(contents(), height, height_units); |
244 } | 205 } |
245 | 206 } |
246 void InstantPage::OnStartCapturingKeyStrokes(int page_id) { | 207 |
247 if (contents()->IsActiveEntry(page_id)) { | 208 void InstantPage::OnSetFocusState(int page_id, OmniboxFocusState focus_state) { |
248 OnInstantSupportDetermined(page_id, true); | 209 if (contents()->IsActiveEntry(page_id)) { |
249 if (ShouldProcessStartCapturingKeyStrokes()) | 210 OnInstantSupportDetermined(page_id, true); |
250 delegate_->StartCapturingKeyStrokes(contents()); | 211 delegate_->SetFocusState(contents(), focus_state); |
251 } | 212 } |
252 } | 213 } |
253 | 214 |
254 void InstantPage::OnStopCapturingKeyStrokes(int page_id) { | 215 void InstantPage::OnDeleteMostVisitedItem(int page_id, const GURL& url) { |
255 if (contents()->IsActiveEntry(page_id)) { | 216 if (contents()->IsActiveEntry(page_id)) { |
256 OnInstantSupportDetermined(page_id, true); | 217 OnInstantSupportDetermined(page_id, true); |
257 if (ShouldProcessStopCapturingKeyStrokes()) | 218 delegate_->DeleteMostVisitedItem(contents(), url); |
258 delegate_->StopCapturingKeyStrokes(contents()); | 219 } |
259 } | 220 } |
260 } | 221 |
261 | 222 void InstantPage::OnUndoMostVisitedItemDeletion(int page_id, const GURL& url) { |
262 void InstantPage::OnSearchBoxNavigate(int page_id, | 223 if (contents()->IsActiveEntry(page_id)) { |
263 const GURL& url, | 224 OnInstantSupportDetermined(page_id, true); |
264 content::PageTransition transition, | 225 delegate_->UndoMostVisitedItemDeletion(contents(), url); |
265 WindowOpenDisposition disposition) { | 226 } |
266 if (contents()->IsActiveEntry(page_id)) { | 227 } |
267 OnInstantSupportDetermined(page_id, true); | 228 |
268 if (ShouldProcessNavigateToURL()) | 229 void InstantPage::OnUndoAllMostVisitedItemDeletions(int page_id) { |
269 delegate_->NavigateToURL(contents(), url, transition, disposition); | 230 if (contents()->IsActiveEntry(page_id)) { |
270 } | 231 OnInstantSupportDetermined(page_id, true); |
271 } | 232 delegate_->UndoAllMostVisitedItemDeletions(contents()); |
272 | 233 } |
273 void InstantPage::OnDeleteMostVisitedItem(const GURL& url) { | 234 } |
274 delegate_->DeleteMostVisitedItem(url); | |
275 } | |
276 | |
277 void InstantPage::OnUndoMostVisitedDeletion(const GURL& url) { | |
278 delegate_->UndoMostVisitedDeletion(url); | |
279 } | |
280 | |
281 void InstantPage::OnUndoAllMostVisitedDeletions() { | |
282 delegate_->UndoAllMostVisitedDeletions(); | |
283 } | |
OLD | NEW |