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 "base/metrics/field_trial.h" | |
| 8 #include "base/strings/utf_string_conversions.h" | |
| 7 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
| 8 #include "chrome/browser/favicon/fallback_icon_service_factory.h" | 10 #include "chrome/browser/favicon/fallback_icon_service_factory.h" |
| 9 #include "chrome/browser/favicon/large_icon_service_factory.h" | 11 #include "chrome/browser/favicon/large_icon_service_factory.h" |
| 10 #include "chrome/browser/history/top_sites_factory.h" | 12 #include "chrome/browser/history/top_sites_factory.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/search/instant_io_context.h" | 14 #include "chrome/browser/search/instant_io_context.h" |
| 13 #include "chrome/browser/search/instant_service_observer.h" | 15 #include "chrome/browser/search/instant_service_observer.h" |
| 14 #include "chrome/browser/search/most_visited_iframe_source.h" | 16 #include "chrome/browser/search/most_visited_iframe_source.h" |
| 15 #include "chrome/browser/search/search.h" | 17 #include "chrome/browser/search/search.h" |
| 18 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | |
| 16 #include "chrome/browser/search/suggestions/suggestions_source.h" | 19 #include "chrome/browser/search/suggestions/suggestions_source.h" |
| 17 #include "chrome/browser/search/thumbnail_source.h" | 20 #include "chrome/browser/search/thumbnail_source.h" |
| 18 #include "chrome/browser/search_engines/template_url_service_factory.h" | 21 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 19 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" | 22 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" |
| 20 #include "chrome/browser/thumbnails/thumbnail_list_source.h" | 23 #include "chrome/browser/thumbnails/thumbnail_list_source.h" |
| 21 #include "chrome/browser/ui/search/instant_search_prerenderer.h" | 24 #include "chrome/browser/ui/search/instant_search_prerenderer.h" |
| 22 #include "chrome/browser/ui/webui/fallback_icon_source.h" | 25 #include "chrome/browser/ui/webui/fallback_icon_source.h" |
| 23 #include "chrome/browser/ui/webui/favicon_source.h" | 26 #include "chrome/browser/ui/webui/favicon_source.h" |
| 24 #include "chrome/browser/ui/webui/large_icon_source.h" | 27 #include "chrome/browser/ui/webui/large_icon_source.h" |
| 25 #include "chrome/browser/ui/webui/theme_source.h" | 28 #include "chrome/browser/ui/webui/theme_source.h" |
| 26 #include "chrome/common/render_messages.h" | 29 #include "chrome/common/render_messages.h" |
| 27 #include "components/favicon/core/fallback_icon_service.h" | 30 #include "components/favicon/core/fallback_icon_service.h" |
| 28 #include "components/favicon/core/large_icon_service.h" | 31 #include "components/favicon/core/large_icon_service.h" |
| 29 #include "components/history/core/browser/top_sites.h" | 32 #include "components/history/core/browser/top_sites.h" |
| 30 #include "components/keyed_service/core/service_access_type.h" | 33 #include "components/keyed_service/core/service_access_type.h" |
| 31 #include "components/search_engines/template_url_service.h" | 34 #include "components/search_engines/template_url_service.h" |
| 35 #include "components/suggestions/proto/suggestions.pb.h" | |
| 36 #include "components/suggestions/suggestions_service.h" | |
| 32 #include "content/public/browser/browser_thread.h" | 37 #include "content/public/browser/browser_thread.h" |
| 33 #include "content/public/browser/notification_service.h" | 38 #include "content/public/browser/notification_service.h" |
| 34 #include "content/public/browser/notification_types.h" | 39 #include "content/public/browser/notification_types.h" |
| 35 #include "content/public/browser/render_process_host.h" | 40 #include "content/public/browser/render_process_host.h" |
| 36 #include "content/public/browser/url_data_source.h" | 41 #include "content/public/browser/url_data_source.h" |
| 37 #include "grit/theme_resources.h" | 42 #include "grit/theme_resources.h" |
| 38 #include "third_party/skia/include/core/SkColor.h" | 43 #include "third_party/skia/include/core/SkColor.h" |
| 39 #include "ui/gfx/color_utils.h" | 44 #include "ui/gfx/color_utils.h" |
| 40 #include "ui/gfx/image/image_skia.h" | 45 #include "ui/gfx/image/image_skia.h" |
| 41 | 46 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 123 LargeIconServiceFactory::GetForBrowserContext(profile_); | 128 LargeIconServiceFactory::GetForBrowserContext(profile_); |
| 124 content::URLDataSource::Add( | 129 content::URLDataSource::Add( |
| 125 profile_, new FallbackIconSource(fallback_icon_service)); | 130 profile_, new FallbackIconSource(fallback_icon_service)); |
| 126 content::URLDataSource::Add( | 131 content::URLDataSource::Add( |
| 127 profile_, new FaviconSource(profile_, FaviconSource::FAVICON)); | 132 profile_, new FaviconSource(profile_, FaviconSource::FAVICON)); |
| 128 content::URLDataSource::Add( | 133 content::URLDataSource::Add( |
| 129 profile_, new LargeIconSource(fallback_icon_service, large_icon_service)); | 134 profile_, new LargeIconSource(fallback_icon_service, large_icon_service)); |
| 130 content::URLDataSource::Add(profile_, new MostVisitedIframeSource()); | 135 content::URLDataSource::Add(profile_, new MostVisitedIframeSource()); |
| 131 content::URLDataSource::Add( | 136 content::URLDataSource::Add( |
| 132 profile_, new suggestions::SuggestionsSource(profile_)); | 137 profile_, new suggestions::SuggestionsSource(profile_)); |
| 138 | |
| 139 if (!base::FieldTrialList::FindFullName("LocalNTPSuggestionsService") | |
|
Mathieu
2015/07/28 13:32:42
bring the field trial check to anonymous namespace
fserb
2015/07/28 17:55:30
Done.
| |
| 140 .empty()) { | |
| 141 suggestions::SuggestionsService* suggestions_service = | |
|
Mathieu
2015/07/28 13:32:42
Instead of calling the factory every time should w
fserb
2015/07/28 17:55:30
Done.
| |
| 142 suggestions::SuggestionsServiceFactory::GetForProfile(profile_); | |
| 143 if (suggestions_service) { | |
| 144 suggestions_service->FetchSuggestionsData( | |
| 145 suggestions::INITIALIZED_ENABLED_HISTORY, | |
| 146 base::Bind(&InstantService::OnSuggestionsAvailable, | |
| 147 weak_ptr_factory_.GetWeakPtr())); | |
| 148 } | |
| 149 } | |
| 133 } | 150 } |
| 134 | 151 |
| 135 InstantService::~InstantService() { | 152 InstantService::~InstantService() { |
| 136 if (template_url_service_) | 153 if (template_url_service_) |
| 137 template_url_service_->RemoveObserver(this); | 154 template_url_service_->RemoveObserver(this); |
| 138 } | 155 } |
| 139 | 156 |
| 140 void InstantService::AddInstantProcess(int process_id) { | 157 void InstantService::AddInstantProcess(int process_id) { |
| 141 process_ids_.insert(process_id); | 158 process_ids_.insert(process_id); |
| 142 | 159 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 160 observers_.RemoveObserver(observer); | 177 observers_.RemoveObserver(observer); |
| 161 } | 178 } |
| 162 | 179 |
| 163 void InstantService::DeleteMostVisitedItem(const GURL& url) { | 180 void InstantService::DeleteMostVisitedItem(const GURL& url) { |
| 164 scoped_refptr<history::TopSites> top_sites = | 181 scoped_refptr<history::TopSites> top_sites = |
| 165 TopSitesFactory::GetForProfile(profile_); | 182 TopSitesFactory::GetForProfile(profile_); |
| 166 if (!top_sites) | 183 if (!top_sites) |
| 167 return; | 184 return; |
| 168 | 185 |
| 169 top_sites->AddBlacklistedURL(url); | 186 top_sites->AddBlacklistedURL(url); |
| 187 if (!base::FieldTrialList::FindFullName("LocalNTPSuggestionsService") | |
| 188 .empty()) { | |
| 189 suggestions::SuggestionsService* suggestions_service = | |
| 190 suggestions::SuggestionsServiceFactory::GetForProfile(profile_); | |
| 191 if (suggestions_service) { | |
| 192 suggestions_service->BlacklistURL( | |
| 193 url, base::Bind(&InstantService::OnSuggestionsAvailable, | |
| 194 weak_ptr_factory_.GetWeakPtr()), | |
| 195 base::Closure()); | |
| 196 } | |
| 197 } | |
| 170 } | 198 } |
| 171 | 199 |
| 172 void InstantService::UndoMostVisitedDeletion(const GURL& url) { | 200 void InstantService::UndoMostVisitedDeletion(const GURL& url) { |
| 173 scoped_refptr<history::TopSites> top_sites = | 201 scoped_refptr<history::TopSites> top_sites = |
| 174 TopSitesFactory::GetForProfile(profile_); | 202 TopSitesFactory::GetForProfile(profile_); |
| 175 if (!top_sites) | 203 if (!top_sites) |
| 176 return; | 204 return; |
| 177 | 205 |
| 178 top_sites->RemoveBlacklistedURL(url); | 206 top_sites->RemoveBlacklistedURL(url); |
| 207 if (!base::FieldTrialList::FindFullName("LocalNTPSuggestionsService") | |
| 208 .empty()) { | |
| 209 suggestions::SuggestionsService* suggestions_service = | |
| 210 suggestions::SuggestionsServiceFactory::GetForProfile(profile_); | |
| 211 if (suggestions_service) { | |
| 212 suggestions_service->UndoBlacklistURL( | |
| 213 url, base::Bind(&InstantService::OnSuggestionsAvailable, | |
| 214 weak_ptr_factory_.GetWeakPtr()), | |
| 215 base::Closure()); | |
| 216 } | |
| 217 } | |
| 179 } | 218 } |
| 180 | 219 |
| 181 void InstantService::UndoAllMostVisitedDeletions() { | 220 void InstantService::UndoAllMostVisitedDeletions() { |
| 182 scoped_refptr<history::TopSites> top_sites = | 221 scoped_refptr<history::TopSites> top_sites = |
| 183 TopSitesFactory::GetForProfile(profile_); | 222 TopSitesFactory::GetForProfile(profile_); |
| 184 if (!top_sites) | 223 if (!top_sites) |
| 185 return; | 224 return; |
| 186 | 225 |
| 187 top_sites->ClearBlacklistedURLs(); | 226 top_sites->ClearBlacklistedURLs(); |
| 227 if (!base::FieldTrialList::FindFullName("LocalNTPSuggestionsService") | |
| 228 .empty()) { | |
| 229 suggestions::SuggestionsService* suggestions_service = | |
| 230 suggestions::SuggestionsServiceFactory::GetForProfile(profile_); | |
| 231 if (suggestions_service) { | |
| 232 suggestions_service->ClearBlacklist( | |
| 233 base::Bind(&InstantService::OnSuggestionsAvailable, | |
| 234 weak_ptr_factory_.GetWeakPtr())); | |
| 235 } | |
| 236 } | |
| 188 } | 237 } |
| 189 | 238 |
| 190 void InstantService::UpdateThemeInfo() { | 239 void InstantService::UpdateThemeInfo() { |
| 191 #if defined(ENABLE_THEMES) | 240 #if defined(ENABLE_THEMES) |
| 192 // Update theme background info. | 241 // Update theme background info. |
| 193 // Initialize |theme_info| if necessary. | 242 // Initialize |theme_info| if necessary. |
| 194 if (!theme_info_) | 243 if (!theme_info_) |
| 195 OnThemeChanged(ThemeServiceFactory::GetForProfile(profile_)); | 244 OnThemeChanged(ThemeServiceFactory::GetForProfile(profile_)); |
| 196 else | 245 else |
| 197 OnThemeChanged(NULL); | 246 OnThemeChanged(NULL); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 258 process_ids_.erase(process_id); | 307 process_ids_.erase(process_id); |
| 259 | 308 |
| 260 if (instant_io_context_.get()) { | 309 if (instant_io_context_.get()) { |
| 261 content::BrowserThread::PostTask( | 310 content::BrowserThread::PostTask( |
| 262 content::BrowserThread::IO, FROM_HERE, | 311 content::BrowserThread::IO, FROM_HERE, |
| 263 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, | 312 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, |
| 264 instant_io_context_, process_id)); | 313 instant_io_context_, process_id)); |
| 265 } | 314 } |
| 266 } | 315 } |
| 267 | 316 |
| 317 void InstantService::OnSuggestionsAvailable( | |
| 318 const suggestions::SuggestionsProfile& profile) { | |
| 319 std::vector<InstantMostVisitedItem> new_suggestions_items; | |
| 320 for (int i = 0; i < profile.suggestions_size(); ++i) { | |
| 321 const suggestions::ChromeSuggestion& suggestion = profile.suggestions(i); | |
| 322 | |
| 323 InstantMostVisitedItem item; | |
| 324 item.url = GURL(suggestion.url()); | |
| 325 item.title = base::UTF8ToUTF16(suggestion.title()); | |
| 326 item.thumbnail = GURL(suggestion.thumbnail()); | |
| 327 item.favicon = GURL(suggestion.favicon_url()); | |
| 328 new_suggestions_items.push_back(item); | |
| 329 } | |
| 330 suggestions_items_ = new_suggestions_items; | |
| 331 NotifyAboutMostVisitedItems(); | |
| 332 } | |
| 333 | |
| 268 void InstantService::OnMostVisitedItemsReceived( | 334 void InstantService::OnMostVisitedItemsReceived( |
| 269 const history::MostVisitedURLList& data) { | 335 const history::MostVisitedURLList& data) { |
| 270 history::MostVisitedURLList reordered_data(data); | 336 history::MostVisitedURLList reordered_data(data); |
| 271 std::vector<InstantMostVisitedItem> new_most_visited_items; | 337 std::vector<InstantMostVisitedItem> new_most_visited_items; |
| 272 for (size_t i = 0; i < reordered_data.size(); i++) { | 338 for (size_t i = 0; i < reordered_data.size(); i++) { |
| 273 const history::MostVisitedURL& url = reordered_data[i]; | 339 const history::MostVisitedURL& url = reordered_data[i]; |
| 274 InstantMostVisitedItem item; | 340 InstantMostVisitedItem item; |
| 275 item.url = url.url; | 341 item.url = url.url; |
| 276 item.title = url.title; | 342 item.title = url.title; |
| 277 new_most_visited_items.push_back(item); | 343 new_most_visited_items.push_back(item); |
| 278 } | 344 } |
| 279 | 345 |
| 280 most_visited_items_ = new_most_visited_items; | 346 most_visited_items_ = new_most_visited_items; |
| 281 NotifyAboutMostVisitedItems(); | 347 NotifyAboutMostVisitedItems(); |
| 282 } | 348 } |
| 283 | 349 |
| 284 void InstantService::NotifyAboutMostVisitedItems() { | 350 void InstantService::NotifyAboutMostVisitedItems() { |
| 285 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, | 351 if (!suggestions_items_.empty() && |
| 286 MostVisitedItemsChanged(most_visited_items_)); | 352 !base::FieldTrialList::FindFullName("LocalNTPSuggestionsService") |
| 353 .empty()) { | |
| 354 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, | |
| 355 MostVisitedItemsChanged(suggestions_items_)); | |
| 356 } else { | |
| 357 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, | |
| 358 MostVisitedItemsChanged(most_visited_items_)); | |
| 359 } | |
| 287 } | 360 } |
| 288 | 361 |
| 289 #if defined(ENABLE_THEMES) | 362 #if defined(ENABLE_THEMES) |
| 290 | 363 |
| 291 namespace { | 364 namespace { |
| 292 | 365 |
| 293 const int kSectionBorderAlphaTransparency = 80; | 366 const int kSectionBorderAlphaTransparency = 80; |
| 294 | 367 |
| 295 // Converts SkColor to RGBAColor | 368 // Converts SkColor to RGBAColor |
| 296 RGBAColor SkColorToRGBAColor(const SkColor& sKColor) { | 369 RGBAColor SkColorToRGBAColor(const SkColor& sKColor) { |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 461 } | 534 } |
| 462 | 535 |
| 463 void InstantService::ResetInstantSearchPrerenderer() { | 536 void InstantService::ResetInstantSearchPrerenderer() { |
| 464 if (!chrome::ShouldPrefetchSearchResults()) | 537 if (!chrome::ShouldPrefetchSearchResults()) |
| 465 return; | 538 return; |
| 466 | 539 |
| 467 GURL url(chrome::GetSearchResultPrefetchBaseURL(profile_)); | 540 GURL url(chrome::GetSearchResultPrefetchBaseURL(profile_)); |
| 468 instant_prerenderer_.reset( | 541 instant_prerenderer_.reset( |
| 469 url.is_valid() ? new InstantSearchPrerenderer(profile_, url) : NULL); | 542 url.is_valid() ? new InstantSearchPrerenderer(profile_, url) : NULL); |
| 470 } | 543 } |
| OLD | NEW |