| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/time.h" | 20 #include "base/time.h" |
| 21 #include "chrome/browser/autocomplete/autocomplete_match.h" | 21 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 22 #include "chrome/browser/profiles/refcounted_profile_keyed_service.h" |
| 22 #include "content/public/browser/notification_observer.h" | 23 #include "content/public/browser/notification_observer.h" |
| 23 #include "content/public/browser/notification_registrar.h" | 24 #include "content/public/browser/notification_registrar.h" |
| 24 #include "googleurl/src/gurl.h" | 25 #include "googleurl/src/gurl.h" |
| 25 | 26 |
| 26 class Profile; | 27 class Profile; |
| 27 | 28 |
| 28 namespace history { | 29 namespace history { |
| 29 | 30 |
| 30 class ShortcutsDatabase; | 31 class ShortcutsDatabase; |
| 31 | 32 |
| 32 // This class manages the shortcut provider backend - access to database on the | 33 // This class manages the shortcut provider backend - access to database on the |
| 33 // db thread, etc. | 34 // db thread, etc. |
| 34 class ShortcutsBackend : public base::RefCountedThreadSafe<ShortcutsBackend>, | 35 class ShortcutsBackend : public RefcountedProfileKeyedService, |
| 35 public content::NotificationObserver { | 36 public content::NotificationObserver { |
| 36 public: | 37 public: |
| 37 // The following struct encapsulates one previously selected omnibox shortcut. | 38 // The following struct encapsulates one previously selected omnibox shortcut. |
| 38 struct Shortcut { | 39 struct Shortcut { |
| 39 Shortcut(const std::string& id, | 40 Shortcut(const std::string& id, |
| 40 const string16& text, | 41 const string16& text, |
| 41 const GURL& url, | 42 const GURL& url, |
| 42 const string16& contents, | 43 const string16& contents, |
| 43 const ACMatchClassifications& contents_class, | 44 const ACMatchClassifications& contents_class, |
| 44 const string16& description, | 45 const string16& description, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 63 string16 description; | 64 string16 description; |
| 64 ACMatchClassifications description_class; | 65 ACMatchClassifications description_class; |
| 65 | 66 |
| 66 base::Time last_access_time; // Last time shortcut was selected. | 67 base::Time last_access_time; // Last time shortcut was selected. |
| 67 int number_of_hits; // How many times shortcut was selected. | 68 int number_of_hits; // How many times shortcut was selected. |
| 68 }; | 69 }; |
| 69 | 70 |
| 70 typedef std::multimap<string16, ShortcutsBackend::Shortcut> ShortcutMap; | 71 typedef std::multimap<string16, ShortcutsBackend::Shortcut> ShortcutMap; |
| 71 | 72 |
| 72 // |profile| is necessary for profile notifications only and can be NULL in | 73 // |profile| is necessary for profile notifications only and can be NULL in |
| 73 // unit-tests. |db_folder_path| could be an empty path only in unit-tests as | 74 // unit-tests. For unit testing, set |suppress_db| to true to prevent creation |
| 74 // well. It means there is no database created, all things are done in memory. | 75 // of the database, in which case all operations are performed in memory only. |
| 75 ShortcutsBackend(const FilePath& db_folder_path, Profile* profile); | 76 ShortcutsBackend(Profile* profile, bool suppress_db); |
| 76 | 77 |
| 77 // The interface is guaranteed to be called on the thread AddObserver() | 78 // The interface is guaranteed to be called on the thread AddObserver() |
| 78 // was called. | 79 // was called. |
| 79 class ShortcutsBackendObserver { | 80 class ShortcutsBackendObserver { |
| 80 public: | 81 public: |
| 81 // Called after the database is loaded and Init() completed. | 82 // Called after the database is loaded and Init() completed. |
| 82 virtual void OnShortcutsLoaded() = 0; | 83 virtual void OnShortcutsLoaded() = 0; |
| 83 // Called when shortcuts changed (added/updated/removed) in the database. | 84 // Called when shortcuts changed (added/updated/removed) in the database. |
| 84 virtual void OnShortcutsChanged() {} | 85 virtual void OnShortcutsChanged() {} |
| 85 | 86 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 void InitInternal(); | 136 void InitInternal(); |
| 136 | 137 |
| 137 // Finishes initialization on UI thread, notifies all observers. | 138 // Finishes initialization on UI thread, notifies all observers. |
| 138 void InitCompleted(); | 139 void InitCompleted(); |
| 139 | 140 |
| 140 // content::NotificationObserver: | 141 // content::NotificationObserver: |
| 141 virtual void Observe(int type, | 142 virtual void Observe(int type, |
| 142 const content::NotificationSource& source, | 143 const content::NotificationSource& source, |
| 143 const content::NotificationDetails& details) OVERRIDE; | 144 const content::NotificationDetails& details) OVERRIDE; |
| 144 | 145 |
| 146 // RefcountedProfileKeyedService |
| 147 virtual void ShutdownOnUIThread() OVERRIDE; |
| 148 |
| 145 enum CurrentState { | 149 enum CurrentState { |
| 146 NOT_INITIALIZED, // Backend created but not initialized. | 150 NOT_INITIALIZED, // Backend created but not initialized. |
| 147 INITIALIZING, // Init() called, but not completed yet. | 151 INITIALIZING, // Init() called, but not completed yet. |
| 148 INITIALIZED, // Initialization completed, all accessors can be safely | 152 INITIALIZED, // Initialization completed, all accessors can be safely |
| 149 // called. | 153 // called. |
| 150 }; | 154 }; |
| 155 |
| 151 CurrentState current_state_; | 156 CurrentState current_state_; |
| 152 ObserverList<ShortcutsBackendObserver> observer_list_; | 157 ObserverList<ShortcutsBackendObserver> observer_list_; |
| 153 scoped_refptr<ShortcutsDatabase> db_; | 158 scoped_refptr<ShortcutsDatabase> db_; |
| 154 | 159 |
| 155 // The |temp_shortcuts_map_| and |temp_guid_map_| used for temporary storage | 160 // The |temp_shortcuts_map_| and |temp_guid_map_| used for temporary storage |
| 156 // between InitInternal() and InitComplete() to avoid doing a potentially huge | 161 // between InitInternal() and InitComplete() to avoid doing a potentially huge |
| 157 // copy. | 162 // copy. |
| 158 scoped_ptr<ShortcutMap> temp_shortcuts_map_; | 163 scoped_ptr<ShortcutMap> temp_shortcuts_map_; |
| 159 scoped_ptr<GuidToShortcutsIteratorMap> temp_guid_map_; | 164 scoped_ptr<GuidToShortcutsIteratorMap> temp_guid_map_; |
| 160 | 165 |
| 161 ShortcutMap shortcuts_map_; | 166 ShortcutMap shortcuts_map_; |
| 162 // This is a helper map for quick access to a shortcut by guid. | 167 // This is a helper map for quick access to a shortcut by guid. |
| 163 GuidToShortcutsIteratorMap guid_map_; | 168 GuidToShortcutsIteratorMap guid_map_; |
| 164 | 169 |
| 165 content::NotificationRegistrar notification_registrar_; | 170 content::NotificationRegistrar notification_registrar_; |
| 166 | 171 |
| 167 // For some unit-test only. | 172 // For some unit-test only. |
| 168 bool no_db_access_; | 173 bool no_db_access_; |
| 169 | 174 |
| 170 DISALLOW_COPY_AND_ASSIGN(ShortcutsBackend); | 175 DISALLOW_COPY_AND_ASSIGN(ShortcutsBackend); |
| 171 }; | 176 }; |
| 172 | 177 |
| 173 } // namespace history | 178 } // namespace history |
| 174 | 179 |
| 175 #endif // CHROME_BROWSER_HISTORY_SHORTCUTS_BACKEND_H_ | 180 #endif // CHROME_BROWSER_HISTORY_SHORTCUTS_BACKEND_H_ |
| OLD | NEW |