history.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/history.h"
6
7#include "base/files/file_path.h"
8#include "base/strings/string_util.h"
9#include "base/strings/utf_string_conversions.h"
10#include "chrome/browser/ui/app_list/search/history_data.h"
11#include "chrome/browser/ui/app_list/search/history_data_store.h"
12#include "chrome/browser/ui/app_list/search/tokenized_string.h"
13#include "content/public/browser/browser_context.h"
14
15namespace app_list {
16
17namespace {
18
19// Normalize the given string by joining all its tokens with a space.
20std::string NormalizeString(const std::string& utf8) {
21  TokenizedString tokenized(UTF8ToUTF16(utf8));
22  return UTF16ToUTF8(JoinString(tokenized.tokens(), ' '));
23}
24
25}  // namespace
26
27History::History(content::BrowserContext* context)
28    : browser_context_(context),
29      data_loaded_(false) {
30  const char kStoreDataFileName[] = "App Launcher Search";
31  const size_t kMaxQueryEntries = 1000;
32  const size_t kMaxSecondaryQueries = 5;
33
34  const base::FilePath data_file =
35      browser_context_->GetPath().AppendASCII(kStoreDataFileName);
36  store_ = new HistoryDataStore(data_file);
37  data_.reset(
38      new HistoryData(store_.get(), kMaxQueryEntries, kMaxSecondaryQueries));
39  data_->AddObserver(this);
40}
41
42History::~History() {
43  data_->RemoveObserver(this);
44}
45
46bool History::IsReady() const {
47  return data_loaded_;
48}
49
50void History::AddLaunchEvent(const std::string& query,
51                             const std::string& result_id) {
52  DCHECK(IsReady());
53  data_->Add(NormalizeString(query), result_id);
54}
55
56scoped_ptr<KnownResults> History::GetKnownResults(
57    const std::string& query) const {
58  DCHECK(IsReady());
59  return data_->GetKnownResults(NormalizeString(query)).Pass();
60}
61
62void History::OnHistoryDataLoadedFromStore() {
63  data_loaded_ = true;
64}
65
66}  // namespace app_list
67