#ifndef CHROME_BROWSER_AUTOCOMPLETE_SHORTCUTS_BACKEND_H_
#define CHROME_BROWSER_AUTOCOMPLETE_SHORTCUTS_BACKEND_H_
#include <map>
#include <string>
#include <vector>
#include "base/files/file_path.h"
#include "base/gtest_prod_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/observer_list.h"
#include "base/strings/string16.h"
#include "base/synchronization/lock.h"
#include "base/time/time.h"
#include "chrome/browser/autocomplete/autocomplete_match.h"
#include "chrome/browser/history/shortcuts_database.h"
#include "components/keyed_service/content/refcounted_browser_context_keyed_service.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "url/gurl.h"
class Profile;
namespace history {
class ShortcutsDatabase;
};
class ShortcutsBackend : public RefcountedBrowserContextKeyedService,
public content::NotificationObserver {
public:
typedef std::multimap<base::string16,
const history::ShortcutsDatabase::Shortcut> ShortcutMap;
ShortcutsBackend(Profile* profile, bool suppress_db);
class ShortcutsBackendObserver {
public:
virtual void OnShortcutsLoaded() = 0;
virtual void OnShortcutsChanged() {}
protected:
virtual ~ShortcutsBackendObserver() {}
};
bool Init();
bool initialized() const { return current_state_ == INITIALIZED; }
const ShortcutMap& shortcuts_map() const { return shortcuts_map_; }
bool DeleteShortcutsWithURL(const GURL& shortcut_url);
void AddObserver(ShortcutsBackendObserver* obs);
void RemoveObserver(ShortcutsBackendObserver* obs);
void AddOrUpdateShortcut(const base::string16& text,
const AutocompleteMatch& match);
private:
friend class base::RefCountedThreadSafe<ShortcutsBackend>;
friend class ShortcutsProviderTest;
friend class ShortcutsBackendTest;
FRIEND_TEST_ALL_PREFIXES(ShortcutsBackendTest, EntitySuggestionTest);
enum CurrentState {
NOT_INITIALIZED,
INITIALIZING,
INITIALIZED,
};
typedef std::map<std::string, ShortcutMap::iterator> GuidMap;
virtual ~ShortcutsBackend();
static history::ShortcutsDatabase::Shortcut::MatchCore MatchToMatchCore(
const AutocompleteMatch& match, Profile* profile);
virtual void ShutdownOnUIThread() OVERRIDE;
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
void InitInternal();
void InitCompleted();
bool AddShortcut(const history::ShortcutsDatabase::Shortcut& shortcut);
bool UpdateShortcut(const history::ShortcutsDatabase::Shortcut& shortcut);
bool DeleteShortcutsWithIDs(
const history::ShortcutsDatabase::ShortcutIDs& shortcut_ids);
bool DeleteShortcutsWithURL(const GURL& url, bool exact_match);
bool DeleteAllShortcuts();
Profile* profile_;
CurrentState current_state_;
ObserverList<ShortcutsBackendObserver> observer_list_;
scoped_refptr<history::ShortcutsDatabase> db_;
scoped_ptr<ShortcutMap> temp_shortcuts_map_;
scoped_ptr<GuidMap> temp_guid_map_;
ShortcutMap shortcuts_map_;
GuidMap guid_map_;
content::NotificationRegistrar notification_registrar_;
bool no_db_access_;
DISALLOW_COPY_AND_ASSIGN(ShortcutsBackend);
};
#endif