summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/app_list/search/app_search_provider.cc
blob: 3a7d5db4e3baab928265e374ef7bc0427e339d16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2013 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/app_search_provider.h"

#include <string>

#include "base/utf_string_conversions.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/app_list/search/app_result.h"
#include "chrome/browser/ui/app_list/search/tokenized_string.h"
#include "chrome/browser/ui/app_list/search/tokenized_string_match.h"

namespace app_list {

class AppSearchProvider::App {
 public:
  explicit App(const extensions::Extension* extension)
      : app_id_(extension->id()),
        indexed_name_(UTF8ToUTF16(extension->name())) {
  }
  ~App() {}

  const std::string& app_id() const { return app_id_; }
  const TokenizedString& indexed_name() const { return indexed_name_; }

 private:
  const std::string app_id_;
  TokenizedString indexed_name_;

  DISALLOW_COPY_AND_ASSIGN(App);
};

AppSearchProvider::AppSearchProvider(
    Profile* profile,
    AppListControllerDelegate* list_controller)
    : profile_(profile),
      list_controller_(list_controller) {
  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
                 content::Source<Profile>(profile_->GetOriginalProfile()));
  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
                 content::Source<Profile>(profile_->GetOriginalProfile()));
  RefreshApps();
}

AppSearchProvider::~AppSearchProvider() {}

void AppSearchProvider::Start(const string16& query) {
  const TokenizedString query_terms(query);

  ClearResults();

  TokenizedStringMatch match;
  for (Apps::const_iterator app_it = apps_.begin();
       app_it != apps_.end();
       ++app_it) {
    if (!match.Calculate(query_terms, (*app_it)->indexed_name()))
      continue;

    scoped_ptr<AppResult> result(
        new AppResult(profile_, (*app_it)->app_id(), list_controller_));
    result->UpdateFromMatch((*app_it)->indexed_name(), match);
    Add(result.PassAs<ChromeSearchResult>());
  }
}

void AppSearchProvider::Stop() {}

void AppSearchProvider::RefreshApps() {
  ExtensionService* extension_service =
      extensions::ExtensionSystemFactory::GetForProfile(profile_)->
      extension_service();
  if (!extension_service)
    return;  // During testing, there is no extension service.

  const ExtensionSet* extensions = extension_service->extensions();
  apps_.clear();
  for (ExtensionSet::const_iterator iter = extensions->begin();
       iter != extensions->end(); ++iter) {
    const extensions::Extension* app = *iter;
    if (!app->ShouldDisplayInAppLauncher())
      continue;

    if (profile_->IsOffTheRecord() &&
        !extension_service->CanLoadInIncognito(app))
      continue;
    apps_.push_back(new App(app));
  }
}

void AppSearchProvider::Observe(int type,
                                const content::NotificationSource& source,
                                const content::NotificationDetails& detaila) {
  RefreshApps();
}

}  // namespace app_list