web_data_service_backend.h revision a93a17c8d99d686bd4a1511e5504e5e6cc9fcadf
1// Copyright 2013 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#ifndef COMPONENTS_WEBDATA_COMMON_WEB_DATA_SERVICE_BACKEND_H_ 6#define COMPONENTS_WEBDATA_COMMON_WEB_DATA_SERVICE_BACKEND_H_ 7 8#include "base/basictypes.h" 9#include "base/callback_forward.h" 10#include "base/compiler_specific.h" 11#include "base/files/file_path.h" 12#include "base/memory/ref_counted.h" 13#include "base/memory/scoped_ptr.h" 14#include "base/memory/scoped_vector.h" 15#include "components/webdata/common/web_database_service.h" 16#include "components/webdata/common/webdata_export.h" 17#include "content/public/browser/browser_thread.h" 18 19 20class WebDatabase; 21class WebDatabaseTable; 22class WebDataRequest; 23class WebDataRequestManager; 24 25namespace tracked_objects { 26class Location; 27} 28 29// WebDataServiceBackend handles all database tasks posted by 30// WebDatabaseService. It is refcounted to allow asynchronous destruction on the 31// DB thread. 32 33// TODO(caitkp): Rename this class to WebDatabaseBackend. 34 35class WEBDATA_EXPORT WebDataServiceBackend 36 : public base::RefCountedThreadSafe< 37 WebDataServiceBackend, 38 content::BrowserThread::DeleteOnDBThread> { 39 public: 40 class Delegate { 41 public: 42 virtual ~Delegate() {} 43 44 // Invoked when the backend has finished loading the db. 45 virtual void DBLoaded(sql::InitStatus status) = 0; 46 }; 47 48 explicit WebDataServiceBackend(const base::FilePath& path, 49 Delegate* delegate); 50 51 // Must call only before InitDatabaseWithCallback. 52 void AddTable(scoped_ptr<WebDatabaseTable> table); 53 54 // Initializes the database and notifies caller via callback when complete. 55 // Callback is called synchronously. 56 void InitDatabase(); 57 58 // Opens the database file from the profile path if an init has not yet been 59 // attempted. Separated from the constructor to ease construction/destruction 60 // of this object on one thread but database access on the DB thread. Returns 61 // the status of the database. 62 sql::InitStatus LoadDatabaseIfNecessary(); 63 64 // Shuts down database. |should_reinit| tells us whether or not it should be 65 // possible to re-initialize the DB after the shutdown. 66 void ShutdownDatabase(bool should_reinit); 67 68 // Task wrappers to update requests and and notify |request_manager_|. These 69 // are used in cases where the request is being made from the UI thread and an 70 // asyncronous callback is required to notify the client of |request|'s 71 // completion. 72 void DBWriteTaskWrapper( 73 const WebDatabaseService::WriteTask& task, 74 scoped_ptr<WebDataRequest> request); 75 void DBReadTaskWrapper( 76 const WebDatabaseService::ReadTask& task, 77 scoped_ptr<WebDataRequest> request); 78 79 // Task runners to run database tasks. 80 void ExecuteWriteTask(const WebDatabaseService::WriteTask& task); 81 scoped_ptr<WDTypedResult> ExecuteReadTask( 82 const WebDatabaseService::ReadTask& task); 83 84 const scoped_refptr<WebDataRequestManager>& request_manager() { 85 return request_manager_; 86 } 87 88 WebDatabase* database() { return db_.get(); } 89 90 protected: 91 virtual ~WebDataServiceBackend(); 92 93 private: 94 friend struct content::BrowserThread::DeleteOnThread< 95 content::BrowserThread::DB>; 96 friend class base::DeleteHelper<WebDataServiceBackend>; 97 // We have to friend RCTS<> so WIN shared-lib build is happy 98 // (http://crbug/112250). 99 friend class base::RefCountedThreadSafe<WebDataServiceBackend, 100 content::BrowserThread::DeleteOnDBThread>; 101 102 // Commit the current transaction. 103 void Commit(); 104 105 // Path to database file. 106 base::FilePath db_path_; 107 108 // The tables that participate in managing the database. These are 109 // owned here but other than that this class does nothing with 110 // them. Their initialization is in whatever factory creates 111 // WebDatabaseService, and lookup by type is provided by the 112 // WebDatabase class. The tables need to be owned by this refcounted 113 // object, or they themselves would need to be refcounted. Owning 114 // them here rather than having WebDatabase own them makes for 115 // easier unit testing of WebDatabase. 116 ScopedVector<WebDatabaseTable> tables_; 117 118 scoped_ptr<WebDatabase> db_; 119 120 // Keeps track of all pending requests made to the db. 121 scoped_refptr<WebDataRequestManager> request_manager_; 122 123 // State of database initialization. Used to prevent the executing of tasks 124 // before the db is ready. 125 sql::InitStatus init_status_; 126 127 // True if an attempt has been made to load the database (even if the attempt 128 // fails), used to avoid continually trying to reinit if the db init fails. 129 bool init_complete_; 130 131 // Delegate. See the class definition above for more information. 132 scoped_ptr<Delegate> delegate_; 133 134 DISALLOW_COPY_AND_ASSIGN(WebDataServiceBackend); 135}; 136 137#endif // COMPONENTS_WEBDATA_COMMON_WEB_DATA_SERVICE_BACKEND_H_ 138