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::DetermineIfPageSupportsInstant() { | |
60 Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id())); | |
61 } | |
62 | |
63 void InstantPage::SendAutocompleteResults( | |
64 const std::vector<InstantAutocompleteResult>& results) { | |
65 Send(new ChromeViewMsg_SearchBoxAutocompleteResults(routing_id(), results)); | |
66 } | |
67 | |
68 void InstantPage::UpOrDownKeyPressed(int count) { | |
69 Send(new ChromeViewMsg_SearchBoxUpOrDownKeyPressed(routing_id(), count)); | |
70 } | |
71 | |
72 void InstantPage::CancelSelection(const string16& user_text) { | |
73 Send(new ChromeViewMsg_SearchBoxCancelSelection(routing_id(), user_text)); | |
74 } | |
75 | |
76 void InstantPage::SendThemeBackgroundInfo( | |
77 const ThemeBackgroundInfo& theme_info) { | |
78 Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info)); | |
79 } | |
80 | |
81 void InstantPage::SetDisplayInstantResults(bool display_instant_results) { | |
82 Send(new ChromeViewMsg_SearchBoxSetDisplayInstantResults( | |
83 routing_id(), display_instant_results)); | |
84 } | |
85 | |
86 void InstantPage::KeyCaptureChanged(bool is_key_capture_enabled) { | |
87 Send(new ChromeViewMsg_SearchBoxKeyCaptureChanged( | |
88 routing_id(), is_key_capture_enabled)); | |
89 } | |
90 | |
91 void InstantPage::SendMostVisitedItems( | |
92 const std::vector<MostVisitedItem>& items) { | |
93 Send(new ChromeViewMsg_InstantMostVisitedItemsChanged(routing_id(), items)); | |
94 } | |
95 | |
96 InstantPage::InstantPage(Delegate* delegate) | |
97 : delegate_(delegate), | |
98 supports_instant_(false) { | |
99 } | |
100 | |
101 void InstantPage::SetContents(content::WebContents* contents) { | 27 void InstantPage::SetContents(content::WebContents* contents) { |
102 Observe(contents); | 28 Observe(contents); |
103 } | 29 } |
104 | 30 |
105 bool InstantPage::ShouldProcessRenderViewCreated() { | 31 void InstantPage::DetermineInstantSupport() { |
106 return false; | 32 SendMessage(scoped_ptr<IPC::Message>( |
107 } | 33 new ChromeViewMsg_SearchBoxDetermineInstantSupport(routing_id()))); |
108 | 34 } |
109 bool InstantPage::ShouldProcessRenderViewGone() { | 35 |
110 return false; | 36 void InstantPage::Change(const string16& query, |
111 } | 37 bool verbatim, |
112 | 38 size_t selection_start, |
113 bool InstantPage::ShouldProcessAboutToNavigateMainFrame() { | 39 size_t selection_end) { |
114 return false; | 40 navigated_after_change_ = false; |
115 } | 41 SendMessage(scoped_ptr<IPC::Message>(new ChromeViewMsg_SearchBoxChange( |
116 | 42 routing_id(), query, verbatim, selection_start, selection_end))); |
117 bool InstantPage::ShouldProcessSetSuggestions() { | 43 } |
118 return false; | 44 |
119 } | 45 void InstantPage::Submit(const string16& query) { |
120 | 46 SendMessage(scoped_ptr<IPC::Message>(new ChromeViewMsg_SearchBoxSubmit( |
121 bool InstantPage::ShouldProcessShowInstantOverlay() { | 47 routing_id(), query))); |
122 return false; | 48 } |
123 } | 49 |
124 | 50 void InstantPage::AutocompleteResults( |
125 bool InstantPage::ShouldProcessStartCapturingKeyStrokes() { | 51 const std::vector<InstantAutocompleteResult>& results) { |
126 return false; | 52 SendMessage(scoped_ptr<IPC::Message>( |
127 } | 53 new ChromeViewMsg_SearchBoxAutocompleteResults(routing_id(), results))); |
128 | 54 } |
129 bool InstantPage::ShouldProcessStopCapturingKeyStrokes() { | 55 |
130 return false; | 56 void InstantPage::Select(int count) { |
131 } | 57 SendMessage(scoped_ptr<IPC::Message>(new ChromeViewMsg_SearchBoxSelect( |
132 | 58 routing_id(), count))); |
133 bool InstantPage::ShouldProcessNavigateToURL() { | 59 } |
134 return false; | 60 |
135 } | 61 void InstantPage::Cancel(const string16& query) { |
136 | 62 SendMessage(scoped_ptr<IPC::Message>(new ChromeViewMsg_SearchBoxCancel( |
137 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) { | 63 routing_id(), query))); |
138 if (ShouldProcessRenderViewCreated()) | 64 } |
139 delegate_->InstantPageRenderViewCreated(contents()); | 65 |
| 66 void InstantPage::Blur(const string16& query) { |
| 67 SendMessage(scoped_ptr<IPC::Message>(new ChromeViewMsg_SearchBoxBlur( |
| 68 routing_id(), query))); |
| 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, size_t font_size) { |
| 99 SendMessage(scoped_ptr<IPC::Message>(new ChromeViewMsg_SearchBoxFontChanged( |
| 100 routing_id(), font, 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::RenderViewGone(base::TerminationStatus /* status */) { |
| 109 delegate_->RenderViewGone(contents()); |
| 110 } |
| 111 |
| 112 void InstantPage::DidNavigateMainFrame( |
| 113 const content::LoadCommittedDetails& /* details */, |
| 114 const content::FrameNavigateParams& /* params */) { |
| 115 navigated_after_change_ = true; |
| 116 if (routing_id_ != routing_id()) { |
| 117 routing_id_ = routing_id(); |
| 118 delegate_->InitSearchBox(contents()); |
| 119 } |
140 } | 120 } |
141 | 121 |
142 void InstantPage::DidFinishLoad( | 122 void InstantPage::DidFinishLoad( |
143 int64 /* frame_id */, | 123 int64 /* frame_id */, |
144 const GURL& /* validated_url */, | 124 const GURL& /* validated_url */, |
145 bool is_main_frame, | 125 bool is_main_frame, |
146 content::RenderViewHost* /* render_view_host */) { | 126 content::RenderViewHost* /* render_view_host */) { |
147 if (is_main_frame && !supports_instant_) | 127 if (is_main_frame && !supports_instant_) |
148 DetermineIfPageSupportsInstant(); | 128 DetermineInstantSupport(); |
149 } | 129 } |
150 | 130 |
151 bool InstantPage::OnMessageReceived(const IPC::Message& message) { | 131 bool InstantPage::OnMessageReceived(const IPC::Message& message) { |
152 bool handled = true; | 132 bool handled = true; |
153 IPC_BEGIN_MESSAGE_MAP(InstantPage, message) | 133 IPC_BEGIN_MESSAGE_MAP(InstantPage, message) |
154 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions) | 134 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxInstantSupportDetermined, |
155 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined, | |
156 OnInstantSupportDetermined) | 135 OnInstantSupportDetermined) |
157 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay, | 136 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxSetSuggestion, |
158 OnShowInstantOverlay) | 137 OnSetSuggestion) |
159 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_StartCapturingKeyStrokes, | 138 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigateToURL, |
160 OnStartCapturingKeyStrokes); | 139 OnNavigateToURL) |
161 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_StopCapturingKeyStrokes, | 140 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxShowOverlay, OnShowOverlay) |
162 OnStopCapturingKeyStrokes); | 141 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxStartKeyCapture, |
163 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate, | 142 OnStartKeyCapture) |
164 OnSearchBoxNavigate); | 143 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxStopKeyCapture, |
165 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantDeleteMostVisitedItem, | 144 OnStopKeyCapture) |
166 OnDeleteMostVisitedItem); | 145 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem, |
167 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantUndoMostVisitedDeletion, | 146 OnDeleteMostVisitedItem) |
168 OnUndoMostVisitedDeletion); | 147 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedItemDeletion, |
169 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantUndoAllMostVisitedDeletions, | 148 OnUndoMostVisitedItemDeletion) |
170 OnUndoAllMostVisitedDeletions); | 149 IPC_MESSAGE_HANDLER( |
| 150 ChromeViewHostMsg_SearchBoxUndoAllMostVisitedItemDeletions, |
| 151 OnUndoAllMostVisitedItemDeletions) |
171 IPC_MESSAGE_UNHANDLED(handled = false) | 152 IPC_MESSAGE_UNHANDLED(handled = false) |
172 IPC_END_MESSAGE_MAP() | 153 IPC_END_MESSAGE_MAP() |
173 return handled; | 154 return handled; |
174 } | 155 } |
175 | 156 |
176 void InstantPage::RenderViewGone(base::TerminationStatus /* status */) { | 157 void InstantPage::SendMessage(scoped_ptr<IPC::Message> message) { |
177 if (ShouldProcessRenderViewGone()) | 158 if (!contents()) |
178 delegate_->InstantPageRenderViewGone(contents()); | 159 return; |
179 } | 160 |
180 | 161 content::RenderProcessHost* process = contents()->GetRenderProcessHost(); |
181 void InstantPage::DidCommitProvisionalLoadForFrame( | 162 if (!process) |
182 int64 /* frame_id */, | 163 return; |
183 bool is_main_frame, | 164 |
184 const GURL& url, | 165 if (service_->IsInstantProcess(process->GetID())) |
185 content::PageTransition /* transition_type */, | 166 Send(message.release()); |
186 content::RenderViewHost* /* render_view_host */) { | |
187 if (is_main_frame && ShouldProcessAboutToNavigateMainFrame()) | |
188 delegate_->InstantPageAboutToNavigateMainFrame(contents(), url); | |
189 } | |
190 | |
191 void InstantPage::OnSetSuggestions( | |
192 int page_id, | |
193 const std::vector<InstantSuggestion>& suggestions) { | |
194 if (contents()->IsActiveEntry(page_id)) { | |
195 OnInstantSupportDetermined(page_id, true); | |
196 if (ShouldProcessSetSuggestions()) | |
197 delegate_->SetSuggestions(contents(), suggestions); | |
198 } | |
199 } | 167 } |
200 | 168 |
201 void InstantPage::OnInstantSupportDetermined(int page_id, | 169 void InstantPage::OnInstantSupportDetermined(int page_id, |
202 bool supports_instant) { | 170 bool supports_instant) { |
203 if (!contents()->IsActiveEntry(page_id) || supports_instant_) { | 171 if (contents()->IsActiveEntry(page_id) && !supports_instant_) { |
204 // Nothing to do if the page already supports Instant. | 172 supports_instant_ = supports_instant; |
205 return; | 173 delegate_->InstantSupportDetermined(contents()); |
206 } | 174 } |
207 | 175 } |
208 supports_instant_ = supports_instant; | 176 |
209 delegate_->InstantSupportDetermined(contents(), supports_instant); | 177 void InstantPage::OnSetSuggestion(int page_id, |
210 | 178 const InstantSuggestion& suggestion) { |
211 // If the page doesn't support Instant, stop listening to it. | 179 if (contents()->IsActiveEntry(page_id)) { |
212 if (!supports_instant) | 180 OnInstantSupportDetermined(page_id, true); |
213 Observe(NULL); | 181 delegate_->SetSuggestion(contents(), suggestion); |
214 } | 182 } |
215 | 183 } |
216 void InstantPage::OnShowInstantOverlay(int page_id, | 184 |
217 InstantShownReason reason, | 185 void InstantPage::OnNavigateToURL(int page_id, |
218 int height, | 186 const GURL& url, |
219 InstantSizeUnits units) { | 187 content::PageTransition transition, |
220 if (contents()->IsActiveEntry(page_id)) { | 188 WindowOpenDisposition disposition) { |
221 OnInstantSupportDetermined(page_id, true); | 189 if (contents()->IsActiveEntry(page_id)) { |
222 if (ShouldProcessShowInstantOverlay()) | 190 OnInstantSupportDetermined(page_id, true); |
223 delegate_->ShowInstantOverlay(contents(), reason, height, units); | 191 delegate_->NavigateToURL(contents(), url, transition, disposition); |
224 } | 192 } |
225 } | 193 } |
226 | 194 |
227 void InstantPage::OnStartCapturingKeyStrokes(int page_id) { | 195 void InstantPage::OnShowOverlay(int page_id, |
228 if (contents()->IsActiveEntry(page_id)) { | 196 int height, |
229 OnInstantSupportDetermined(page_id, true); | 197 bool is_height_in_pixels) { |
230 if (ShouldProcessStartCapturingKeyStrokes()) | 198 if (contents()->IsActiveEntry(page_id)) { |
231 delegate_->StartCapturingKeyStrokes(contents()); | 199 OnInstantSupportDetermined(page_id, true); |
232 } | 200 delegate_->ShowOverlay(contents(), height, is_height_in_pixels); |
233 } | 201 } |
234 | 202 } |
235 void InstantPage::OnStopCapturingKeyStrokes(int page_id) { | 203 |
236 if (contents()->IsActiveEntry(page_id)) { | 204 void InstantPage::OnStartKeyCapture(int page_id) { |
237 OnInstantSupportDetermined(page_id, true); | 205 if (contents()->IsActiveEntry(page_id)) { |
238 if (ShouldProcessStopCapturingKeyStrokes()) | 206 OnInstantSupportDetermined(page_id, true); |
239 delegate_->StopCapturingKeyStrokes(contents()); | 207 delegate_->StartKeyCapture(contents()); |
240 } | 208 } |
241 } | 209 } |
242 | 210 |
243 void InstantPage::OnSearchBoxNavigate(int page_id, | 211 void InstantPage::OnStopKeyCapture(int page_id) { |
244 const GURL& url, | 212 if (contents()->IsActiveEntry(page_id)) { |
245 content::PageTransition transition, | 213 OnInstantSupportDetermined(page_id, true); |
246 WindowOpenDisposition disposition) { | 214 delegate_->StopKeyCapture(contents()); |
247 if (contents()->IsActiveEntry(page_id)) { | 215 } |
248 OnInstantSupportDetermined(page_id, true); | 216 } |
249 if (ShouldProcessNavigateToURL()) | 217 |
250 delegate_->NavigateToURL(contents(), url, transition, disposition); | 218 void InstantPage::OnDeleteMostVisitedItem(int page_id, const GURL& url) { |
251 } | 219 if (contents()->IsActiveEntry(page_id)) { |
252 } | 220 OnInstantSupportDetermined(page_id, true); |
253 | 221 delegate_->DeleteMostVisitedItem(contents(), url); |
254 void InstantPage::OnDeleteMostVisitedItem(const GURL& url) { | 222 } |
255 delegate_->DeleteMostVisitedItem(url); | 223 } |
256 } | 224 |
257 | 225 void InstantPage::OnUndoMostVisitedItemDeletion(int page_id, const GURL& url) { |
258 void InstantPage::OnUndoMostVisitedDeletion(const GURL& url) { | 226 if (contents()->IsActiveEntry(page_id)) { |
259 delegate_->UndoMostVisitedDeletion(url); | 227 OnInstantSupportDetermined(page_id, true); |
260 } | 228 delegate_->UndoMostVisitedItemDeletion(contents(), url); |
261 | 229 } |
262 void InstantPage::OnUndoAllMostVisitedDeletions() { | 230 } |
263 delegate_->UndoAllMostVisitedDeletions(); | 231 |
264 } | 232 void InstantPage::OnUndoAllMostVisitedItemDeletions(int page_id) { |
| 233 if (contents()->IsActiveEntry(page_id)) { |
| 234 OnInstantSupportDetermined(page_id, true); |
| 235 delegate_->UndoAllMostVisitedItemDeletions(contents()); |
| 236 } |
| 237 } |
OLD | NEW |