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