Chromium Code Reviews| 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) { | |
|
sreeram
2013/06/03 20:40:42
Nit: This works for now, because the InstantServic
kmadhusu
2013/06/03 21:40:49
Filed crbug.com/246351.
| |
| 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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 131 std::vector<InstantMostVisitedItemIDPair> items; | 148 std::vector<InstantMostVisitedItemIDPair> items; |
| 132 most_visited_item_cache_.GetCurrentItems(&items); | 149 most_visited_item_cache_.GetCurrentItems(&items); |
| 133 BrowserThread::PostTask( | 150 BrowserThread::PostTask( |
| 134 BrowserThread::IO, FROM_HERE, | 151 BrowserThread::IO, FROM_HERE, |
| 135 base::Bind(&InstantIOContext::AddMostVisitedItemsOnIO, | 152 base::Bind(&InstantIOContext::AddMostVisitedItemsOnIO, |
| 136 instant_io_context_, | 153 instant_io_context_, |
| 137 items)); | 154 items)); |
| 138 } | 155 } |
| 139 } | 156 } |
| 140 | 157 |
| 158 void InstantService::DeleteMostVisitedItem(const GURL& url) { | |
| 159 history::TopSites* top_sites = profile_->GetTopSites(); | |
| 160 if (!top_sites) | |
| 161 return; | |
| 162 | |
| 163 top_sites->AddBlacklistedURL(url); | |
| 164 } | |
| 165 | |
| 166 void InstantService::UndoMostVisitedDeletion(const GURL& url) { | |
| 167 history::TopSites* top_sites = profile_->GetTopSites(); | |
| 168 if (!top_sites) | |
| 169 return; | |
| 170 | |
| 171 top_sites->RemoveBlacklistedURL(url); | |
| 172 } | |
| 173 | |
| 174 void InstantService::UndoAllMostVisitedDeletions() { | |
| 175 history::TopSites* top_sites = profile_->GetTopSites(); | |
| 176 if (!top_sites) | |
| 177 return; | |
| 178 | |
| 179 top_sites->ClearBlacklistedURLs(); | |
| 180 } | |
| 181 | |
| 141 void InstantService::GetCurrentMostVisitedItems( | 182 void InstantService::GetCurrentMostVisitedItems( |
| 142 std::vector<InstantMostVisitedItemIDPair>* items) const { | 183 std::vector<InstantMostVisitedItemIDPair>* items) const { |
| 143 most_visited_item_cache_.GetCurrentItems(items); | 184 most_visited_item_cache_.GetCurrentItems(items); |
| 144 } | 185 } |
| 145 | 186 |
| 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() { | 187 void InstantService::Shutdown() { |
| 154 process_ids_.clear(); | 188 process_ids_.clear(); |
| 155 | 189 |
| 156 if (instant_io_context_) { | 190 if (instant_io_context_) { |
| 157 BrowserThread::PostTask( | 191 BrowserThread::PostTask( |
| 158 BrowserThread::IO, FROM_HERE, | 192 BrowserThread::IO, FROM_HERE, |
| 159 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, | 193 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, |
| 160 instant_io_context_)); | 194 instant_io_context_)); |
| 161 } | 195 } |
| 162 instant_io_context_ = NULL; | 196 instant_io_context_ = NULL; |
| 163 } | 197 } |
| 164 | 198 |
| 165 void InstantService::Observe(int type, | 199 void InstantService::Observe(int type, |
| 166 const content::NotificationSource& source, | 200 const content::NotificationSource& source, |
| 167 const content::NotificationDetails& details) { | 201 const content::NotificationDetails& details) { |
| 168 switch (type) { | 202 switch (type) { |
| 169 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { | 203 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { |
| 170 int process_id = | 204 int process_id = |
| 171 content::Source<content::RenderProcessHost>(source)->GetID(); | 205 content::Source<content::RenderProcessHost>(source)->GetID(); |
| 172 process_ids_.erase(process_id); | 206 process_ids_.erase(process_id); |
| 173 | 207 |
| 174 if (instant_io_context_) { | 208 if (instant_io_context_) { |
| 175 BrowserThread::PostTask( | 209 BrowserThread::PostTask( |
| 176 BrowserThread::IO, FROM_HERE, | 210 BrowserThread::IO, FROM_HERE, |
| 177 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, | 211 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, |
| 178 instant_io_context_, process_id)); | 212 instant_io_context_, process_id)); |
| 179 } | 213 } |
| 180 break; | 214 break; |
| 181 } | 215 } |
| 216 case chrome::NOTIFICATION_TOP_SITES_CHANGED: { | |
| 217 history::TopSites* top_sites = profile_->GetTopSites(); | |
| 218 if (top_sites) { | |
| 219 top_sites->GetMostVisitedURLs( | |
| 220 base::Bind(&InstantService::OnMostVisitedItemsReceived, | |
| 221 weak_ptr_factory_.GetWeakPtr())); | |
| 222 } | |
| 223 break; | |
| 224 } | |
| 182 default: | 225 default: |
| 183 NOTREACHED() << "Unexpected notification type in InstantService."; | 226 NOTREACHED() << "Unexpected notification type in InstantService."; |
| 184 } | 227 } |
| 185 } | 228 } |
| 229 | |
| 230 bool InstantService::GetMostVisitedItemForID( | |
| 231 InstantRestrictedID most_visited_item_id, | |
| 232 InstantMostVisitedItem* item) const { | |
| 233 return most_visited_item_cache_.GetItemWithRestrictedID( | |
| 234 most_visited_item_id, item); | |
| 235 } | |
| 236 | |
| 237 void InstantService::OnMostVisitedItemsReceived( | |
| 238 const history::MostVisitedURLList& data) { | |
| 239 // Android doesn't use Browser/BrowserList. Do nothing for Android platform. | |
| 240 #if !defined(OS_ANDROID) | |
| 241 history::MostVisitedURLList reordered_data(data); | |
| 242 history::TopSites::MaybeShuffle(&reordered_data); | |
| 243 | |
| 244 std::vector<InstantMostVisitedItem> most_visited_items; | |
| 245 for (size_t i = 0; i < reordered_data.size(); i++) { | |
| 246 const history::MostVisitedURL& url = reordered_data[i]; | |
| 247 InstantMostVisitedItem item; | |
| 248 item.url = url.url; | |
| 249 item.title = url.title; | |
| 250 most_visited_items.push_back(item); | |
| 251 } | |
| 252 AddMostVisitedItems(most_visited_items); | |
| 253 | |
| 254 const BrowserList* browser_list = | |
| 255 BrowserList::GetInstance(chrome::GetActiveDesktop()); | |
| 256 for (BrowserList::const_iterator it = browser_list->begin(); | |
| 257 it != browser_list->end(); ++it) { | |
| 258 if ((*it)->profile() != profile_ || !((*it)->instant_controller())) | |
| 259 continue; | |
| 260 | |
| 261 InstantController* controller = (*it)->instant_controller()->instant(); | |
| 262 if (!controller) | |
| 263 continue; | |
| 264 controller->UpdateMostVisitedItems(); | |
| 265 } | |
|
sreeram
2013/06/03 20:40:42
This is okay for now. But I think it would be clea
kmadhusu
2013/06/03 21:40:49
Filed crbug.com/246355.
| |
| 266 #endif | |
| 267 } | |
| OLD | NEW |