history.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// found in the LICENSE file. 490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/ui/app_list/search/history.h" 690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/files/file_path.h" 87d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/strings/string_util.h" 9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h" 1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/ui/app_list/search/history_data.h" 1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/ui/app_list/search/history_data_store.h" 1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/ui/app_list/search/tokenized_string.h" 1390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/browser/browser_context.h" 1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace app_list { 1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace { 1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Normalize the given string by joining all its tokens with a space. 2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)std::string NormalizeString(const std::string& utf8) { 2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) TokenizedString tokenized(UTF8ToUTF16(utf8)); 2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return UTF16ToUTF8(JoinString(tokenized.tokens(), ' ')); 2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} // namespace 2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)History::History(content::BrowserContext* context) 2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) : browser_context_(context), 2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) data_loaded_(false) { 3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const char kStoreDataFileName[] = "App Launcher Search"; 3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const size_t kMaxQueryEntries = 1000; 3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const size_t kMaxSecondaryQueries = 5; 3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const base::FilePath data_file = 3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) browser_context_->GetPath().AppendASCII(kStoreDataFileName); 3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) store_ = new HistoryDataStore(data_file); 377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) data_.reset( 387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) new HistoryData(store_.get(), kMaxQueryEntries, kMaxSecondaryQueries)); 3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) data_->AddObserver(this); 4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)History::~History() { 4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) data_->RemoveObserver(this); 4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool History::IsReady() const { 4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return data_loaded_; 4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void History::AddLaunchEvent(const std::string& query, 5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const std::string& result_id) { 5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) DCHECK(IsReady()); 5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) data_->Add(NormalizeString(query), result_id); 5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 5590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)scoped_ptr<KnownResults> History::GetKnownResults( 5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const std::string& query) const { 5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) DCHECK(IsReady()); 5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return data_->GetKnownResults(NormalizeString(query)).Pass(); 6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 6290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void History::OnHistoryDataLoadedFromStore() { 6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) data_loaded_ = true; 6490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 6590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 6690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} // namespace app_list 67