Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(300)

Side by Side Diff: chrome/browser/history/shortcuts_backend.h

Issue 9689085: Clean up ShortcutsProvider and related classes. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_HISTORY_SHORTCUTS_BACKEND_H_ 5 #ifndef CHROME_BROWSER_HISTORY_SHORTCUTS_BACKEND_H_
6 #define CHROME_BROWSER_HISTORY_SHORTCUTS_BACKEND_H_ 6 #define CHROME_BROWSER_HISTORY_SHORTCUTS_BACKEND_H_
7 #pragma once 7 #pragma once
8 8
9 #include <map> 9 #include <map>
10 #include <string> 10 #include <string>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/file_path.h" 13 #include "base/file_path.h"
14 #include "base/gtest_prod_util.h" 14 #include "base/gtest_prod_util.h"
15 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h" 16 #include "base/memory/scoped_ptr.h"
17 #include "base/observer_list.h" 17 #include "base/observer_list.h"
18 #include "base/string16.h" 18 #include "base/string16.h"
19 #include "base/synchronization/lock.h" 19 #include "base/synchronization/lock.h"
20 #include "chrome/browser/autocomplete/shortcuts_provider_shortcut.h" 20 #include "base/time.h"
21 #include "chrome/browser/history/shortcuts_database.h" 21 #include "chrome/browser/autocomplete/autocomplete_match.h"
22 #include "content/public/browser/notification_observer.h" 22 #include "content/public/browser/notification_observer.h"
23 #include "content/public/browser/notification_registrar.h" 23 #include "content/public/browser/notification_registrar.h"
24 #include "googleurl/src/gurl.h" 24 #include "googleurl/src/gurl.h"
25 25
26 class Profile; 26 class Profile;
27 27
28 namespace history { 28 namespace history {
29 29
30 class ShortcutsDatabase;
31
30 // This class manages the shortcut provider backend - access to database on the 32 // This class manages the shortcut provider backend - access to database on the
31 // db thread, etc. 33 // db thread, etc.
32 class ShortcutsBackend : public base::RefCountedThreadSafe<ShortcutsBackend>, 34 class ShortcutsBackend : public base::RefCountedThreadSafe<ShortcutsBackend>,
33 public content::NotificationObserver { 35 public content::NotificationObserver {
34 public: 36 public:
37 // The following struct encapsulates one previously selected omnibox shortcut.
38 struct Shortcut {
39 Shortcut(const std::string& id,
40 const string16& text,
41 const GURL& url,
42 const string16& contents,
43 const ACMatchClassifications& contents_class,
44 const string16& description,
45 const ACMatchClassifications& description_class,
46 const base::Time& last_access_time,
47 int number_of_hits);
48 // Required for STL, we don't use this directly.
49 Shortcut();
50 ~Shortcut();
51
52 std::string id; // Unique guid for the shortcut.
53 string16 text; // The user's original input string.
54 GURL url; // The corresponding destination URL.
brettw 2012/03/13 22:47:23 Either use 2-spaces or align them all the same.
55
56 // Contents and description from the original match, along with their
57 // corresponding markup. We need these in order to correctly mark which
58 // parts are URLs, dim, etc. However, we strip all MATCH classifications
59 // from these since we'll mark the matching portions ourselves as we match
60 // the user's current typing against these Shortcuts.
61 string16 contents;
62 ACMatchClassifications contents_class;
63 string16 description;
64 ACMatchClassifications description_class;
65
66 base::Time last_access_time; // Last time shortcut was selected.
67 int number_of_hits; // How many times shortcut was selected.
68 };
69
70 typedef std::multimap<string16, ShortcutsBackend::Shortcut> ShortcutMap;
71
35 // |profile| is necessary for profile notifications only and can be NULL in 72 // |profile| is necessary for profile notifications only and can be NULL in
36 // unit-tests. |db_folder_path| could be an empty path only in unit-tests as 73 // unit-tests. |db_folder_path| could be an empty path only in unit-tests as
37 // well. It means there is no database created, all things are done in memory. 74 // well. It means there is no database created, all things are done in memory.
38 ShortcutsBackend(const FilePath& db_folder_path, Profile* profile); 75 ShortcutsBackend(const FilePath& db_folder_path, Profile* profile);
39 virtual ~ShortcutsBackend(); 76 virtual ~ShortcutsBackend();
40 77
41 // The interface is guaranteed to be called on the thread AddObserver() 78 // The interface is guaranteed to be called on the thread AddObserver()
42 // was called. 79 // was called.
43 class ShortcutsBackendObserver { 80 class ShortcutsBackendObserver {
44 public: 81 public:
45 // Called after the database is loaded and Init() completed. 82 // Called after the database is loaded and Init() completed.
46 virtual void OnShortcutsLoaded() = 0; 83 virtual void OnShortcutsLoaded() = 0;
47 // Called when shortcuts changed (added/updated/removed) in the database. 84 // Called when shortcuts changed (added/updated/removed) in the database.
48 virtual void OnShortcutsChanged() {} 85 virtual void OnShortcutsChanged() {}
49 protected: 86 protected:
50 virtual ~ShortcutsBackendObserver() {} 87 virtual ~ShortcutsBackendObserver() {}
51 }; 88 };
52 89
53 // Asynchronously initializes the ShortcutsBackend, it is safe to call 90 // Asynchronously initializes the ShortcutsBackend, it is safe to call
54 // multiple times - only the first call will be processed. 91 // multiple times - only the first call will be processed.
55 bool Init(); 92 bool Init();
56 93
57 bool initialized() const { return current_state_ == INITIALIZED; } 94 bool initialized() const { return current_state_ == INITIALIZED; }
58 95
59 // All of the public functions *must* be called on UI thread only! 96 // All of the public functions *must* be called on UI thread only!
60 97
61 // Adds the Shortcut to the database. 98 // Adds the Shortcut to the database.
62 bool AddShortcut(const shortcuts_provider::Shortcut& shortcut); 99 bool AddShortcut(const ShortcutsBackend::Shortcut& shortcut);
63 100
64 // Updates timing and selection count for the Shortcut. 101 // Updates timing and selection count for the Shortcut.
65 bool UpdateShortcut(const shortcuts_provider::Shortcut& shortcut); 102 bool UpdateShortcut(const ShortcutsBackend::Shortcut& shortcut);
66 103
67 // Deletes the Shortcuts with the id. 104 // Deletes the Shortcuts with the id.
68 bool DeleteShortcutsWithIds(const std::vector<std::string>& shortcut_ids); 105 bool DeleteShortcutsWithIds(const std::vector<std::string>& shortcut_ids);
69 106
70 // Deletes the Shortcuts with the url. 107 // Deletes the Shortcuts with the url.
71 bool DeleteShortcutsWithUrl(const GURL& shortcut_url); 108 bool DeleteShortcutsWithUrl(const GURL& shortcut_url);
72 109
73 // Deletes all of the shortcuts. 110 // Deletes all of the shortcuts.
74 bool DeleteAllShortcuts(); 111 bool DeleteAllShortcuts();
75 112
76 const shortcuts_provider::ShortcutMap& shortcuts_map() const { 113 const ShortcutMap& shortcuts_map() const {
77 return shortcuts_map_; 114 return shortcuts_map_;
78 } 115 }
79 116
80 const shortcuts_provider::GuidToShortcutsIteratorMap& guid_map() const {
81 return guid_map_;
82 }
83
84 void AddObserver(ShortcutsBackendObserver* obs) { 117 void AddObserver(ShortcutsBackendObserver* obs) {
85 observer_list_.AddObserver(obs); 118 observer_list_.AddObserver(obs);
86 } 119 }
87 120
88 void RemoveObserver(ShortcutsBackendObserver* obs) { 121 void RemoveObserver(ShortcutsBackendObserver* obs) {
89 observer_list_.RemoveObserver(obs); 122 observer_list_.RemoveObserver(obs);
90 } 123 }
91 124
92 private: 125 private:
126 typedef std::map<std::string, ShortcutMap::iterator>
127 GuidToShortcutsIteratorMap;
128
93 // Internal initialization of the back-end. Posted by Init() to the DB thread. 129 // Internal initialization of the back-end. Posted by Init() to the DB thread.
94 // On completion posts InitCompleted() back to UI thread. 130 // On completion posts InitCompleted() back to UI thread.
95 void InitInternal(); 131 void InitInternal();
96 132
97 // Finishes initialization on UI thread, notifies all observers. 133 // Finishes initialization on UI thread, notifies all observers.
98 void InitCompleted(); 134 void InitCompleted();
99 135
100 // content::NotificationObserver: 136 // content::NotificationObserver:
101 virtual void Observe(int type, 137 virtual void Observe(int type,
102 const content::NotificationSource& source, 138 const content::NotificationSource& source,
103 const content::NotificationDetails& details) OVERRIDE; 139 const content::NotificationDetails& details) OVERRIDE;
104 140
105 enum CurrentState { 141 enum CurrentState {
106 NOT_INITIALIZED, // Backend created but not initialized. 142 NOT_INITIALIZED, // Backend created but not initialized.
107 INITIALIZING, // Init() called, but not completed yet. 143 INITIALIZING, // Init() called, but not completed yet.
108 INITIALIZED, // Initialization completed, all accessors can be safely 144 INITIALIZED, // Initialization completed, all accessors can be safely
109 // called. 145 // called.
110 }; 146 };
111 CurrentState current_state_; 147 CurrentState current_state_;
112 ObserverList<ShortcutsBackendObserver> observer_list_; 148 ObserverList<ShortcutsBackendObserver> observer_list_;
113 scoped_refptr<ShortcutsDatabase> db_; 149 scoped_refptr<ShortcutsDatabase> db_;
114 150
115 // The |temp_shortcuts_map_| and |temp_guid_map_| used for temporary storage 151 // The |temp_shortcuts_map_| and |temp_guid_map_| used for temporary storage
116 // between InitInternal() and InitComplete() to avoid doing a potentially huge 152 // between InitInternal() and InitComplete() to avoid doing a potentially huge
117 // copy. 153 // copy.
118 scoped_ptr<shortcuts_provider::ShortcutMap> temp_shortcuts_map_; 154 scoped_ptr<ShortcutMap> temp_shortcuts_map_;
119 scoped_ptr<shortcuts_provider::GuidToShortcutsIteratorMap> temp_guid_map_; 155 scoped_ptr<GuidToShortcutsIteratorMap> temp_guid_map_;
120 156
121 shortcuts_provider::ShortcutMap shortcuts_map_; 157 ShortcutMap shortcuts_map_;
122 // This is a helper map for quick access to a shortcut by guid. 158 // This is a helper map for quick access to a shortcut by guid.
123 shortcuts_provider::GuidToShortcutsIteratorMap guid_map_; 159 GuidToShortcutsIteratorMap guid_map_;
124 160
125 content::NotificationRegistrar notification_registrar_; 161 content::NotificationRegistrar notification_registrar_;
126 162
127 // For some unit-test only. 163 // For some unit-test only.
128 bool no_db_access_; 164 bool no_db_access_;
129 165
130 DISALLOW_COPY_AND_ASSIGN(ShortcutsBackend); 166 DISALLOW_COPY_AND_ASSIGN(ShortcutsBackend);
131 }; 167 };
132 168
133 } // namespace history 169 } // namespace history
134 170
135 #endif // CHROME_BROWSER_HISTORY_SHORTCUTS_BACKEND_H_ 171 #endif // CHROME_BROWSER_HISTORY_SHORTCUTS_BACKEND_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698