app_search_provider.cc revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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_ui_util.h" 12#include "chrome/browser/extensions/extension_util.h" 13#include "chrome/browser/profiles/profile.h" 14#include "chrome/browser/ui/app_list/search/app_result.h" 15#include "chrome/browser/ui/app_list/search/tokenized_string.h" 16#include "chrome/browser/ui/app_list/search/tokenized_string_match.h" 17#include "extensions/browser/extension_registry.h" 18#include "extensions/browser/extension_system.h" 19#include "extensions/common/extension.h" 20#include "extensions/common/extension_set.h" 21 22using extensions::ExtensionRegistry; 23 24namespace app_list { 25 26class AppSearchProvider::App { 27 public: 28 explicit App(const extensions::Extension* extension) 29 : app_id_(extension->id()), 30 indexed_name_(base::UTF8ToUTF16(extension->name())) { 31 } 32 ~App() {} 33 34 const std::string& app_id() const { return app_id_; } 35 const TokenizedString& indexed_name() const { return indexed_name_; } 36 37 private: 38 const std::string app_id_; 39 TokenizedString indexed_name_; 40 41 DISALLOW_COPY_AND_ASSIGN(App); 42}; 43 44AppSearchProvider::AppSearchProvider(Profile* profile, 45 AppListControllerDelegate* list_controller) 46 : profile_(profile), 47 list_controller_(list_controller), 48 extension_registry_observer_(this) { 49 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); 50 RefreshApps(); 51} 52 53AppSearchProvider::~AppSearchProvider() {} 54 55void AppSearchProvider::Start(const base::string16& query) { 56 const TokenizedString query_terms(query); 57 58 ClearResults(); 59 60 TokenizedStringMatch match; 61 for (Apps::const_iterator app_it = apps_.begin(); 62 app_it != apps_.end(); 63 ++app_it) { 64 if (!match.Calculate(query_terms, (*app_it)->indexed_name())) 65 continue; 66 67 scoped_ptr<AppResult> result( 68 new AppResult(profile_, (*app_it)->app_id(), list_controller_)); 69 result->UpdateFromMatch((*app_it)->indexed_name(), match); 70 Add(result.PassAs<SearchResult>()); 71 } 72} 73 74void AppSearchProvider::Stop() {} 75 76void AppSearchProvider::AddApps(const extensions::ExtensionSet& extensions) { 77 for (extensions::ExtensionSet::const_iterator iter = extensions.begin(); 78 iter != extensions.end(); ++iter) { 79 const extensions::Extension* app = iter->get(); 80 81 if (!extensions::ui_util::ShouldDisplayInAppLauncher(app, profile_)) 82 continue; 83 84 if (profile_->IsOffTheRecord() && 85 !extensions::util::CanLoadInIncognito(app, profile_)) 86 continue; 87 apps_.push_back(new App(app)); 88 } 89} 90 91void AppSearchProvider::RefreshApps() { 92 apps_.clear(); 93 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_); 94 AddApps(registry->enabled_extensions()); 95 AddApps(registry->disabled_extensions()); 96 AddApps(registry->terminated_extensions()); 97} 98 99void AppSearchProvider::OnExtensionLoaded( 100 content::BrowserContext* browser_context, 101 const extensions::Extension* extension) { 102 RefreshApps(); 103} 104 105void AppSearchProvider::OnExtensionUninstalled( 106 content::BrowserContext* browser_context, 107 const extensions::Extension* extension) { 108 RefreshApps(); 109} 110 111} // namespace app_list 112