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..2757c4eb1266993c72727b2fd4a9a867a779de50 |
--- /dev/null |
+++ b/chrome/browser/ui/app_list/search/suggestions/suggestions_search_provider.cc |
@@ -0,0 +1,87 @@ |
+// 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 <string> |
huangs
2014/12/03 20:36:11
Inlude string16 instead of <string>.
Mathieu
2014/12/03 21:01:30
Done.
|
+ |
+#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; |
+ |
+ // 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()); |
+ } |
+} |
+ |
+ |
huangs
2014/12/03 20:36:11
Extra space.
Roger McFarlane (Chromium)
2014/12/03 20:37:21
uber-nit: extra whitespace
Mathieu
2014/12/03 21:01:30
Done.
Mathieu
2014/12/03 21:01:30
Done.
|
+} // namespace app_list |