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..14aa3e7c15643184cfc92fb678ec9bd5faa16530 |
--- /dev/null |
+++ b/chrome/browser/ui/app_list/search/suggestions/suggestions_search_provider.cc |
@@ -0,0 +1,77 @@ |
+// 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/suggestions_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. |
+ return; |
+ } |
+ |
+ // Only return suggestions on an empty query. |
+ if (!query.empty()) return; |
calamity
2014/12/05 04:01:58
Is this CL clang-formatted? If not, please run git
Mathieu
2014/12/05 20:14:32
Ran git cl format, which produces different output
calamity
2014/12/10 01:47:42
Yeah... I think there are special options if you w
|
+ |
+ // Suggestions service is enabled, initiate a query. |
+ 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<SuggestionsResult> result(new SuggestionsResult( |
+ profile_, list_controller_, suggestions_service_, suggestion, i + 1)); |
+ Add(result.Pass()); |
+ } |
+} |
+ |
+} // namespace app_list |