Home | History | Annotate | Download | only in common
      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 // Chromium settings and storage represent user-selected preferences and
      6 // information and MUST not be extracted, overwritten or modified except
      7 // through Chromium defined APIs.
      8 
      9 #ifndef COMPONENTS_WEBDATA_COMMON_WEB_DATABASE_SERVICE_H_
     10 #define COMPONENTS_WEBDATA_COMMON_WEB_DATABASE_SERVICE_H_
     11 
     12 #include "base/basictypes.h"
     13 #include "base/callback_forward.h"
     14 #include "base/compiler_specific.h"
     15 #include "base/files/file_path.h"
     16 #include "base/memory/ref_counted.h"
     17 #include "base/memory/ref_counted_delete_on_message_loop.h"
     18 #include "base/memory/scoped_ptr.h"
     19 #include "base/memory/weak_ptr.h"
     20 #include "base/message_loop/message_loop_proxy.h"
     21 #include "base/observer_list.h"
     22 #include "components/webdata/common/web_data_service_base.h"
     23 #include "components/webdata/common/web_database.h"
     24 #include "components/webdata/common/webdata_export.h"
     25 
     26 class WebDataServiceBackend;
     27 class WebDataRequestManager;
     28 
     29 namespace content {
     30 class BrowserContext;
     31 }
     32 
     33 namespace tracked_objects {
     34 class Location;
     35 }
     36 
     37 class WDTypedResult;
     38 class WebDataServiceConsumer;
     39 
     40 
     41 ////////////////////////////////////////////////////////////////////////////////
     42 //
     43 // WebDatabaseService defines the interface to a generic data repository
     44 // responsible for controlling access to the web database (metadata associated
     45 // with web pages).
     46 //
     47 ////////////////////////////////////////////////////////////////////////////////
     48 
     49 class WEBDATA_EXPORT WebDatabaseService
     50     : public base::RefCountedDeleteOnMessageLoop<WebDatabaseService> {
     51  public:
     52   typedef base::Callback<scoped_ptr<WDTypedResult>(WebDatabase*)> ReadTask;
     53   typedef base::Callback<WebDatabase::State(WebDatabase*)> WriteTask;
     54 
     55   // Types for managing DB loading callbacks.
     56   typedef base::Closure DBLoadedCallback;
     57   typedef base::Callback<void(sql::InitStatus)> DBLoadErrorCallback;
     58 
     59   // Takes the path to the WebDatabase file.
     60   // WebDatabaseService lives on |ui_thread| and posts tasks to |db_thread|.
     61   WebDatabaseService(const base::FilePath& path,
     62                      const scoped_refptr<base::MessageLoopProxy>& ui_thread,
     63                      const scoped_refptr<base::MessageLoopProxy>& db_thread);
     64 
     65   // Adds |table| as a WebDatabaseTable that will participate in
     66   // managing the database, transferring ownership. All calls to this
     67   // method must be made before |LoadDatabase| is called.
     68   virtual void AddTable(scoped_ptr<WebDatabaseTable> table);
     69 
     70   // Initializes the web database service.
     71   virtual void LoadDatabase();
     72 
     73   // Unloads the database without actually shutting down the service.  This can
     74   // be used to temporarily reduce the browser process' memory footprint.
     75   virtual void UnloadDatabase();
     76 
     77   // Unloads database and will not reload.
     78   virtual void ShutdownDatabase();
     79 
     80   // Gets a pointer to the WebDatabase (owned by WebDatabaseService).
     81   // TODO(caitkp): remove this method once SyncServices no longer depend on it.
     82   virtual WebDatabase* GetDatabaseOnDB() const;
     83 
     84   // Returns a pointer to the WebDataServiceBackend.
     85   scoped_refptr<WebDataServiceBackend> GetBackend() const;
     86 
     87   // Schedule an update/write task on the DB thread.
     88   virtual void ScheduleDBTask(
     89       const tracked_objects::Location& from_here,
     90       const WriteTask& task);
     91 
     92   // Schedule a read task on the DB thread.
     93   virtual WebDataServiceBase::Handle ScheduleDBTaskWithResult(
     94       const tracked_objects::Location& from_here,
     95       const ReadTask& task,
     96       WebDataServiceConsumer* consumer);
     97 
     98   // Cancel an existing request for a task on the DB thread.
     99   // TODO(caitkp): Think about moving the definition of the Handle type to
    100   // somewhere else.
    101   virtual void CancelRequest(WebDataServiceBase::Handle h);
    102 
    103   // Register a callback to be notified that the database has loaded. Multiple
    104   // callbacks may be registered, and each will be called at most once
    105   // (following a successful database load), then cleared.
    106   // Note: if the database load is already complete, then the callback will NOT
    107   // be stored or called.
    108   void RegisterDBLoadedCallback(const DBLoadedCallback& callback);
    109 
    110   // Register a callback to be notified that the database has failed to load.
    111   // Multiple callbacks may be registered, and each will be called at most once
    112   // (following a database load failure), then cleared.
    113   // Note: if the database load is already complete, then the callback will NOT
    114   // be stored or called.
    115   void RegisterDBErrorCallback(const DBLoadErrorCallback& callback);
    116 
    117   bool db_loaded() const { return db_loaded_; };
    118 
    119  private:
    120   class BackendDelegate;
    121   friend class BackendDelegate;
    122   friend class base::RefCountedDeleteOnMessageLoop<WebDatabaseService>;
    123   friend class base::DeleteHelper<WebDatabaseService>;
    124 
    125   typedef std::vector<DBLoadedCallback> LoadedCallbacks;
    126   typedef std::vector<DBLoadErrorCallback> ErrorCallbacks;
    127 
    128   virtual ~WebDatabaseService();
    129 
    130   void OnDatabaseLoadDone(sql::InitStatus status);
    131 
    132   base::FilePath path_;
    133 
    134   // The primary owner is |WebDatabaseService| but is refcounted because
    135   // PostTask on DB thread may outlive us.
    136   scoped_refptr<WebDataServiceBackend> wds_backend_;
    137 
    138   // All vended weak pointers are invalidated in ShutdownDatabase().
    139   base::WeakPtrFactory<WebDatabaseService> weak_ptr_factory_;
    140 
    141   // Callbacks to be called once the DB has loaded.
    142   LoadedCallbacks loaded_callbacks_;
    143 
    144   // Callbacks to be called if the DB has failed to load.
    145   ErrorCallbacks error_callbacks_;
    146 
    147   // True if the WebDatabase has loaded.
    148   bool db_loaded_;
    149 
    150   scoped_refptr<base::MessageLoopProxy> db_thread_;
    151 
    152   DISALLOW_COPY_AND_ASSIGN(WebDatabaseService);
    153 };
    154 
    155 #endif  // COMPONENTS_WEBDATA_COMMON_WEB_DATABASE_SERVICE_H_
    156