| 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 "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "chrome/browser/history/history_notifications.h" | 8 #include "chrome/browser/history/history_notifications.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/search/instant_io_context.h" | 10 #include "chrome/browser/search/instant_io_context.h" |
| 11 #include "chrome/browser/search/instant_service_factory.h" | 11 #include "chrome/browser/search/instant_service_factory.h" |
| 12 #include "chrome/browser/search/local_omnibox_popup_source.h" | 12 #include "chrome/browser/search/local_omnibox_popup_source.h" |
| 13 #include "chrome/browser/ui/webui/favicon_source.h" | 13 #include "chrome/browser/ui/webui/favicon_source.h" |
| 14 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" | 14 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" |
| 15 #include "chrome/browser/ui/webui/theme_source.h" | 15 #include "chrome/browser/ui/webui/theme_source.h" |
| 16 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
| 19 #include "content/public/browser/notification_types.h" | 19 #include "content/public/browser/notification_types.h" |
| 20 #include "content/public/browser/render_process_host.h" | 20 #include "content/public/browser/render_process_host.h" |
| 21 #include "content/public/browser/url_data_source.h" | 21 #include "content/public/browser/url_data_source.h" |
| 22 #include "googleurl/src/gurl.h" | 22 #include "googleurl/src/gurl.h" |
| 23 #include "net/url_request/url_request.h" | 23 #include "net/url_request/url_request.h" |
| 24 | 24 |
| 25 using content::BrowserThread; | 25 using content::BrowserThread; |
| 26 | 26 |
| 27 namespace { | |
| 28 | |
| 29 // Copies deleted urls out of the history data structure |details| into a | |
| 30 // straight vector of GURLs. | |
| 31 void HistoryDetailsToDeletedURLs(const history::URLsDeletedDetails& details, | |
| 32 std::vector<GURL>* deleted_urls) { | |
| 33 for (history::URLRows::const_iterator it = details.rows.begin(); | |
| 34 it != details.rows.end(); | |
| 35 ++it) { | |
| 36 deleted_urls->push_back(it->url()); | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 InstantService::InstantService(Profile* profile) | 27 InstantService::InstantService(Profile* profile) |
| 43 : profile_(profile), | 28 : profile_(profile), |
| 44 last_most_visited_item_id_(0) { | 29 most_visited_item_cache_(kMaxInstantMostVisitedItemCacheSize) { |
| 45 // Stub for unit tests. | 30 // Stub for unit tests. |
| 46 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) | 31 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) |
| 47 return; | 32 return; |
| 48 | 33 |
| 49 registrar_.Add(this, | 34 registrar_.Add(this, |
| 50 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 35 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 51 content::NotificationService::AllSources()); | 36 content::NotificationService::AllSources()); |
| 52 registrar_.Add(this, | |
| 53 chrome::NOTIFICATION_HISTORY_URLS_DELETED, | |
| 54 content::NotificationService::AllSources()); | |
| 55 | 37 |
| 56 instant_io_context_ = new InstantIOContext(); | 38 instant_io_context_ = new InstantIOContext(); |
| 57 | 39 |
| 58 if (profile_ && profile_->GetResourceContext()) { | 40 if (profile_ && profile_->GetResourceContext()) { |
| 59 BrowserThread::PostTask( | 41 BrowserThread::PostTask( |
| 60 BrowserThread::IO, FROM_HERE, | 42 BrowserThread::IO, FROM_HERE, |
| 61 base::Bind(&InstantIOContext::SetUserDataOnIO, | 43 base::Bind(&InstantIOContext::SetUserDataOnIO, |
| 62 profile->GetResourceContext(), instant_io_context_)); | 44 profile->GetResourceContext(), instant_io_context_)); |
| 63 } | 45 } |
| 64 | 46 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 77 | 59 |
| 78 // static | 60 // static |
| 79 const std::string InstantService::MaybeTranslateInstantPathOnUI( | 61 const std::string InstantService::MaybeTranslateInstantPathOnUI( |
| 80 Profile* profile, const std::string& path) { | 62 Profile* profile, const std::string& path) { |
| 81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 82 InstantService* instant_service = | 64 InstantService* instant_service = |
| 83 InstantServiceFactory::GetForProfile(profile); | 65 InstantServiceFactory::GetForProfile(profile); |
| 84 if (!instant_service) | 66 if (!instant_service) |
| 85 return path; | 67 return path; |
| 86 | 68 |
| 87 uint64 most_visited_item_id = 0; | 69 InstantRestrictedID restricted_id = 0; |
| 88 if (base::StringToUint64(path, &most_visited_item_id)) { | 70 DCHECK_EQ(sizeof(InstantRestrictedID), sizeof(int)); |
| 89 GURL url; | 71 if (base::StringToInt(path, &restricted_id)) { |
| 90 if (instant_service->GetURLForMostVisitedItemId(most_visited_item_id, &url)) | 72 InstantMostVisitedItem item; |
| 91 return url.spec(); | 73 if (instant_service->GetMostVisitedItemForID(restricted_id, &item)) |
| 74 return item.url.spec(); |
| 92 } | 75 } |
| 93 return path; | 76 return path; |
| 94 } | 77 } |
| 95 | 78 |
| 96 const std::string InstantService::MaybeTranslateInstantPathOnIO( | 79 const std::string InstantService::MaybeTranslateInstantPathOnIO( |
| 97 const net::URLRequest* request, const std::string& path) { | 80 const net::URLRequest* request, const std::string& path) { |
| 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 99 uint64 most_visited_item_id = 0; | 82 |
| 100 if (base::StringToUint64(path, &most_visited_item_id)) { | 83 InstantRestrictedID restricted_id = 0; |
| 84 DCHECK_EQ(sizeof(InstantRestrictedID), sizeof(int)); |
| 85 if (base::StringToInt(path, &restricted_id)) { |
| 101 GURL url; | 86 GURL url; |
| 102 if (InstantIOContext::GetURLForMostVisitedItemId(request, | 87 if (InstantIOContext::GetURLForMostVisitedItemID(request, |
| 103 most_visited_item_id, | 88 restricted_id, |
| 104 &url)) | 89 &url)) { |
| 105 return url.spec(); | 90 return url.spec(); |
| 91 } |
| 106 } | 92 } |
| 107 return path; | 93 return path; |
| 108 } | 94 } |
| 109 | 95 |
| 110 // static | 96 // static |
| 111 bool InstantService::IsInstantPath(const GURL& url) { | 97 bool InstantService::IsInstantPath(const GURL& url) { |
| 112 // Strip leading slash. | 98 // Strip leading slash. |
| 113 std::string path = url.path().substr(1); | 99 std::string path = url.path().substr(1); |
| 114 | 100 |
| 115 // Check that path is of Most Visited item ID form. | 101 // Check that path is of Most Visited item ID form. |
| 116 uint64 dummy = 0; | 102 InstantRestrictedID dummy = 0; |
| 117 return base::StringToUint64(path, &dummy); | 103 return base::StringToInt(path, &dummy); |
| 118 } | 104 } |
| 119 | 105 |
| 120 void InstantService::AddInstantProcess(int process_id) { | 106 void InstantService::AddInstantProcess(int process_id) { |
| 121 process_ids_.insert(process_id); | 107 process_ids_.insert(process_id); |
| 122 | 108 |
| 123 if (instant_io_context_) { | 109 if (instant_io_context_) { |
| 124 BrowserThread::PostTask( | 110 BrowserThread::PostTask( |
| 125 BrowserThread::IO, FROM_HERE, | 111 BrowserThread::IO, FROM_HERE, |
| 126 base::Bind(&InstantIOContext::AddInstantProcessOnIO, | 112 base::Bind(&InstantIOContext::AddInstantProcessOnIO, |
| 127 instant_io_context_, process_id)); | 113 instant_io_context_, process_id)); |
| 128 } | 114 } |
| 129 } | 115 } |
| 130 | 116 |
| 131 bool InstantService::IsInstantProcess(int process_id) const { | 117 bool InstantService::IsInstantProcess(int process_id) const { |
| 132 return process_ids_.find(process_id) != process_ids_.end(); | 118 return process_ids_.find(process_id) != process_ids_.end(); |
| 133 } | 119 } |
| 134 | 120 |
| 135 uint64 InstantService::AddURL(const GURL& url) { | 121 void InstantService::AddMostVisitedItems( |
| 136 uint64 id = 0; | 122 const std::vector<InstantMostVisitedItem>& items) { |
| 137 if (GetMostVisitedItemIDForURL(url, &id)) | 123 most_visited_item_cache_.AddItems(items); |
| 138 return id; | |
| 139 | 124 |
| 140 last_most_visited_item_id_++; | 125 // Post task to the IO thread to copy the data. |
| 141 most_visited_item_id_to_url_map_[last_most_visited_item_id_] = url; | |
| 142 url_to_most_visited_item_id_map_[url] = last_most_visited_item_id_; | |
| 143 | |
| 144 if (instant_io_context_) { | 126 if (instant_io_context_) { |
| 127 std::vector<InstantMostVisitedItemIDPair> items; |
| 128 most_visited_item_cache_.GetCurrentItems(&items); |
| 145 BrowserThread::PostTask( | 129 BrowserThread::PostTask( |
| 146 BrowserThread::IO, FROM_HERE, | 130 BrowserThread::IO, FROM_HERE, |
| 147 base::Bind(&InstantIOContext::AddMostVisitedItemIDOnIO, | 131 base::Bind(&InstantIOContext::AddMostVisitedItemsOnIO, |
| 148 instant_io_context_, last_most_visited_item_id_, url)); | 132 instant_io_context_, |
| 133 items)); |
| 149 } | 134 } |
| 150 | |
| 151 return last_most_visited_item_id_; | |
| 152 } | 135 } |
| 153 | 136 |
| 154 bool InstantService::GetMostVisitedItemIDForURL( | 137 void InstantService::GetCurrentMostVisitedItems( |
| 155 const GURL& url, | 138 std::vector<InstantMostVisitedItemIDPair>* items) const { |
| 156 uint64 *most_visited_item_id) { | 139 most_visited_item_cache_.GetCurrentItems(items); |
| 157 std::map<GURL, uint64>::iterator it = | |
| 158 url_to_most_visited_item_id_map_.find(url); | |
| 159 if (it != url_to_most_visited_item_id_map_.end()) { | |
| 160 *most_visited_item_id = it->second; | |
| 161 return true; | |
| 162 } | |
| 163 *most_visited_item_id = 0; | |
| 164 return false; | |
| 165 } | 140 } |
| 166 | 141 |
| 167 bool InstantService::GetURLForMostVisitedItemId(uint64 most_visited_item_id, | 142 bool InstantService::GetMostVisitedItemForID( |
| 168 GURL* url) { | 143 InstantRestrictedID most_visited_item_id, |
| 169 std::map<uint64, GURL>::iterator it = | 144 InstantMostVisitedItem* item) const { |
| 170 most_visited_item_id_to_url_map_.find(most_visited_item_id); | 145 return most_visited_item_cache_.GetItemWithRestrictedID( |
| 171 if (it != most_visited_item_id_to_url_map_.end()) { | 146 most_visited_item_id, item); |
| 172 *url = it->second; | |
| 173 return true; | |
| 174 } | |
| 175 *url = GURL(); | |
| 176 return false; | |
| 177 } | 147 } |
| 178 | 148 |
| 179 void InstantService::Shutdown() { | 149 void InstantService::Shutdown() { |
| 180 process_ids_.clear(); | 150 process_ids_.clear(); |
| 181 | 151 |
| 182 if (instant_io_context_) { | 152 if (instant_io_context_) { |
| 183 BrowserThread::PostTask( | 153 BrowserThread::PostTask( |
| 184 BrowserThread::IO, FROM_HERE, | 154 BrowserThread::IO, FROM_HERE, |
| 185 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, | 155 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, |
| 186 instant_io_context_)); | 156 instant_io_context_)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 198 process_ids_.erase(process_id); | 168 process_ids_.erase(process_id); |
| 199 | 169 |
| 200 if (instant_io_context_) { | 170 if (instant_io_context_) { |
| 201 BrowserThread::PostTask( | 171 BrowserThread::PostTask( |
| 202 BrowserThread::IO, FROM_HERE, | 172 BrowserThread::IO, FROM_HERE, |
| 203 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, | 173 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, |
| 204 instant_io_context_, process_id)); | 174 instant_io_context_, process_id)); |
| 205 } | 175 } |
| 206 break; | 176 break; |
| 207 } | 177 } |
| 208 case chrome::NOTIFICATION_HISTORY_URLS_DELETED: { | |
| 209 content::Details<history::URLsDeletedDetails> det(details); | |
| 210 std::vector<GURL> deleted_urls; | |
| 211 HistoryDetailsToDeletedURLs(*det.ptr(), &deleted_urls); | |
| 212 | |
| 213 std::vector<uint64> deleted_ids; | |
| 214 if (det->all_history) { | |
| 215 url_to_most_visited_item_id_map_.clear(); | |
| 216 most_visited_item_id_to_url_map_.clear(); | |
| 217 } else { | |
| 218 DeleteHistoryURLs(deleted_urls, &deleted_ids); | |
| 219 } | |
| 220 | |
| 221 if (instant_io_context_) { | |
| 222 BrowserThread::PostTask( | |
| 223 BrowserThread::IO, FROM_HERE, | |
| 224 base::Bind(&InstantIOContext::DeleteMostVisitedURLsOnIO, | |
| 225 instant_io_context_, deleted_ids, det->all_history)); | |
| 226 } | |
| 227 break; | |
| 228 } | |
| 229 default: | 178 default: |
| 230 NOTREACHED() << "Unexpected notification type in InstantService."; | 179 NOTREACHED() << "Unexpected notification type in InstantService."; |
| 231 } | 180 } |
| 232 } | 181 } |
| 233 | |
| 234 void InstantService::DeleteHistoryURLs(const std::vector<GURL>& deleted_urls, | |
| 235 std::vector<uint64>* deleted_ids) { | |
| 236 for (std::vector<GURL>::const_iterator it = deleted_urls.begin(); | |
| 237 it != deleted_urls.end(); | |
| 238 ++it) { | |
| 239 std::map<GURL, uint64>::iterator item = | |
| 240 url_to_most_visited_item_id_map_.find(*it); | |
| 241 if (item != url_to_most_visited_item_id_map_.end()) { | |
| 242 uint64 most_visited_item_id = item->second; | |
| 243 url_to_most_visited_item_id_map_.erase(item); | |
| 244 most_visited_item_id_to_url_map_.erase( | |
| 245 most_visited_item_id_to_url_map_.find(most_visited_item_id)); | |
| 246 deleted_ids->push_back(most_visited_item_id); | |
| 247 } | |
| 248 } | |
| 249 } | |
| OLD | NEW |