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 |
| 9 #include "base/logging.h" |
7 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "build/build_config.h" |
8 #include "chrome/browser/history/history_notifications.h" | 12 #include "chrome/browser/history/history_notifications.h" |
| 13 #include "chrome/browser/history/top_sites.h" |
9 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/search/instant_io_context.h" | 15 #include "chrome/browser/search/instant_io_context.h" |
11 #include "chrome/browser/search/instant_service_factory.h" | 16 #include "chrome/browser/search/instant_service_factory.h" |
12 #include "chrome/browser/search/local_ntp_source.h" | 17 #include "chrome/browser/search/local_ntp_source.h" |
13 #include "chrome/browser/search/most_visited_iframe_source.h" | 18 #include "chrome/browser/search/most_visited_iframe_source.h" |
14 #include "chrome/browser/search/suggestion_iframe_source.h" | 19 #include "chrome/browser/search/suggestion_iframe_source.h" |
| 20 #include "chrome/browser/ui/browser.h" |
| 21 #include "chrome/browser/ui/browser_instant_controller.h" |
| 22 #include "chrome/browser/ui/browser_list.h" |
| 23 #include "chrome/browser/ui/host_desktop.h" |
| 24 #include "chrome/browser/ui/search/instant_controller.h" |
15 #include "chrome/browser/ui/webui/favicon_source.h" | 25 #include "chrome/browser/ui/webui/favicon_source.h" |
16 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" | 26 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" |
17 #include "chrome/browser/ui/webui/theme_source.h" | 27 #include "chrome/browser/ui/webui/theme_source.h" |
18 #include "chrome/common/chrome_notification_types.h" | 28 #include "chrome/common/chrome_notification_types.h" |
19 #include "content/public/browser/browser_thread.h" | 29 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/notification_service.h" | 30 #include "content/public/browser/notification_service.h" |
21 #include "content/public/browser/notification_types.h" | 31 #include "content/public/browser/notification_types.h" |
22 #include "content/public/browser/render_process_host.h" | 32 #include "content/public/browser/render_process_host.h" |
23 #include "content/public/browser/url_data_source.h" | 33 #include "content/public/browser/url_data_source.h" |
24 #include "googleurl/src/gurl.h" | 34 #include "googleurl/src/gurl.h" |
25 #include "net/url_request/url_request.h" | 35 #include "net/url_request/url_request.h" |
26 | 36 |
27 using content::BrowserThread; | 37 using content::BrowserThread; |
28 | 38 |
29 InstantService::InstantService(Profile* profile) | 39 InstantService::InstantService(Profile* profile) |
30 : profile_(profile), | 40 : profile_(profile), |
31 most_visited_item_cache_(kMaxInstantMostVisitedItemCacheSize) { | 41 most_visited_item_cache_(kMaxInstantMostVisitedItemCacheSize), |
| 42 weak_ptr_factory_(this) { |
32 // Stub for unit tests. | 43 // Stub for unit tests. |
33 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) | 44 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) |
34 return; | 45 return; |
35 | 46 |
36 registrar_.Add(this, | 47 registrar_.Add(this, |
37 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 48 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
38 content::NotificationService::AllSources()); | 49 content::NotificationService::AllSources()); |
39 | 50 |
| 51 history::TopSites* top_sites = profile_->GetTopSites(); |
| 52 if (top_sites) { |
| 53 registrar_.Add(this, |
| 54 chrome::NOTIFICATION_TOP_SITES_CHANGED, |
| 55 content::Source<history::TopSites>(top_sites)); |
| 56 } |
40 instant_io_context_ = new InstantIOContext(); | 57 instant_io_context_ = new InstantIOContext(); |
41 | 58 |
42 if (profile_ && profile_->GetResourceContext()) { | 59 if (profile_ && profile_->GetResourceContext()) { |
43 BrowserThread::PostTask( | 60 BrowserThread::PostTask( |
44 BrowserThread::IO, FROM_HERE, | 61 BrowserThread::IO, FROM_HERE, |
45 base::Bind(&InstantIOContext::SetUserDataOnIO, | 62 base::Bind(&InstantIOContext::SetUserDataOnIO, |
46 profile->GetResourceContext(), instant_io_context_)); | 63 profile->GetResourceContext(), instant_io_context_)); |
47 } | 64 } |
48 | 65 |
49 // Set up the data sources that Instant uses on the NTP. | 66 // Set up the data sources that Instant uses on the NTP. |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 std::vector<InstantMostVisitedItemIDPair> items; | 149 std::vector<InstantMostVisitedItemIDPair> items; |
133 most_visited_item_cache_.GetCurrentItems(&items); | 150 most_visited_item_cache_.GetCurrentItems(&items); |
134 BrowserThread::PostTask( | 151 BrowserThread::PostTask( |
135 BrowserThread::IO, FROM_HERE, | 152 BrowserThread::IO, FROM_HERE, |
136 base::Bind(&InstantIOContext::AddMostVisitedItemsOnIO, | 153 base::Bind(&InstantIOContext::AddMostVisitedItemsOnIO, |
137 instant_io_context_, | 154 instant_io_context_, |
138 items)); | 155 items)); |
139 } | 156 } |
140 } | 157 } |
141 | 158 |
| 159 void InstantService::DeleteMostVisitedItem(const GURL& url) { |
| 160 history::TopSites* top_sites = profile_->GetTopSites(); |
| 161 if (!top_sites) |
| 162 return; |
| 163 |
| 164 top_sites->AddBlacklistedURL(url); |
| 165 } |
| 166 |
| 167 void InstantService::UndoMostVisitedDeletion(const GURL& url) { |
| 168 history::TopSites* top_sites = profile_->GetTopSites(); |
| 169 if (!top_sites) |
| 170 return; |
| 171 |
| 172 top_sites->RemoveBlacklistedURL(url); |
| 173 } |
| 174 |
| 175 void InstantService::UndoAllMostVisitedDeletions() { |
| 176 history::TopSites* top_sites = profile_->GetTopSites(); |
| 177 if (!top_sites) |
| 178 return; |
| 179 |
| 180 top_sites->ClearBlacklistedURLs(); |
| 181 } |
| 182 |
142 void InstantService::GetCurrentMostVisitedItems( | 183 void InstantService::GetCurrentMostVisitedItems( |
143 std::vector<InstantMostVisitedItemIDPair>* items) const { | 184 std::vector<InstantMostVisitedItemIDPair>* items) const { |
144 most_visited_item_cache_.GetCurrentItems(items); | 185 most_visited_item_cache_.GetCurrentItems(items); |
145 } | 186 } |
146 | 187 |
147 bool InstantService::GetMostVisitedItemForID( | |
148 InstantRestrictedID most_visited_item_id, | |
149 InstantMostVisitedItem* item) const { | |
150 return most_visited_item_cache_.GetItemWithRestrictedID( | |
151 most_visited_item_id, item); | |
152 } | |
153 | |
154 void InstantService::Shutdown() { | 188 void InstantService::Shutdown() { |
155 process_ids_.clear(); | 189 process_ids_.clear(); |
156 | 190 |
157 if (instant_io_context_.get()) { | 191 if (instant_io_context_.get()) { |
158 BrowserThread::PostTask( | 192 BrowserThread::PostTask( |
159 BrowserThread::IO, | 193 BrowserThread::IO, |
160 FROM_HERE, | 194 FROM_HERE, |
161 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, | 195 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, |
162 instant_io_context_)); | 196 instant_io_context_)); |
163 } | 197 } |
(...skipping 12 matching lines...) Expand all Loading... |
176 if (instant_io_context_.get()) { | 210 if (instant_io_context_.get()) { |
177 BrowserThread::PostTask( | 211 BrowserThread::PostTask( |
178 BrowserThread::IO, | 212 BrowserThread::IO, |
179 FROM_HERE, | 213 FROM_HERE, |
180 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, | 214 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, |
181 instant_io_context_, | 215 instant_io_context_, |
182 process_id)); | 216 process_id)); |
183 } | 217 } |
184 break; | 218 break; |
185 } | 219 } |
| 220 case chrome::NOTIFICATION_TOP_SITES_CHANGED: { |
| 221 history::TopSites* top_sites = profile_->GetTopSites(); |
| 222 if (top_sites) { |
| 223 top_sites->GetMostVisitedURLs( |
| 224 base::Bind(&InstantService::OnMostVisitedItemsReceived, |
| 225 weak_ptr_factory_.GetWeakPtr())); |
| 226 } |
| 227 break; |
| 228 } |
186 default: | 229 default: |
187 NOTREACHED() << "Unexpected notification type in InstantService."; | 230 NOTREACHED() << "Unexpected notification type in InstantService."; |
188 } | 231 } |
189 } | 232 } |
| 233 |
| 234 bool InstantService::GetMostVisitedItemForID( |
| 235 InstantRestrictedID most_visited_item_id, |
| 236 InstantMostVisitedItem* item) const { |
| 237 return most_visited_item_cache_.GetItemWithRestrictedID( |
| 238 most_visited_item_id, item); |
| 239 } |
| 240 |
| 241 void InstantService::OnMostVisitedItemsReceived( |
| 242 const history::MostVisitedURLList& data) { |
| 243 // Android doesn't use Browser/BrowserList. Do nothing for Android platform. |
| 244 #if !defined(OS_ANDROID) |
| 245 history::MostVisitedURLList reordered_data(data); |
| 246 history::TopSites::MaybeShuffle(&reordered_data); |
| 247 |
| 248 std::vector<InstantMostVisitedItem> most_visited_items; |
| 249 for (size_t i = 0; i < reordered_data.size(); i++) { |
| 250 const history::MostVisitedURL& url = reordered_data[i]; |
| 251 InstantMostVisitedItem item; |
| 252 item.url = url.url; |
| 253 item.title = url.title; |
| 254 most_visited_items.push_back(item); |
| 255 } |
| 256 AddMostVisitedItems(most_visited_items); |
| 257 |
| 258 const BrowserList* browser_list = |
| 259 BrowserList::GetInstance(chrome::GetActiveDesktop()); |
| 260 for (BrowserList::const_iterator it = browser_list->begin(); |
| 261 it != browser_list->end(); ++it) { |
| 262 if ((*it)->profile() != profile_ || !((*it)->instant_controller())) |
| 263 continue; |
| 264 |
| 265 InstantController* controller = (*it)->instant_controller()->instant(); |
| 266 if (!controller) |
| 267 continue; |
| 268 // TODO(kmadhusu): It would be cleaner to have each InstantController |
| 269 // register itself as an InstantServiceObserver and push out updates that |
| 270 // way. Refer to crbug.com/246355 for more details. |
| 271 controller->UpdateMostVisitedItems(); |
| 272 } |
| 273 #endif |
| 274 } |
OLD | NEW |