| 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/instant/instant_service.h" | 5 #include "chrome/browser/instant/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" | |
| 9 #include "chrome/browser/instant/instant_io_context.h" | 8 #include "chrome/browser/instant/instant_io_context.h" |
| 10 #include "chrome/browser/instant/instant_service_factory.h" | 9 #include "chrome/browser/instant/instant_service_factory.h" |
| 11 #include "chrome/browser/instant/local_omnibox_popup_source.h" | 10 #include "chrome/browser/instant/local_omnibox_popup_source.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/ui/webui/favicon_source.h" | 12 #include "chrome/browser/ui/webui/favicon_source.h" |
| 14 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" | 13 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" |
| 15 #include "chrome/browser/ui/webui/theme_source.h" | 14 #include "chrome/browser/ui/webui/theme_source.h" |
| 16 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
| 19 #include "content/public/browser/notification_types.h" | 18 #include "content/public/browser/notification_types.h" |
| 20 #include "content/public/browser/render_process_host.h" | 19 #include "content/public/browser/render_process_host.h" |
| 21 #include "content/public/browser/url_data_source.h" | 20 #include "content/public/browser/url_data_source.h" |
| 22 #include "googleurl/src/gurl.h" | 21 #include "googleurl/src/gurl.h" |
| 23 #include "net/url_request/url_request.h" | 22 #include "net/url_request/url_request.h" |
| 24 | 23 |
| 25 using content::BrowserThread; | 24 using content::BrowserThread; |
| 26 | 25 |
| 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) | 26 InstantService::InstantService(Profile* profile) |
| 43 : profile_(profile), | 27 : profile_(profile), |
| 44 last_most_visited_item_id_(0) { | 28 most_visited_item_cache_(kMaxInstantMostVisitedItemCacheSize) { |
| 45 // Stub for unit tests. | 29 // Stub for unit tests. |
| 46 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) | 30 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) |
| 47 return; | 31 return; |
| 48 | 32 |
| 49 registrar_.Add(this, | 33 registrar_.Add(this, |
| 50 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 34 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 51 content::NotificationService::AllSources()); | 35 content::NotificationService::AllSources()); |
| 52 registrar_.Add(this, | |
| 53 chrome::NOTIFICATION_HISTORY_URLS_DELETED, | |
| 54 content::NotificationService::AllSources()); | |
| 55 | 36 |
| 56 instant_io_context_ = new InstantIOContext(); | 37 instant_io_context_ = new InstantIOContext(); |
| 57 | 38 |
| 58 if (profile_ && profile_->GetResourceContext()) { | 39 if (profile_ && profile_->GetResourceContext()) { |
| 59 BrowserThread::PostTask( | 40 BrowserThread::PostTask( |
| 60 BrowserThread::IO, FROM_HERE, | 41 BrowserThread::IO, FROM_HERE, |
| 61 base::Bind(&InstantIOContext::SetUserDataOnIO, | 42 base::Bind(&InstantIOContext::SetUserDataOnIO, |
| 62 profile->GetResourceContext(), instant_io_context_)); | 43 profile->GetResourceContext(), instant_io_context_)); |
| 63 } | 44 } |
| 64 | 45 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 77 | 58 |
| 78 // static | 59 // static |
| 79 const std::string InstantService::MaybeTranslateInstantPathOnUI( | 60 const std::string InstantService::MaybeTranslateInstantPathOnUI( |
| 80 Profile* profile, const std::string& path) { | 61 Profile* profile, const std::string& path) { |
| 81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 82 InstantService* instant_service = | 63 InstantService* instant_service = |
| 83 InstantServiceFactory::GetForProfile(profile); | 64 InstantServiceFactory::GetForProfile(profile); |
| 84 if (!instant_service) | 65 if (!instant_service) |
| 85 return path; | 66 return path; |
| 86 | 67 |
| 87 uint64 most_visited_item_id = 0; | 68 InstantRestrictedID restricted_id = 0; |
| 88 if (base::StringToUint64(path, &most_visited_item_id)) { | 69 DCHECK_EQ(sizeof(InstantRestrictedID), sizeof(int)); |
| 89 GURL url; | 70 if (base::StringToInt(path, &restricted_id)) { |
| 90 if (instant_service->GetURLForMostVisitedItemId(most_visited_item_id, &url)) | 71 InstantMostVisitedItem item; |
| 91 return url.spec(); | 72 if (instant_service->GetMostVisitedItemForID(restricted_id, &item)) |
| 73 return item.url.spec(); |
| 92 } | 74 } |
| 93 return path; | 75 return path; |
| 94 } | 76 } |
| 95 | 77 |
| 96 const std::string InstantService::MaybeTranslateInstantPathOnIO( | 78 const std::string InstantService::MaybeTranslateInstantPathOnIO( |
| 97 const net::URLRequest* request, const std::string& path) { | 79 const net::URLRequest* request, const std::string& path) { |
| 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 99 uint64 most_visited_item_id = 0; | 81 |
| 100 if (base::StringToUint64(path, &most_visited_item_id)) { | 82 InstantRestrictedID restricted_id = 0; |
| 83 DCHECK_EQ(sizeof(InstantRestrictedID), sizeof(int)); |
| 84 if (base::StringToInt(path, &restricted_id)) { |
| 101 GURL url; | 85 GURL url; |
| 102 if (InstantIOContext::GetURLForMostVisitedItemId(request, | 86 if (InstantIOContext::GetURLForMostVisitedItemID(request, |
| 103 most_visited_item_id, | 87 restricted_id, |
| 104 &url)) | 88 &url)) { |
| 105 return url.spec(); | 89 return url.spec(); |
| 90 } |
| 106 } | 91 } |
| 107 return path; | 92 return path; |
| 108 } | 93 } |
| 109 | 94 |
| 110 // static | 95 // static |
| 111 bool InstantService::IsInstantPath(const GURL& url) { | 96 bool InstantService::IsInstantPath(const GURL& url) { |
| 112 // Strip leading slash. | 97 // Strip leading slash. |
| 113 std::string path = url.path().substr(1); | 98 std::string path = url.path().substr(1); |
| 114 | 99 |
| 115 // Check that path is of Most Visited item ID form. | 100 // Check that path is of Most Visited item ID form. |
| 116 uint64 dummy = 0; | 101 uint64 dummy = 0; |
| 117 return base::StringToUint64(path, &dummy); | 102 return base::StringToUint64(path, &dummy); |
| 118 } | 103 } |
| 119 | 104 |
| 120 void InstantService::AddInstantProcess(int process_id) { | 105 void InstantService::AddInstantProcess(int process_id) { |
| 121 process_ids_.insert(process_id); | 106 process_ids_.insert(process_id); |
| 122 | 107 |
| 123 if (instant_io_context_) { | 108 if (instant_io_context_) { |
| 124 BrowserThread::PostTask( | 109 BrowserThread::PostTask( |
| 125 BrowserThread::IO, FROM_HERE, | 110 BrowserThread::IO, FROM_HERE, |
| 126 base::Bind(&InstantIOContext::AddInstantProcessOnIO, | 111 base::Bind(&InstantIOContext::AddInstantProcessOnIO, |
| 127 instant_io_context_, process_id)); | 112 instant_io_context_, process_id)); |
| 128 } | 113 } |
| 129 } | 114 } |
| 130 | 115 |
| 131 bool InstantService::IsInstantProcess(int process_id) const { | 116 bool InstantService::IsInstantProcess(int process_id) const { |
| 132 return process_ids_.find(process_id) != process_ids_.end(); | 117 return process_ids_.find(process_id) != process_ids_.end(); |
| 133 } | 118 } |
| 134 | 119 |
| 135 uint64 InstantService::AddURL(const GURL& url) { | 120 void InstantService::AddMostVisitedItems( |
| 136 uint64 id = 0; | 121 const std::vector<InstantMostVisitedItem>& items) { |
| 137 if (GetMostVisitedItemIDForURL(url, &id)) | 122 most_visited_item_cache_.AddItems(items); |
| 138 return id; | |
| 139 | 123 |
| 140 last_most_visited_item_id_++; | 124 // 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_) { | 125 if (instant_io_context_) { |
| 126 std::vector<InstantMostVisitedItemIDPair> items; |
| 127 most_visited_item_cache_.GetCurrentItems(&items); |
| 145 BrowserThread::PostTask( | 128 BrowserThread::PostTask( |
| 146 BrowserThread::IO, FROM_HERE, | 129 BrowserThread::IO, FROM_HERE, |
| 147 base::Bind(&InstantIOContext::AddMostVisitedItemIDOnIO, | 130 base::Bind(&InstantIOContext::AddMostVisitedItemsOnIO, |
| 148 instant_io_context_, last_most_visited_item_id_, url)); | 131 instant_io_context_, |
| 132 items)); |
| 149 } | 133 } |
| 150 | |
| 151 return last_most_visited_item_id_; | |
| 152 } | 134 } |
| 153 | 135 |
| 154 bool InstantService::GetMostVisitedItemIDForURL( | 136 void InstantService::GetCurrentMostVisitedItems( |
| 155 const GURL& url, | 137 std::vector<InstantMostVisitedItemIDPair>* items) const { |
| 156 uint64 *most_visited_item_id) { | 138 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 } | 139 } |
| 166 | 140 |
| 167 bool InstantService::GetURLForMostVisitedItemId(uint64 most_visited_item_id, | 141 bool InstantService::GetMostVisitedItemForID( |
| 168 GURL* url) { | 142 InstantRestrictedID most_visited_item_id, |
| 169 std::map<uint64, GURL>::iterator it = | 143 InstantMostVisitedItem* item) const { |
| 170 most_visited_item_id_to_url_map_.find(most_visited_item_id); | 144 return most_visited_item_cache_.GetItemWithRestrictedID( |
| 171 if (it != most_visited_item_id_to_url_map_.end()) { | 145 most_visited_item_id, item); |
| 172 *url = it->second; | |
| 173 return true; | |
| 174 } | |
| 175 *url = GURL(); | |
| 176 return false; | |
| 177 } | 146 } |
| 178 | 147 |
| 179 void InstantService::Shutdown() { | 148 void InstantService::Shutdown() { |
| 180 process_ids_.clear(); | 149 process_ids_.clear(); |
| 181 | 150 |
| 182 if (instant_io_context_) { | 151 if (instant_io_context_) { |
| 183 BrowserThread::PostTask( | 152 BrowserThread::PostTask( |
| 184 BrowserThread::IO, FROM_HERE, | 153 BrowserThread::IO, FROM_HERE, |
| 185 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, | 154 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, |
| 186 instant_io_context_)); | 155 instant_io_context_)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 198 process_ids_.erase(process_id); | 167 process_ids_.erase(process_id); |
| 199 | 168 |
| 200 if (instant_io_context_) { | 169 if (instant_io_context_) { |
| 201 BrowserThread::PostTask( | 170 BrowserThread::PostTask( |
| 202 BrowserThread::IO, FROM_HERE, | 171 BrowserThread::IO, FROM_HERE, |
| 203 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, | 172 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, |
| 204 instant_io_context_, process_id)); | 173 instant_io_context_, process_id)); |
| 205 } | 174 } |
| 206 break; | 175 break; |
| 207 } | 176 } |
| 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: | 177 default: |
| 230 NOTREACHED() << "Unexpected notification type in InstantService."; | 178 NOTREACHED() << "Unexpected notification type in InstantService."; |
| 231 } | 179 } |
| 232 } | 180 } |
| 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 |