history.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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/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(new HistoryData(store_, kMaxQueryEntries, kMaxSecondaryQueries));
38  data_->AddObserver(this);
39}
40
41History::~History() {
42  data_->RemoveObserver(this);
43}
44
45bool History::IsReady() const {
46  return data_loaded_;
47}
48
49void History::AddLaunchEvent(const std::string& query,
50                             const std::string& result_id) {
51  DCHECK(IsReady());
52  data_->Add(NormalizeString(query), result_id);
53}
54
55scoped_ptr<KnownResults> History::GetKnownResults(
56    const std::string& query) const {
57  DCHECK(IsReady());
58  return data_->GetKnownResults(NormalizeString(query)).Pass();
59}
60
61void History::OnHistoryDataLoadedFromStore() {
62  data_loaded_ = true;
63}
64
65}  // namespace app_list
66