Index: chrome/browser/ui/app_list/search/suggestions/suggestions_search_provider.cc |
diff --git a/chrome/browser/ui/app_list/search/suggestions/suggestions_search_provider.cc b/chrome/browser/ui/app_list/search/suggestions/suggestions_search_provider.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5f4869e744f9244b3d34034fecfbc4520bfa70e8 |
--- /dev/null |
+++ b/chrome/browser/ui/app_list/search/suggestions/suggestions_search_provider.cc |
@@ -0,0 +1,84 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/ui/app_list/search/suggestions/suggestions_search_provider.h" |
+ |
+#include "base/strings/string16.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
+#include "chrome/browser/sync/profile_sync_service.h" |
+#include "chrome/browser/sync/profile_sync_service_factory.h" |
+#include "chrome/browser/ui/app_list/search/suggestions/url_suggestion_result.h" |
+#include "components/suggestions/proto/suggestions.pb.h" |
+#include "components/suggestions/suggestions_service.h" |
+#include "components/suggestions/suggestions_utils.h" |
+ |
+using suggestions::SyncState; |
+ |
+namespace app_list { |
+ |
+namespace { |
+ |
+// Return the current SyncState for use with the SuggestionsService. |
+SyncState GetSyncState(Profile* profile) { |
+ ProfileSyncService* sync = |
+ ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); |
+ if (!sync) |
+ return SyncState::SYNC_OR_HISTORY_SYNC_DISABLED; |
+ return suggestions::GetSyncState( |
+ sync->IsSyncEnabledAndLoggedIn(), sync->SyncActive(), |
+ sync->GetActiveDataTypes().Has(syncer::HISTORY_DELETE_DIRECTIVES)); |
+} |
+ |
+} // namespace |
+ |
+SuggestionsSearchProvider::SuggestionsSearchProvider( |
+ Profile* profile, |
+ AppListControllerDelegate* list_controller) |
+ : profile_(profile), |
+ list_controller_(list_controller), |
+ suggestions_service_( |
+ suggestions::SuggestionsServiceFactory::GetForProfile(profile)), |
+ weak_ptr_factory_(this) { |
+} |
+ |
+SuggestionsSearchProvider::~SuggestionsSearchProvider() { |
+} |
+ |
+void SuggestionsSearchProvider::Start(const base::string16& query) { |
+ ClearResults(); |
+ if (!suggestions_service_) { |
+ // Service not enabled, do not return any results. |
Matt Giuca
2014/12/09 05:34:54
nit: Move this comment above the if statement and
Mathieu
2014/12/09 21:18:26
Done.
|
+ return; |
+ } |
+ |
+ // Only return suggestions on an empty query. |
+ if (!query.empty()) |
+ return; |
+ |
+ // Suggestions service is enabled, initiate a query. |
Matt Giuca
2014/12/09 05:34:54
Supernit: ',' should be ';'.
Mathieu
2014/12/09 21:18:25
Done.
|
+ suggestions_service_->FetchSuggestionsData( |
+ GetSyncState(profile_), |
+ base::Bind(&SuggestionsSearchProvider::OnSuggestionsProfileAvailable, |
+ weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
+void SuggestionsSearchProvider::Stop() { |
+} |
+ |
+void SuggestionsSearchProvider::OnSuggestionsProfileAvailable( |
+ const suggestions::SuggestionsProfile& suggestions_profile) { |
+ for (int i = 0; i < suggestions_profile.suggestions_size(); ++i) { |
+ const suggestions::ChromeSuggestion& suggestion = |
+ suggestions_profile.suggestions(i); |
+ |
+ // TODO(mathp): If it's an app, create an AppResult. |
+ scoped_ptr<URLSuggestionResult> result(new URLSuggestionResult( |
+ profile_, list_controller_, suggestions_service_, suggestion)); |
+ result->set_relevance(1.0 / (i + 1)); |
+ Add(result.Pass()); |
Matt Giuca
2014/12/09 05:34:54
Is there any chance this gets called more than onc
Mathieu
2014/12/09 21:18:26
There is technically "no chance" because in Sugges
Matt Giuca
2014/12/10 01:47:34
Acknowledged.
|
+ } |
+} |
+ |
+} // namespace app_list |