web_data_service_backend.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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 "content/public/browser/browser_thread.h" 17 18 19class WebDatabase; 20class WebDatabaseTable; 21class WebDataRequest; 22class WebDataRequestManager; 23 24namespace tracked_objects { 25class Location; 26} 27 28// WebDataServiceBackend handles all database tasks posted by 29// WebDatabaseService. It is refcounted to allow asynchronous destruction on the 30// DB thread. 31 32class WebDataServiceBackend 33 : public base::RefCountedThreadSafe< 34 WebDataServiceBackend, 35 content::BrowserThread::DeleteOnDBThread> { 36 public: 37 class Delegate { 38 public: 39 virtual ~Delegate() {} 40 41 // Invoked when the backend has finished loading the db. 42 virtual void DBLoaded(sql::InitStatus status) = 0; 43 }; 44 45 explicit WebDataServiceBackend(const base::FilePath& path, 46 Delegate* delegate); 47 48 // Must call only before InitDatabaseWithCallback. 49 void AddTable(scoped_ptr<WebDatabaseTable> table); 50 51 // Initializes the database and notifies caller via callback when complete. 52 // Callback is called synchronously. 53 void InitDatabase(); 54 55 // Opens the database file from the profile path if an init has not yet been 56 // attempted. Separated from the constructor to ease construction/destruction 57 // of this object on one thread but database access on the DB thread. Returns 58 // the status of the database. 59 sql::InitStatus LoadDatabaseIfNecessary(); 60 61 // Shuts down database. |should_reinit| tells us whether or not it should be 62 // possible to re-initialize the DB after the shutdown. 63 void ShutdownDatabase(bool should_reinit); 64 65 // Task wrappers to run database tasks. 66 void DBWriteTaskWrapper( 67 const WebDatabaseService::WriteTask& task, 68 scoped_ptr<WebDataRequest> request); 69 void DBReadTaskWrapper( 70 const WebDatabaseService::ReadTask& task, 71 scoped_ptr<WebDataRequest> request); 72 73 const scoped_refptr<WebDataRequestManager>& request_manager() { 74 return request_manager_; 75 } 76 77 WebDatabase* database() { return db_.get(); } 78 79 private: 80 friend struct content::BrowserThread::DeleteOnThread< 81 content::BrowserThread::DB>; 82 friend class base::DeleteHelper<WebDataServiceBackend>; 83 84 virtual ~WebDataServiceBackend(); 85 86 // Commit the current transaction. 87 void Commit(); 88 89 // Path to database file. 90 base::FilePath db_path_; 91 92 // The tables that participate in managing the database. These are 93 // owned here but other than that this class does nothing with 94 // them. Their initialization is in whatever factory creates 95 // WebDatabaseService, and lookup by type is provided by the 96 // WebDatabase class. The tables need to be owned by this refcounted 97 // object, or they themselves would need to be refcounted. Owning 98 // them here rather than having WebDatabase own them makes for 99 // easier unit testing of WebDatabase. 100 ScopedVector<WebDatabaseTable> tables_; 101 102 scoped_ptr<WebDatabase> db_; 103 104 // Keeps track of all pending requests made to the db. 105 scoped_refptr<WebDataRequestManager> request_manager_; 106 107 // State of database initialization. Used to prevent the executing of tasks 108 // before the db is ready. 109 sql::InitStatus init_status_; 110 111 // True if an attempt has been made to load the database (even if the attempt 112 // fails), used to avoid continually trying to reinit if the db init fails. 113 bool init_complete_; 114 115 // Delegate. See the class definition above for more information. 116 scoped_ptr<Delegate> delegate_; 117 118 DISALLOW_COPY_AND_ASSIGN(WebDataServiceBackend); 119}; 120 121#endif // COMPONENTS_WEBDATA_COMMON_WEB_DATA_SERVICE_BACKEND_H_ 122