app_search_provider.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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 3// found in the LICENSE file. 4 5#include "chrome/browser/ui/app_list/search/app_search_provider.h" 6 7#include <string> 8 9#include "base/strings/utf_string_conversions.h" 10#include "chrome/browser/extensions/extension_service.h" 11#include "chrome/browser/extensions/extension_system_factory.h" 12#include "chrome/browser/profiles/profile.h" 13#include "chrome/browser/ui/app_list/search/app_result.h" 14#include "chrome/browser/ui/app_list/search/tokenized_string.h" 15#include "chrome/browser/ui/app_list/search/tokenized_string_match.h" 16#include "chrome/common/chrome_notification_types.h" 17#include "content/public/browser/notification_details.h" 18#include "content/public/browser/notification_source.h" 19 20namespace app_list { 21 22class AppSearchProvider::App { 23 public: 24 explicit App(const extensions::Extension* extension) 25 : app_id_(extension->id()), 26 indexed_name_(UTF8ToUTF16(extension->name())) { 27 } 28 ~App() {} 29 30 const std::string& app_id() const { return app_id_; } 31 const TokenizedString& indexed_name() const { return indexed_name_; } 32 33 private: 34 const std::string app_id_; 35 TokenizedString indexed_name_; 36 37 DISALLOW_COPY_AND_ASSIGN(App); 38}; 39 40AppSearchProvider::AppSearchProvider( 41 Profile* profile, 42 AppListControllerDelegate* list_controller) 43 : profile_(profile), 44 list_controller_(list_controller) { 45 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, 46 content::Source<Profile>(profile_->GetOriginalProfile())); 47 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, 48 content::Source<Profile>(profile_->GetOriginalProfile())); 49 RefreshApps(); 50} 51 52AppSearchProvider::~AppSearchProvider() {} 53 54void AppSearchProvider::Start(const string16& query) { 55 const TokenizedString query_terms(query); 56 57 ClearResults(); 58 59 TokenizedStringMatch match; 60 for (Apps::const_iterator app_it = apps_.begin(); 61 app_it != apps_.end(); 62 ++app_it) { 63 if (!match.Calculate(query_terms, (*app_it)->indexed_name())) 64 continue; 65 66 scoped_ptr<AppResult> result( 67 new AppResult(profile_, (*app_it)->app_id(), list_controller_)); 68 result->UpdateFromMatch((*app_it)->indexed_name(), match); 69 Add(result.PassAs<ChromeSearchResult>()); 70 } 71} 72 73void AppSearchProvider::Stop() {} 74 75void AppSearchProvider::RefreshApps() { 76 ExtensionService* extension_service = 77 extensions::ExtensionSystemFactory::GetForProfile(profile_)-> 78 extension_service(); 79 if (!extension_service) 80 return; // During testing, there is no extension service. 81 82 const ExtensionSet* extensions = extension_service->extensions(); 83 apps_.clear(); 84 for (ExtensionSet::const_iterator iter = extensions->begin(); 85 iter != extensions->end(); ++iter) { 86 const extensions::Extension* app = iter->get(); 87 if (!app->ShouldDisplayInAppLauncher()) 88 continue; 89 90 if (profile_->IsOffTheRecord() && 91 !extension_service->CanLoadInIncognito(app)) 92 continue; 93 apps_.push_back(new App(app)); 94 } 95} 96 97void AppSearchProvider::Observe(int type, 98 const content::NotificationSource& source, 99 const content::NotificationDetails& detaila) { 100 RefreshApps(); 101} 102 103} // namespace app_list 104