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" | |
|
kmadhusu
2015/07/31 21:05:45
This is already included in instant_service.h. You
fserb
2015/08/05 14:56:26
Done.
| |
| 36 #include "components/suggestions/suggestions_service.h" | |
|
kmadhusu
2015/07/31 21:05:44
This is already included in instant_service.h. You
fserb
2015/08/05 14:56:26
Done.
| |
| 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 |
| 42 #if !defined(OS_ANDROID) | 47 #if !defined(OS_ANDROID) |
| 43 #include "chrome/browser/search/local_ntp_source.h" | 48 #include "chrome/browser/search/local_ntp_source.h" |
| 44 #endif | 49 #endif |
| 45 | 50 |
| 46 #if defined(ENABLE_THEMES) | 51 #if defined(ENABLE_THEMES) |
| 47 #include "chrome/browser/themes/theme_properties.h" | 52 #include "chrome/browser/themes/theme_properties.h" |
| 48 #include "chrome/browser/themes/theme_service.h" | 53 #include "chrome/browser/themes/theme_service.h" |
| 49 #include "chrome/browser/themes/theme_service_factory.h" | 54 #include "chrome/browser/themes/theme_service_factory.h" |
| 50 #endif // defined(ENABLE_THEMES) | 55 #endif // defined(ENABLE_THEMES) |
| 51 | 56 |
| 57 namespace { | |
| 58 const char kLocalNTPSuggestionService[] = "LocalNTPSuggestionsService"; | |
|
kmadhusu
2015/07/31 21:05:44
style nit: Add a blank line before this.
fserb
2015/08/05 14:56:26
Done.
| |
| 59 const char kLocalNTPSuggestionServiceEnabled[] = "Enabled"; | |
| 60 | |
| 61 bool IsLocalNTPSuggestionServiceEnabled() { | |
| 62 return base::StartsWith( | |
| 63 base::FieldTrialList::FindFullName(kLocalNTPSuggestionService), | |
| 64 kLocalNTPSuggestionServiceEnabled, base::CompareCase::INSENSITIVE_ASCII); | |
|
kmadhusu
2015/07/31 21:05:44
Include base/strings/string_util.h for CompareCase
fserb
2015/08/05 14:56:26
Done.
| |
| 65 } | |
|
kmadhusu
2015/07/31 21:05:44
style nit: Add a blank line after this.
fserb
2015/08/05 14:56:26
Done.
| |
| 66 } // namespace | |
| 67 | |
| 52 InstantService::InstantService(Profile* profile) | 68 InstantService::InstantService(Profile* profile) |
| 53 : profile_(profile), | 69 : profile_(profile), |
| 54 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile_)), | 70 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile_)), |
| 55 omnibox_start_margin_(chrome::kDisableStartMargin), | 71 omnibox_start_margin_(chrome::kDisableStartMargin), |
| 72 suggestions_service_(NULL), | |
| 56 weak_ptr_factory_(this) { | 73 weak_ptr_factory_(this) { |
| 57 // The initialization below depends on a typical set of browser threads. Skip | 74 // The initialization below depends on a typical set of browser threads. Skip |
| 58 // it if we are running in a unit test without the full suite. | 75 // it if we are running in a unit test without the full suite. |
| 59 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) | 76 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) |
| 60 return; | 77 return; |
| 61 | 78 |
| 62 // This depends on the existence of the typical browser threads. Therefore it | 79 // This depends on the existence of the typical browser threads. Therefore it |
| 63 // is only instantiated here (after the check for a UI thread above). | 80 // is only instantiated here (after the check for a UI thread above). |
| 64 instant_io_context_ = new InstantIOContext(); | 81 instant_io_context_ = new InstantIOContext(); |
| 65 | 82 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 123 LargeIconServiceFactory::GetForBrowserContext(profile_); | 140 LargeIconServiceFactory::GetForBrowserContext(profile_); |
| 124 content::URLDataSource::Add( | 141 content::URLDataSource::Add( |
| 125 profile_, new FallbackIconSource(fallback_icon_service)); | 142 profile_, new FallbackIconSource(fallback_icon_service)); |
| 126 content::URLDataSource::Add( | 143 content::URLDataSource::Add( |
| 127 profile_, new FaviconSource(profile_, FaviconSource::FAVICON)); | 144 profile_, new FaviconSource(profile_, FaviconSource::FAVICON)); |
| 128 content::URLDataSource::Add( | 145 content::URLDataSource::Add( |
| 129 profile_, new LargeIconSource(fallback_icon_service, large_icon_service)); | 146 profile_, new LargeIconSource(fallback_icon_service, large_icon_service)); |
| 130 content::URLDataSource::Add(profile_, new MostVisitedIframeSource()); | 147 content::URLDataSource::Add(profile_, new MostVisitedIframeSource()); |
| 131 content::URLDataSource::Add( | 148 content::URLDataSource::Add( |
| 132 profile_, new suggestions::SuggestionsSource(profile_)); | 149 profile_, new suggestions::SuggestionsSource(profile_)); |
| 150 | |
| 151 if (IsLocalNTPSuggestionServiceEnabled()) { | |
| 152 suggestions_service_ = | |
| 153 suggestions::SuggestionsServiceFactory::GetForProfile(profile_); | |
| 154 } | |
| 155 | |
| 156 if (suggestions_service_) { | |
| 157 suggestions_service_->FetchSuggestionsData( | |
| 158 suggestions::INITIALIZED_ENABLED_HISTORY, | |
| 159 base::Bind(&InstantService::OnSuggestionsAvailable, | |
| 160 weak_ptr_factory_.GetWeakPtr())); | |
| 161 } | |
| 133 } | 162 } |
| 134 | 163 |
| 135 InstantService::~InstantService() { | 164 InstantService::~InstantService() { |
| 136 if (template_url_service_) | 165 if (template_url_service_) |
| 137 template_url_service_->RemoveObserver(this); | 166 template_url_service_->RemoveObserver(this); |
| 138 } | 167 } |
| 139 | 168 |
| 140 void InstantService::AddInstantProcess(int process_id) { | 169 void InstantService::AddInstantProcess(int process_id) { |
| 141 process_ids_.insert(process_id); | 170 process_ids_.insert(process_id); |
| 142 | 171 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 156 observers_.AddObserver(observer); | 185 observers_.AddObserver(observer); |
| 157 } | 186 } |
| 158 | 187 |
| 159 void InstantService::RemoveObserver(InstantServiceObserver* observer) { | 188 void InstantService::RemoveObserver(InstantServiceObserver* observer) { |
| 160 observers_.RemoveObserver(observer); | 189 observers_.RemoveObserver(observer); |
| 161 } | 190 } |
| 162 | 191 |
| 163 void InstantService::DeleteMostVisitedItem(const GURL& url) { | 192 void InstantService::DeleteMostVisitedItem(const GURL& url) { |
| 164 scoped_refptr<history::TopSites> top_sites = | 193 scoped_refptr<history::TopSites> top_sites = |
| 165 TopSitesFactory::GetForProfile(profile_); | 194 TopSitesFactory::GetForProfile(profile_); |
| 166 if (!top_sites) | 195 if (top_sites) |
| 167 return; | 196 top_sites->AddBlacklistedURL(url); |
| 168 | 197 |
| 169 top_sites->AddBlacklistedURL(url); | 198 if (suggestions_service_) { |
| 199 suggestions_service_->BlacklistURL( | |
| 200 url, base::Bind(&InstantService::OnSuggestionsAvailable, | |
| 201 weak_ptr_factory_.GetWeakPtr()), | |
| 202 base::Closure()); | |
| 203 } | |
| 170 } | 204 } |
| 171 | 205 |
| 172 void InstantService::UndoMostVisitedDeletion(const GURL& url) { | 206 void InstantService::UndoMostVisitedDeletion(const GURL& url) { |
| 173 scoped_refptr<history::TopSites> top_sites = | 207 scoped_refptr<history::TopSites> top_sites = |
| 174 TopSitesFactory::GetForProfile(profile_); | 208 TopSitesFactory::GetForProfile(profile_); |
| 175 if (!top_sites) | 209 if (top_sites) |
| 176 return; | 210 top_sites->RemoveBlacklistedURL(url); |
| 177 | 211 |
| 178 top_sites->RemoveBlacklistedURL(url); | 212 if (suggestions_service_) { |
| 213 suggestions_service_->UndoBlacklistURL( | |
| 214 url, base::Bind(&InstantService::OnSuggestionsAvailable, | |
| 215 weak_ptr_factory_.GetWeakPtr()), | |
| 216 base::Closure()); | |
| 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 top_sites->ClearBlacklistedURLs(); |
| 186 | 225 |
| 187 top_sites->ClearBlacklistedURLs(); | 226 if (suggestions_service_) { |
| 227 suggestions_service_->ClearBlacklist( | |
| 228 base::Bind(&InstantService::OnSuggestionsAvailable, | |
| 229 weak_ptr_factory_.GetWeakPtr())); | |
| 230 } | |
| 188 } | 231 } |
| 189 | 232 |
| 190 void InstantService::UpdateThemeInfo() { | 233 void InstantService::UpdateThemeInfo() { |
| 191 #if defined(ENABLE_THEMES) | 234 #if defined(ENABLE_THEMES) |
| 192 // Update theme background info. | 235 // Update theme background info. |
| 193 // Initialize |theme_info| if necessary. | 236 // Initialize |theme_info| if necessary. |
| 194 if (!theme_info_) | 237 if (!theme_info_) |
| 195 OnThemeChanged(ThemeServiceFactory::GetForProfile(profile_)); | 238 OnThemeChanged(ThemeServiceFactory::GetForProfile(profile_)); |
| 196 else | 239 else |
| 197 OnThemeChanged(NULL); | 240 OnThemeChanged(NULL); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 258 process_ids_.erase(process_id); | 301 process_ids_.erase(process_id); |
| 259 | 302 |
| 260 if (instant_io_context_.get()) { | 303 if (instant_io_context_.get()) { |
| 261 content::BrowserThread::PostTask( | 304 content::BrowserThread::PostTask( |
| 262 content::BrowserThread::IO, FROM_HERE, | 305 content::BrowserThread::IO, FROM_HERE, |
| 263 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, | 306 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, |
| 264 instant_io_context_, process_id)); | 307 instant_io_context_, process_id)); |
| 265 } | 308 } |
| 266 } | 309 } |
| 267 | 310 |
| 311 void InstantService::OnSuggestionsAvailable( | |
| 312 const suggestions::SuggestionsProfile& profile) { | |
| 313 std::vector<InstantMostVisitedItem> new_suggestions_items; | |
| 314 for (int i = 0; i < profile.suggestions_size(); ++i) { | |
| 315 const suggestions::ChromeSuggestion& suggestion = profile.suggestions(i); | |
| 316 | |
| 317 InstantMostVisitedItem item; | |
| 318 item.url = GURL(suggestion.url()); | |
| 319 item.title = base::UTF8ToUTF16(suggestion.title()); | |
| 320 item.thumbnail = GURL(suggestion.thumbnail()); | |
|
kmadhusu
2015/07/31 21:05:45
nit: Its better to check if suggestion.has_thumbna
fserb
2015/08/05 14:56:25
Done.
| |
| 321 item.favicon = GURL(suggestion.favicon_url()); | |
| 322 new_suggestions_items.push_back(item); | |
| 323 } | |
| 324 suggestions_items_ = new_suggestions_items; | |
| 325 NotifyAboutMostVisitedItems(); | |
| 326 } | |
| 327 | |
| 268 void InstantService::OnMostVisitedItemsReceived( | 328 void InstantService::OnMostVisitedItemsReceived( |
| 269 const history::MostVisitedURLList& data) { | 329 const history::MostVisitedURLList& data) { |
| 270 history::MostVisitedURLList reordered_data(data); | 330 history::MostVisitedURLList reordered_data(data); |
| 271 std::vector<InstantMostVisitedItem> new_most_visited_items; | 331 std::vector<InstantMostVisitedItem> new_most_visited_items; |
| 272 for (size_t i = 0; i < reordered_data.size(); i++) { | 332 for (size_t i = 0; i < reordered_data.size(); i++) { |
| 273 const history::MostVisitedURL& url = reordered_data[i]; | 333 const history::MostVisitedURL& url = reordered_data[i]; |
| 274 InstantMostVisitedItem item; | 334 InstantMostVisitedItem item; |
| 275 item.url = url.url; | 335 item.url = url.url; |
| 276 item.title = url.title; | 336 item.title = url.title; |
| 277 new_most_visited_items.push_back(item); | 337 new_most_visited_items.push_back(item); |
| 278 } | 338 } |
| 279 | 339 |
| 280 most_visited_items_ = new_most_visited_items; | 340 most_visited_items_ = new_most_visited_items; |
| 281 NotifyAboutMostVisitedItems(); | 341 NotifyAboutMostVisitedItems(); |
| 282 } | 342 } |
| 283 | 343 |
| 284 void InstantService::NotifyAboutMostVisitedItems() { | 344 void InstantService::NotifyAboutMostVisitedItems() { |
| 285 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, | 345 if (suggestions_service_ && !suggestions_items_.empty()) { |
| 286 MostVisitedItemsChanged(most_visited_items_)); | 346 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, |
| 347 MostVisitedItemsChanged(suggestions_items_)); | |
| 348 } else { | |
| 349 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, | |
| 350 MostVisitedItemsChanged(most_visited_items_)); | |
| 351 } | |
| 287 } | 352 } |
| 288 | 353 |
| 289 #if defined(ENABLE_THEMES) | 354 #if defined(ENABLE_THEMES) |
| 290 | 355 |
| 291 namespace { | 356 namespace { |
| 292 | 357 |
| 293 const int kSectionBorderAlphaTransparency = 80; | 358 const int kSectionBorderAlphaTransparency = 80; |
| 294 | 359 |
| 295 // Converts SkColor to RGBAColor | 360 // Converts SkColor to RGBAColor |
| 296 RGBAColor SkColorToRGBAColor(const SkColor& sKColor) { | 361 RGBAColor SkColorToRGBAColor(const SkColor& sKColor) { |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 461 } | 526 } |
| 462 | 527 |
| 463 void InstantService::ResetInstantSearchPrerenderer() { | 528 void InstantService::ResetInstantSearchPrerenderer() { |
| 464 if (!chrome::ShouldPrefetchSearchResults()) | 529 if (!chrome::ShouldPrefetchSearchResults()) |
| 465 return; | 530 return; |
| 466 | 531 |
| 467 GURL url(chrome::GetSearchResultPrefetchBaseURL(profile_)); | 532 GURL url(chrome::GetSearchResultPrefetchBaseURL(profile_)); |
| 468 instant_prerenderer_.reset( | 533 instant_prerenderer_.reset( |
| 469 url.is_valid() ? new InstantSearchPrerenderer(profile_, url) : NULL); | 534 url.is_valid() ? new InstantSearchPrerenderer(profile_, url) : NULL); |
| 470 } | 535 } |
| OLD | NEW |