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> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "chrome/browser/history/history_notifications.h" | 12 #include "chrome/browser/history/history_notifications.h" |
| 13 #include "chrome/browser/history/top_sites.h" | 13 #include "chrome/browser/history/top_sites.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/search/instant_io_context.h" | 15 #include "chrome/browser/search/instant_io_context.h" |
| 16 #include "chrome/browser/search/instant_service_factory.h" | 16 #include "chrome/browser/search/instant_service_factory.h" |
| 17 #include "chrome/browser/search/local_ntp_source.h" | 17 #include "chrome/browser/search/local_ntp_source.h" |
| 18 #include "chrome/browser/search/most_visited_iframe_source.h" | 18 #include "chrome/browser/search/most_visited_iframe_source.h" |
| 19 #include "chrome/browser/search/search.h" | |
| 19 #include "chrome/browser/search/suggestion_iframe_source.h" | 20 #include "chrome/browser/search/suggestion_iframe_source.h" |
| 20 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
| 21 #include "chrome/browser/ui/browser_instant_controller.h" | 22 #include "chrome/browser/ui/browser_instant_controller.h" |
| 22 #include "chrome/browser/ui/browser_list.h" | 23 #include "chrome/browser/ui/browser_list.h" |
| 23 #include "chrome/browser/ui/host_desktop.h" | 24 #include "chrome/browser/ui/host_desktop.h" |
| 24 #include "chrome/browser/ui/search/instant_controller.h" | 25 #include "chrome/browser/ui/search/instant_controller.h" |
| 25 #include "chrome/browser/ui/webui/favicon_source.h" | 26 #include "chrome/browser/ui/webui/favicon_source.h" |
| 26 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" | 27 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" |
| 27 #include "chrome/browser/ui/webui/theme_source.h" | 28 #include "chrome/browser/ui/webui/theme_source.h" |
| 28 #include "chrome/common/chrome_notification_types.h" | 29 #include "chrome/common/chrome_notification_types.h" |
| 29 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
| 30 #include "content/public/browser/notification_service.h" | 31 #include "content/public/browser/notification_service.h" |
| 31 #include "content/public/browser/notification_types.h" | 32 #include "content/public/browser/notification_types.h" |
| 32 #include "content/public/browser/render_process_host.h" | 33 #include "content/public/browser/render_process_host.h" |
| 33 #include "content/public/browser/url_data_source.h" | 34 #include "content/public/browser/url_data_source.h" |
| 34 #include "googleurl/src/gurl.h" | 35 #include "googleurl/src/gurl.h" |
| 35 #include "net/url_request/url_request.h" | 36 #include "net/url_request/url_request.h" |
| 36 | 37 |
| 37 using content::BrowserThread; | 38 using content::BrowserThread; |
| 38 | 39 |
| 39 InstantService::InstantService(Profile* profile) | 40 InstantService::InstantService(Profile* profile) |
| 40 : profile_(profile), | 41 : profile_(profile), |
| 41 most_visited_item_cache_(kMaxInstantMostVisitedItemCacheSize), | |
| 42 weak_ptr_factory_(this) { | 42 weak_ptr_factory_(this) { |
| 43 // Stub for unit tests. | 43 // Stub for unit tests. |
| 44 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) | 44 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) |
| 45 return; | 45 return; |
| 46 | 46 |
| 47 registrar_.Add(this, | 47 registrar_.Add(this, |
| 48 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 48 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 49 content::NotificationService::AllSources()); | 49 content::NotificationService::AllSources()); |
| 50 | 50 |
| 51 history::TopSites* top_sites = profile_->GetTopSites(); | 51 history::TopSites* top_sites = profile_->GetTopSites(); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 71 content::URLDataSource::Add(profile, new FaviconSource( | 71 content::URLDataSource::Add(profile, new FaviconSource( |
| 72 profile, FaviconSource::FAVICON)); | 72 profile, FaviconSource::FAVICON)); |
| 73 content::URLDataSource::Add(profile, new LocalNtpSource()); | 73 content::URLDataSource::Add(profile, new LocalNtpSource()); |
| 74 content::URLDataSource::Add(profile, new SuggestionIframeSource()); | 74 content::URLDataSource::Add(profile, new SuggestionIframeSource()); |
| 75 content::URLDataSource::Add(profile, new MostVisitedIframeSource()); | 75 content::URLDataSource::Add(profile, new MostVisitedIframeSource()); |
| 76 } | 76 } |
| 77 | 77 |
| 78 InstantService::~InstantService() { | 78 InstantService::~InstantService() { |
| 79 } | 79 } |
| 80 | 80 |
| 81 // static | |
| 82 const std::string InstantService::MaybeTranslateInstantPathOnUI( | |
| 83 Profile* profile, const std::string& path) { | |
| 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 85 InstantService* instant_service = | |
| 86 InstantServiceFactory::GetForProfile(profile); | |
| 87 if (!instant_service) | |
| 88 return path; | |
| 89 | |
| 90 InstantRestrictedID restricted_id = 0; | |
| 91 DCHECK_EQ(sizeof(InstantRestrictedID), sizeof(int)); | |
| 92 if (base::StringToInt(path, &restricted_id)) { | |
| 93 InstantMostVisitedItem item; | |
| 94 if (instant_service->GetMostVisitedItemForID(restricted_id, &item)) | |
| 95 return item.url.spec(); | |
| 96 } | |
| 97 return path; | |
| 98 } | |
| 99 | |
| 100 const std::string InstantService::MaybeTranslateInstantPathOnIO( | |
| 101 const net::URLRequest* request, const std::string& path) { | |
| 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 103 | |
| 104 InstantRestrictedID restricted_id = 0; | |
| 105 DCHECK_EQ(sizeof(InstantRestrictedID), sizeof(int)); | |
| 106 if (base::StringToInt(path, &restricted_id)) { | |
| 107 GURL url; | |
| 108 if (InstantIOContext::GetURLForMostVisitedItemID(request, | |
| 109 restricted_id, | |
| 110 &url)) { | |
| 111 return url.spec(); | |
| 112 } | |
| 113 } | |
| 114 return path; | |
| 115 } | |
| 116 | |
| 117 // static | |
| 118 bool InstantService::IsInstantPath(const GURL& url) { | |
| 119 // Strip leading slash. | |
| 120 std::string path = url.path().substr(1); | |
| 121 | |
| 122 // Check that path is of Most Visited item ID form. | |
| 123 InstantRestrictedID dummy = 0; | |
| 124 return base::StringToInt(path, &dummy); | |
| 125 } | |
| 126 | |
| 127 void InstantService::AddInstantProcess(int process_id) { | 81 void InstantService::AddInstantProcess(int process_id) { |
| 128 process_ids_.insert(process_id); | 82 process_ids_.insert(process_id); |
| 129 | 83 |
| 130 if (instant_io_context_) { | 84 if (instant_io_context_) { |
| 131 BrowserThread::PostTask( | 85 BrowserThread::PostTask( |
| 132 BrowserThread::IO, FROM_HERE, | 86 BrowserThread::IO, FROM_HERE, |
| 133 base::Bind(&InstantIOContext::AddInstantProcessOnIO, | 87 base::Bind(&InstantIOContext::AddInstantProcessOnIO, |
| 134 instant_io_context_, process_id)); | 88 instant_io_context_, process_id)); |
| 135 } | 89 } |
| 136 } | 90 } |
| 137 | 91 |
| 138 bool InstantService::IsInstantProcess(int process_id) const { | 92 bool InstantService::IsInstantProcess(int process_id) const { |
| 139 return process_ids_.find(process_id) != process_ids_.end(); | 93 return process_ids_.find(process_id) != process_ids_.end(); |
| 140 } | 94 } |
| 141 | 95 |
| 142 void InstantService::AddMostVisitedItems( | |
| 143 const std::vector<InstantMostVisitedItem>& items) { | |
| 144 most_visited_item_cache_.AddItems(items); | |
| 145 | |
| 146 // Post task to the IO thread to copy the data. | |
| 147 if (instant_io_context_) { | |
| 148 std::vector<InstantMostVisitedItemIDPair> items; | |
| 149 most_visited_item_cache_.GetCurrentItems(&items); | |
| 150 BrowserThread::PostTask( | |
| 151 BrowserThread::IO, FROM_HERE, | |
| 152 base::Bind(&InstantIOContext::AddMostVisitedItemsOnIO, | |
| 153 instant_io_context_, | |
| 154 items)); | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 void InstantService::DeleteMostVisitedItem(const GURL& url) { | 96 void InstantService::DeleteMostVisitedItem(const GURL& url) { |
| 159 history::TopSites* top_sites = profile_->GetTopSites(); | 97 history::TopSites* top_sites = profile_->GetTopSites(); |
| 160 if (!top_sites) | 98 if (!top_sites) |
| 161 return; | 99 return; |
| 162 | 100 |
| 163 top_sites->AddBlacklistedURL(url); | 101 top_sites->AddBlacklistedURL(url); |
| 164 } | 102 } |
| 165 | 103 |
| 166 void InstantService::UndoMostVisitedDeletion(const GURL& url) { | 104 void InstantService::UndoMostVisitedDeletion(const GURL& url) { |
| 167 history::TopSites* top_sites = profile_->GetTopSites(); | 105 history::TopSites* top_sites = profile_->GetTopSites(); |
| 168 if (!top_sites) | 106 if (!top_sites) |
| 169 return; | 107 return; |
| 170 | 108 |
| 171 top_sites->RemoveBlacklistedURL(url); | 109 top_sites->RemoveBlacklistedURL(url); |
| 172 } | 110 } |
| 173 | 111 |
| 174 void InstantService::UndoAllMostVisitedDeletions() { | 112 void InstantService::UndoAllMostVisitedDeletions() { |
| 175 history::TopSites* top_sites = profile_->GetTopSites(); | 113 history::TopSites* top_sites = profile_->GetTopSites(); |
| 176 if (!top_sites) | 114 if (!top_sites) |
| 177 return; | 115 return; |
| 178 | 116 |
| 179 top_sites->ClearBlacklistedURLs(); | 117 top_sites->ClearBlacklistedURLs(); |
| 180 } | 118 } |
| 181 | 119 |
| 182 void InstantService::GetCurrentMostVisitedItems( | 120 void InstantService::GetCurrentMostVisitedItems( |
| 183 std::vector<InstantMostVisitedItemIDPair>* items) const { | 121 std::vector<InstantMostVisitedItem>* items) const { |
| 184 most_visited_item_cache_.GetCurrentItems(items); | 122 *items = most_visited_items_; |
| 185 } | 123 } |
| 186 | 124 |
| 187 void InstantService::Shutdown() { | 125 void InstantService::Shutdown() { |
| 188 process_ids_.clear(); | 126 process_ids_.clear(); |
| 189 | 127 |
| 190 if (instant_io_context_) { | 128 if (instant_io_context_) { |
| 191 BrowserThread::PostTask( | 129 BrowserThread::PostTask( |
| 192 BrowserThread::IO, FROM_HERE, | 130 BrowserThread::IO, FROM_HERE, |
| 193 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, | 131 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, |
| 194 instant_io_context_)); | 132 instant_io_context_)); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 220 base::Bind(&InstantService::OnMostVisitedItemsReceived, | 158 base::Bind(&InstantService::OnMostVisitedItemsReceived, |
| 221 weak_ptr_factory_.GetWeakPtr())); | 159 weak_ptr_factory_.GetWeakPtr())); |
| 222 } | 160 } |
| 223 break; | 161 break; |
| 224 } | 162 } |
| 225 default: | 163 default: |
| 226 NOTREACHED() << "Unexpected notification type in InstantService."; | 164 NOTREACHED() << "Unexpected notification type in InstantService."; |
| 227 } | 165 } |
| 228 } | 166 } |
| 229 | 167 |
| 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( | 168 void InstantService::OnMostVisitedItemsReceived( |
| 238 const history::MostVisitedURLList& data) { | 169 const history::MostVisitedURLList& data) { |
| 239 // Android doesn't use Browser/BrowserList. Do nothing for Android platform. | 170 // Android doesn't use Browser/BrowserList. Do nothing for Android platform. |
| 240 #if !defined(OS_ANDROID) | 171 #if !defined(OS_ANDROID) |
| 241 history::MostVisitedURLList reordered_data(data); | 172 history::MostVisitedURLList reordered_data(data); |
| 242 history::TopSites::MaybeShuffle(&reordered_data); | 173 history::TopSites::MaybeShuffle(&reordered_data); |
| 243 | 174 |
| 244 std::vector<InstantMostVisitedItem> most_visited_items; | 175 std::vector<InstantMostVisitedItem> most_visited_items; |
|
Jered
2013/05/31 17:34:33
Call this new_most_visited_items to make the disti
kmadhusu
2013/06/04 02:36:01
Done.
| |
| 245 for (size_t i = 0; i < reordered_data.size(); i++) { | 176 for (size_t i = 0; i < reordered_data.size(); i++) { |
| 246 const history::MostVisitedURL& url = reordered_data[i]; | 177 const history::MostVisitedURL& url = reordered_data[i]; |
| 247 InstantMostVisitedItem item; | 178 InstantMostVisitedItem item; |
| 248 item.url = url.url; | 179 item.url = url.url; |
| 249 item.title = url.title; | 180 item.title = url.title; |
| 250 most_visited_items.push_back(item); | 181 most_visited_items.push_back(item); |
| 251 } | 182 } |
| 252 AddMostVisitedItems(most_visited_items); | 183 if (chrome::AreMostVisitedItemsEqual(most_visited_items, |
| 184 most_visited_items_)) | |
| 185 return; | |
| 186 | |
| 187 most_visited_items_ = most_visited_items; | |
| 253 | 188 |
| 254 const BrowserList* browser_list = | 189 const BrowserList* browser_list = |
| 255 BrowserList::GetInstance(chrome::GetActiveDesktop()); | 190 BrowserList::GetInstance(chrome::GetActiveDesktop()); |
| 256 for (BrowserList::const_iterator it = browser_list->begin(); | 191 for (BrowserList::const_iterator it = browser_list->begin(); |
| 257 it != browser_list->end(); ++it) { | 192 it != browser_list->end(); ++it) { |
| 258 if ((*it)->profile() != profile_ || !((*it)->instant_controller())) | 193 if ((*it)->profile() != profile_ || !((*it)->instant_controller())) |
| 259 continue; | 194 continue; |
| 260 | 195 |
| 261 InstantController* controller = (*it)->instant_controller()->instant(); | 196 InstantController* controller = (*it)->instant_controller()->instant(); |
| 262 if (!controller) | 197 if (!controller) |
| 263 continue; | 198 continue; |
| 264 controller->UpdateMostVisitedItems(); | 199 controller->UpdateMostVisitedItems(); |
| 265 } | 200 } |
| 266 #endif | 201 #endif |
| 267 } | 202 } |
| OLD | NEW |