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" |
| 9 #include "base/files/file_path.h" |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/supports_user_data.h" |
| 13 #include "content/public/browser/browser_thread.h" |
8 #include "content/public/browser/notification_source.h" | 14 #include "content/public/browser/notification_source.h" |
| 15 #include "sql/init_status.h" |
| 16 |
| 17 class WebDatabase; |
| 18 class WebDatabaseService; |
| 19 |
| 20 namespace base { |
| 21 class Thread; |
| 22 } |
9 | 23 |
10 // Base for WebDataService class hierarchy. | 24 // Base for WebDataService class hierarchy. |
11 class WebDataServiceBase { | 25 class WebDataServiceBase |
| 26 : public base::RefCountedThreadSafe<WebDataServiceBase, |
| 27 content::BrowserThread::DeleteOnUIThread> { |
12 public: | 28 public: |
13 // All requests return an opaque handle of the following type. | 29 // All requests return an opaque handle of the following type. |
14 typedef int Handle; | 30 typedef int Handle; |
15 | 31 |
16 virtual ~WebDataServiceBase() {} | 32 // 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 // takes a single parameter, the sql::InitStatus value from trying |
| 35 // to open the database. |
| 36 // TODO(joi): Should we combine this with WebDatabaseService::InitCallback? |
| 37 typedef base::Callback<void(sql::InitStatus)> ProfileErrorCallback; |
| 38 |
| 39 // |callback| will only be invoked on error, and only if |
| 40 // |callback.is_null()| evaluates to false. |
| 41 explicit WebDataServiceBase(const ProfileErrorCallback& callback); |
17 | 42 |
18 // Cancel any pending request. You need to call this method if your | 43 // Cancel any pending request. You need to call this method if your |
19 // WebDataServiceConsumer is about to be deleted. | 44 // WebDataServiceConsumer is about to be deleted. |
20 virtual void CancelRequest(Handle h) = 0; | 45 virtual void CancelRequest(Handle h); |
21 | 46 |
22 // Returns the notification source for this service. This may use a | 47 // Returns the notification source for this service. This may use a |
23 // pointer other than this object's |this| pointer. | 48 // pointer other than this object's |this| pointer. |
24 virtual content::NotificationSource GetNotificationSource() = 0; | 49 virtual content::NotificationSource GetNotificationSource(); |
| 50 |
| 51 // Shutdown the web data service. The service can no longer be used after this |
| 52 // call. |
| 53 virtual void ShutdownOnUIThread(); |
| 54 |
| 55 // Initializes the web data service. |
| 56 virtual void Init(const base::FilePath& path); |
| 57 |
| 58 // Unloads the database without actually shutting down the service. This can |
| 59 // be used to temporarily reduce the browser process' memory footprint. |
| 60 void UnloadDatabase(); |
| 61 |
| 62 // Unloads the database permanently and shuts down service. |
| 63 void ShutdownDatabase(); |
| 64 |
| 65 // Returns true if the database load has completetd successfully, and |
| 66 // ShutdownOnUIThread has not yet been called. |
| 67 virtual bool IsDatabaseLoaded(); |
| 68 |
| 69 // 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 |
| 71 // DBThread. |
| 72 virtual WebDatabase* GetDatabase(); |
| 73 |
| 74 // 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 |
| 76 // the DB thread, and will be destroyed on the DB thread soon after |
| 77 // |ShutdownOnUIThread()| is called. |
| 78 base::SupportsUserData* GetDBUserData(); |
| 79 |
| 80 protected: |
| 81 virtual ~WebDataServiceBase(); |
| 82 virtual void ShutdownOnDBThread(); |
| 83 |
| 84 // Our database service. |
| 85 scoped_ptr<WebDatabaseService> wdbs_; |
| 86 |
| 87 // True if we've received a notification that the WebDatabase has loaded. |
| 88 bool db_loaded_; |
| 89 |
| 90 private: |
| 91 friend struct content::BrowserThread::DeleteOnThread< |
| 92 content::BrowserThread::UI>; |
| 93 friend class base::DeleteHelper<WebDataServiceBase>; |
| 94 |
| 95 ProfileErrorCallback profile_error_callback_; |
| 96 |
| 97 // This makes the destructor public, and thus allows us to aggregate |
| 98 // SupportsUserData. It is private by default to prevent incorrect |
| 99 // usage in class hierarchies where it is inherited by |
| 100 // reference-counted objects. |
| 101 class SupportsUserDataAggregatable : public base::SupportsUserData { |
| 102 public: |
| 103 SupportsUserDataAggregatable() {} |
| 104 virtual ~SupportsUserDataAggregatable() {} |
| 105 private: |
| 106 DISALLOW_COPY_AND_ASSIGN(SupportsUserDataAggregatable); |
| 107 }; |
| 108 |
| 109 // Storage for user data to be accessed only on the DB thread. May |
| 110 // be used e.g. for SyncableService subclasses that need to be owned |
| 111 // by this object. Is created on first call to |GetDBUserData()|. |
| 112 scoped_ptr<SupportsUserDataAggregatable> db_thread_user_data_; |
| 113 |
| 114 void DBInitFailed(sql::InitStatus sql_status); |
| 115 void NotifyDatabaseLoadedOnUIThread(); |
| 116 void DatabaseInitOnDB(sql::InitStatus status); |
25 }; | 117 }; |
26 | 118 |
27 #endif // CHROME_BROWSER_API_WEBDATA_WEB_DATA_SERVICE_BASE_H_ | 119 #endif // CHROME_BROWSER_API_WEBDATA_WEB_DATA_SERVICE_BASE_H_ |
OLD | NEW |