Chromium Code Reviews| 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..ee8da069a32a8edb4fd570a85dac3d2de210bc7f |
| --- /dev/null |
| +++ b/chrome/browser/ui/app_list/search/suggestions/suggestions_search_provider.cc |
| @@ -0,0 +1,83 @@ |
| +// 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 the service is not enabled, do not return any results. |
| + if (!suggestions_service_) |
| + 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<URLSuggestionResult> result(new URLSuggestionResult( |
|
huangs
2014/12/10 07:14:04
base/memory/scoped_ptr.h was never included in thi
Mathieu
2014/12/10 16:01:29
Done. Yes it's for the API.
|
| + profile_, list_controller_, suggestions_service_, suggestion)); |
| + result->set_relevance(1.0 / (i + 1)); |
|
huangs
2014/12/10 07:14:04
Some comment on the formula 1.0 / (i + 1). If you
Mathieu
2014/12/10 16:01:29
Relevance is between [0.0, 1.0]
|
| + Add(result.Pass()); |
| + } |
| +} |
| + |
| +} // namespace app_list |