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_API_WEBDATA_WEB_DATA_SERVICE_BASE_H_ | 5 #ifndef CHROME_BROWSER_API_WEBDATA_WEB_DATA_SERVICE_BASE_H_ |
6 #define CHROME_BROWSER_API_WEBDATA_WEB_DATA_SERVICE_BASE_H_ | 6 #define CHROME_BROWSER_API_WEBDATA_WEB_DATA_SERVICE_BASE_H_ |
7 | 7 |
8 #include "base/callback_forward.h" | 8 #include "base/callback_forward.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/supports_user_data.h" | 12 #include "base/supports_user_data.h" |
13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/browser/notification_source.h" | 14 #include "content/public/browser/notification_source.h" |
15 #include "sql/init_status.h" | 15 #include "sql/init_status.h" |
16 | 16 |
17 class WebDatabase; | 17 class WebDatabase; |
18 class WebDatabaseService; | 18 class WebDatabaseService; |
| 19 class WebDatabaseTable; |
19 | 20 |
20 namespace base { | 21 namespace base { |
21 class Thread; | 22 class Thread; |
22 } | 23 } |
23 | 24 |
24 // Base for WebDataService class hierarchy. | 25 // Base for WebDataService class hierarchy. |
25 class WebDataServiceBase | 26 class WebDataServiceBase |
26 : public base::RefCountedThreadSafe<WebDataServiceBase, | 27 : public base::RefCountedThreadSafe<WebDataServiceBase, |
27 content::BrowserThread::DeleteOnUIThread> { | 28 content::BrowserThread::DeleteOnUIThread> { |
28 public: | 29 public: |
29 // All requests return an opaque handle of the following type. | 30 // All requests return an opaque handle of the following type. |
30 typedef int Handle; | 31 typedef int Handle; |
31 | 32 |
32 // Users of this class may provide a callback to handle errors | 33 // Users of this class may provide a callback to handle errors |
33 // (e.g. by showing a UI). The callback is called only on error, and | 34 // (e.g. by showing a UI). The callback is called only on error, and |
34 // takes a single parameter, the sql::InitStatus value from trying | 35 // takes a single parameter, the sql::InitStatus value from trying |
35 // to open the database. | 36 // to open the database. |
36 // TODO(joi): Should we combine this with WebDatabaseService::InitCallback? | 37 // TODO(joi): Should we combine this with WebDatabaseService::InitCallback? |
37 typedef base::Callback<void(sql::InitStatus)> ProfileErrorCallback; | 38 typedef base::Callback<void(sql::InitStatus)> ProfileErrorCallback; |
38 | 39 |
39 // |callback| will only be invoked on error, and only if | 40 // |callback| will only be invoked on error, and only if |
40 // |callback.is_null()| evaluates to false. | 41 // |callback.is_null()| evaluates to false. |
41 explicit WebDataServiceBase(const ProfileErrorCallback& callback); | 42 // |
| 43 // The ownership of |wdbs| is shared, with the primary owner being the |
| 44 // WebDataServiceWrapper, and secondary owners being subclasses of |
| 45 // WebDataServiceBase, which receive |wdbs| upon construction. The |
| 46 // WebDataServiceWrapper handles the initializing and shutting down and of |
| 47 // the |wdbs| object. |
| 48 WebDataServiceBase(scoped_refptr<WebDatabaseService> wdbs, |
| 49 const ProfileErrorCallback& callback); |
42 | 50 |
43 // Cancel any pending request. You need to call this method if your | 51 // Cancel any pending request. You need to call this method if your |
44 // WebDataServiceConsumer is about to be deleted. | 52 // WebDataServiceConsumer is about to be deleted. |
45 virtual void CancelRequest(Handle h); | 53 virtual void CancelRequest(Handle h); |
46 | 54 |
47 // Returns the notification source for this service. This may use a | 55 // Returns the notification source for this service. This may use a |
48 // pointer other than this object's |this| pointer. | 56 // pointer other than this object's |this| pointer. |
49 virtual content::NotificationSource GetNotificationSource(); | 57 virtual content::NotificationSource GetNotificationSource(); |
50 | 58 |
51 // Shutdown the web data service. The service can no longer be used after this | 59 // Shutdown the web data service. The service can no longer be used after this |
52 // call. | 60 // call. |
53 virtual void ShutdownOnUIThread(); | 61 virtual void ShutdownOnUIThread(); |
54 | 62 |
| 63 // Adds the given table to the database. Passes ownership. Must be |
| 64 // called for all tables before Init. |
| 65 // |
| 66 // TODO(joi): This method is duplicated a couple of layers deep; |
| 67 // once we have a single object creating the WebDatabaseService as |
| 68 // well as all the XyzWebDataService objects, we should be able to |
| 69 // simplify. |
| 70 void AddTable(scoped_ptr<WebDatabaseTable> table); |
| 71 |
55 // Initializes the web data service. | 72 // Initializes the web data service. |
56 virtual void Init(const base::FilePath& path); | 73 virtual void Init(); |
57 | 74 |
58 // Unloads the database without actually shutting down the service. This can | 75 // Unloads the database without actually shutting down the service. This can |
59 // be used to temporarily reduce the browser process' memory footprint. | 76 // be used to temporarily reduce the browser process' memory footprint. |
60 void UnloadDatabase(); | 77 void UnloadDatabase(); |
61 | 78 |
62 // Unloads the database permanently and shuts down service. | 79 // Unloads the database permanently and shuts down service. |
63 void ShutdownDatabase(); | 80 void ShutdownDatabase(); |
64 | 81 |
65 // Returns true if the database load has completetd successfully, and | 82 // Returns true if the database load has completetd successfully, and |
66 // ShutdownOnUIThread has not yet been called. | 83 // ShutdownOnUIThread has not yet been called. |
67 virtual bool IsDatabaseLoaded(); | 84 virtual bool IsDatabaseLoaded(); |
68 | 85 |
69 // Returns a pointer to the DB (used by SyncableServices). May return NULL if | 86 // Returns a pointer to the DB (used by SyncableServices). May return NULL if |
70 // the database is not loaded or otherwise unavailable. Must be called on | 87 // the database is not loaded or otherwise unavailable. Must be called on |
71 // DBThread. | 88 // DBThread. |
72 virtual WebDatabase* GetDatabase(); | 89 virtual WebDatabase* GetDatabase(); |
73 | 90 |
74 // Returns a SupportsUserData objects that may be used to store data | 91 // Returns a SupportsUserData objects that may be used to store data |
75 // owned by the DB thread on this object. Should be called only from | 92 // owned by the DB thread on this object. Should be called only from |
76 // the DB thread, and will be destroyed on the DB thread soon after | 93 // the DB thread, and will be destroyed on the DB thread soon after |
77 // |ShutdownOnUIThread()| is called. | 94 // |ShutdownOnUIThread()| is called. |
78 base::SupportsUserData* GetDBUserData(); | 95 base::SupportsUserData* GetDBUserData(); |
79 | 96 |
80 protected: | 97 protected: |
81 virtual ~WebDataServiceBase(); | 98 virtual ~WebDataServiceBase(); |
82 virtual void ShutdownOnDBThread(); | 99 virtual void ShutdownOnDBThread(); |
83 | 100 |
84 // Our database service. | 101 // Our database service. |
85 scoped_ptr<WebDatabaseService> wdbs_; | 102 scoped_refptr<WebDatabaseService> wdbs_; |
86 | 103 |
87 // True if we've received a notification that the WebDatabase has loaded. | 104 // True if we've received a notification that the WebDatabase has loaded. |
88 bool db_loaded_; | 105 bool db_loaded_; |
89 | 106 |
90 private: | 107 private: |
91 friend struct content::BrowserThread::DeleteOnThread< | 108 friend struct content::BrowserThread::DeleteOnThread< |
92 content::BrowserThread::UI>; | 109 content::BrowserThread::UI>; |
93 friend class base::DeleteHelper<WebDataServiceBase>; | 110 friend class base::DeleteHelper<WebDataServiceBase>; |
94 | 111 |
95 ProfileErrorCallback profile_error_callback_; | 112 ProfileErrorCallback profile_error_callback_; |
(...skipping 14 matching lines...) Expand all Loading... |
110 // be used e.g. for SyncableService subclasses that need to be owned | 127 // be used e.g. for SyncableService subclasses that need to be owned |
111 // by this object. Is created on first call to |GetDBUserData()|. | 128 // by this object. Is created on first call to |GetDBUserData()|. |
112 scoped_ptr<SupportsUserDataAggregatable> db_thread_user_data_; | 129 scoped_ptr<SupportsUserDataAggregatable> db_thread_user_data_; |
113 | 130 |
114 void DBInitFailed(sql::InitStatus sql_status); | 131 void DBInitFailed(sql::InitStatus sql_status); |
115 void NotifyDatabaseLoadedOnUIThread(); | 132 void NotifyDatabaseLoadedOnUIThread(); |
116 void DatabaseInitOnDB(sql::InitStatus status); | 133 void DatabaseInitOnDB(sql::InitStatus status); |
117 }; | 134 }; |
118 | 135 |
119 #endif // CHROME_BROWSER_API_WEBDATA_WEB_DATA_SERVICE_BASE_H_ | 136 #endif // CHROME_BROWSER_API_WEBDATA_WEB_DATA_SERVICE_BASE_H_ |
OLD | NEW |