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/search/instant_service.h" | 5 #include "chrome/browser/search/instant_service.h" |
6 | 6 |
7 #include <vector> | |
8 | |
7 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
8 #include "chrome/browser/history/history_notifications.h" | 10 #include "chrome/browser/history/history_notifications.h" |
11 #include "chrome/browser/history/top_sites.h" | |
9 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/search/instant_io_context.h" | 13 #include "chrome/browser/search/instant_io_context.h" |
11 #include "chrome/browser/search/instant_service_factory.h" | 14 #include "chrome/browser/search/instant_service_factory.h" |
12 #include "chrome/browser/search/local_ntp_source.h" | 15 #include "chrome/browser/search/local_ntp_source.h" |
13 #include "chrome/browser/search/most_visited_iframe_source.h" | 16 #include "chrome/browser/search/most_visited_iframe_source.h" |
14 #include "chrome/browser/search/suggestion_iframe_source.h" | 17 #include "chrome/browser/search/suggestion_iframe_source.h" |
18 #include "chrome/browser/ui/browser.h" | |
19 #include "chrome/browser/ui/browser_finder.h" | |
20 #include "chrome/browser/ui/browser_instant_controller.h" | |
21 #include "chrome/browser/ui/host_desktop.h" | |
22 #include "chrome/browser/ui/search/instant_controller.h" | |
15 #include "chrome/browser/ui/webui/favicon_source.h" | 23 #include "chrome/browser/ui/webui/favicon_source.h" |
16 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" | 24 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" |
17 #include "chrome/browser/ui/webui/theme_source.h" | 25 #include "chrome/browser/ui/webui/theme_source.h" |
18 #include "chrome/common/chrome_notification_types.h" | 26 #include "chrome/common/chrome_notification_types.h" |
19 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
21 #include "content/public/browser/notification_types.h" | 29 #include "content/public/browser/notification_types.h" |
22 #include "content/public/browser/render_process_host.h" | 30 #include "content/public/browser/render_process_host.h" |
23 #include "content/public/browser/url_data_source.h" | 31 #include "content/public/browser/url_data_source.h" |
24 #include "googleurl/src/gurl.h" | 32 #include "googleurl/src/gurl.h" |
25 #include "net/url_request/url_request.h" | 33 #include "net/url_request/url_request.h" |
26 | 34 |
27 using content::BrowserThread; | 35 using content::BrowserThread; |
28 | 36 |
29 InstantService::InstantService(Profile* profile) | 37 InstantService::InstantService(Profile* profile) |
30 : profile_(profile), | 38 : profile_(profile), |
31 most_visited_item_cache_(kMaxInstantMostVisitedItemCacheSize) { | 39 most_visited_item_cache_(kMaxInstantMostVisitedItemCacheSize), |
40 weak_ptr_factory_(this) { | |
32 // Stub for unit tests. | 41 // Stub for unit tests. |
33 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) | 42 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) |
34 return; | 43 return; |
35 | 44 |
36 registrar_.Add(this, | 45 registrar_.Add(this, |
37 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 46 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
38 content::NotificationService::AllSources()); | 47 content::NotificationService::AllSources()); |
39 | 48 |
49 history::TopSites* top_sites = profile_->GetTopSites(); | |
50 if (top_sites) { | |
51 registrar_.Add(this, | |
52 chrome::NOTIFICATION_TOP_SITES_CHANGED, | |
53 content::Source<history::TopSites>(top_sites)); | |
54 } | |
40 instant_io_context_ = new InstantIOContext(); | 55 instant_io_context_ = new InstantIOContext(); |
41 | 56 |
42 if (profile_ && profile_->GetResourceContext()) { | 57 if (profile_ && profile_->GetResourceContext()) { |
43 BrowserThread::PostTask( | 58 BrowserThread::PostTask( |
44 BrowserThread::IO, FROM_HERE, | 59 BrowserThread::IO, FROM_HERE, |
45 base::Bind(&InstantIOContext::SetUserDataOnIO, | 60 base::Bind(&InstantIOContext::SetUserDataOnIO, |
46 profile->GetResourceContext(), instant_io_context_)); | 61 profile->GetResourceContext(), instant_io_context_)); |
47 } | 62 } |
48 | 63 |
49 // Set up the data sources that Instant uses on the NTP. | 64 // Set up the data sources that Instant uses on the NTP. |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
131 std::vector<InstantMostVisitedItemIDPair> items; | 146 std::vector<InstantMostVisitedItemIDPair> items; |
132 most_visited_item_cache_.GetCurrentItems(&items); | 147 most_visited_item_cache_.GetCurrentItems(&items); |
133 BrowserThread::PostTask( | 148 BrowserThread::PostTask( |
134 BrowserThread::IO, FROM_HERE, | 149 BrowserThread::IO, FROM_HERE, |
135 base::Bind(&InstantIOContext::AddMostVisitedItemsOnIO, | 150 base::Bind(&InstantIOContext::AddMostVisitedItemsOnIO, |
136 instant_io_context_, | 151 instant_io_context_, |
137 items)); | 152 items)); |
138 } | 153 } |
139 } | 154 } |
140 | 155 |
156 void InstantService::DeleteMostVisitedItem(const GURL& item_url) { | |
157 history::TopSites* top_sites = profile_->GetTopSites(); | |
158 if (!top_sites) | |
159 return; | |
160 | |
161 top_sites->AddBlacklistedURL(item_url); | |
162 } | |
163 | |
164 void InstantService::UndoMostVisitedDeletion( | |
165 const GURL& most_visited_item_url) { | |
Jered
2013/05/21 20:18:21
"item_url" or "url" would be clear to me.
kmadhusu
2013/05/23 18:26:22
most_visited_item_url => url. Fixed.
| |
166 history::TopSites* top_sites = profile_->GetTopSites(); | |
167 if (!top_sites) | |
168 return; | |
169 | |
170 top_sites->RemoveBlacklistedURL(most_visited_item_url); | |
171 } | |
172 | |
173 void InstantService::UndoAllMostVisitedDeletions() { | |
174 history::TopSites* top_sites = profile_->GetTopSites(); | |
175 if (!top_sites) | |
176 return; | |
177 | |
178 top_sites->ClearBlacklistedURLs(); | |
179 } | |
180 | |
141 void InstantService::GetCurrentMostVisitedItems( | 181 void InstantService::GetCurrentMostVisitedItems( |
142 std::vector<InstantMostVisitedItemIDPair>* items) const { | 182 std::vector<InstantMostVisitedItemIDPair>* items) const { |
143 most_visited_item_cache_.GetCurrentItems(items); | 183 most_visited_item_cache_.GetCurrentItems(items); |
144 } | 184 } |
145 | 185 |
146 bool InstantService::GetMostVisitedItemForID( | |
147 InstantRestrictedID most_visited_item_id, | |
148 InstantMostVisitedItem* item) const { | |
149 return most_visited_item_cache_.GetItemWithRestrictedID( | |
150 most_visited_item_id, item); | |
151 } | |
152 | |
153 void InstantService::Shutdown() { | 186 void InstantService::Shutdown() { |
154 process_ids_.clear(); | 187 process_ids_.clear(); |
155 | 188 |
156 if (instant_io_context_) { | 189 if (instant_io_context_) { |
157 BrowserThread::PostTask( | 190 BrowserThread::PostTask( |
158 BrowserThread::IO, FROM_HERE, | 191 BrowserThread::IO, FROM_HERE, |
159 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, | 192 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, |
160 instant_io_context_)); | 193 instant_io_context_)); |
161 } | 194 } |
162 instant_io_context_ = NULL; | 195 instant_io_context_ = NULL; |
163 } | 196 } |
164 | 197 |
165 void InstantService::Observe(int type, | 198 void InstantService::Observe(int type, |
166 const content::NotificationSource& source, | 199 const content::NotificationSource& source, |
167 const content::NotificationDetails& details) { | 200 const content::NotificationDetails& details) { |
168 switch (type) { | 201 switch (type) { |
169 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { | 202 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { |
170 int process_id = | 203 int process_id = |
171 content::Source<content::RenderProcessHost>(source)->GetID(); | 204 content::Source<content::RenderProcessHost>(source)->GetID(); |
172 process_ids_.erase(process_id); | 205 process_ids_.erase(process_id); |
173 | 206 |
174 if (instant_io_context_) { | 207 if (instant_io_context_) { |
175 BrowserThread::PostTask( | 208 BrowserThread::PostTask( |
176 BrowserThread::IO, FROM_HERE, | 209 BrowserThread::IO, FROM_HERE, |
177 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, | 210 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, |
178 instant_io_context_, process_id)); | 211 instant_io_context_, process_id)); |
179 } | 212 } |
180 break; | 213 break; |
181 } | 214 } |
215 case chrome::NOTIFICATION_TOP_SITES_CHANGED: { | |
216 history::TopSites* top_sites = profile_->GetTopSites(); | |
217 if (top_sites) { | |
218 top_sites->GetMostVisitedURLs( | |
219 base::Bind(&InstantService::OnMostVisitedItemsReceived, | |
220 weak_ptr_factory_.GetWeakPtr())); | |
221 } | |
222 break; | |
223 } | |
182 default: | 224 default: |
183 NOTREACHED() << "Unexpected notification type in InstantService."; | 225 NOTREACHED() << "Unexpected notification type in InstantService."; |
184 } | 226 } |
185 } | 227 } |
228 | |
229 bool InstantService::GetMostVisitedItemForID( | |
230 InstantRestrictedID most_visited_item_id, | |
231 InstantMostVisitedItem* item) const { | |
232 return most_visited_item_cache_.GetItemWithRestrictedID( | |
233 most_visited_item_id, item); | |
234 } | |
235 | |
236 void InstantService::OnMostVisitedItemsReceived( | |
237 const history::MostVisitedURLList& data) { | |
238 if (data.empty()) | |
239 return; | |
240 | |
241 std::vector<InstantMostVisitedItem> most_visited_items; | |
242 for (size_t i = 0; i < data.size(); i++) { | |
243 const history::MostVisitedURL& url = data[i]; | |
244 InstantMostVisitedItem item; | |
245 item.url = url.url; | |
246 item.title = url.title; | |
247 most_visited_items.push_back(item); | |
248 } | |
249 AddMostVisitedItems(most_visited_items); | |
250 SendMostVisitedItems(); | |
251 } | |
252 | |
253 void InstantService::SendMostVisitedItems() { | |
254 Browser* browser = chrome::FindBrowserWithProfile(profile_, | |
Jered
2013/05/21 20:18:21
Is it correct to send the update only to one brows
kmadhusu
2013/05/23 18:26:22
Good catch. Fixed to send updates to all the brows
| |
255 chrome::GetActiveDesktop()); | |
256 if (!browser || !browser->instant_controller()) | |
257 return; | |
258 | |
259 InstantController* controller = browser->instant_controller()->instant(); | |
260 if (!controller) | |
261 return; | |
262 | |
263 std::vector<InstantMostVisitedItemIDPair> items_with_ids; | |
Jered
2013/05/21 20:18:21
Instead, expose UpdateMostVisitedItems() and call
kmadhusu
2013/05/23 18:26:22
Done.
| |
264 GetCurrentMostVisitedItems(&items_with_ids); | |
265 controller->SendMostVisitedItems(items_with_ids); | |
266 } | |
OLD | NEW |