Home | History | Annotate | Download | only in common
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef COMPONENTS_WEBDATA_COMMON_WEB_DATA_SERVICE_BASE_H_
      6 #define COMPONENTS_WEBDATA_COMMON_WEB_DATA_SERVICE_BASE_H_
      7 
      8 #include "base/callback.h"
      9 #include "base/files/file_path.h"
     10 #include "base/memory/ref_counted.h"
     11 #include "base/memory/ref_counted_delete_on_message_loop.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "components/webdata/common/webdata_export.h"
     14 #include "sql/init_status.h"
     15 
     16 class WebDatabase;
     17 class WebDatabaseService;
     18 class WebDatabaseTable;
     19 
     20 namespace base {
     21 class Thread;
     22 }
     23 
     24 // Base for WebDataService class hierarchy.
     25 // WebDataServiceBase is destroyed on the UI thread.
     26 class WEBDATA_EXPORT WebDataServiceBase
     27     : public base::RefCountedDeleteOnMessageLoop<WebDataServiceBase> {
     28  public:
     29   // All requests return an opaque handle of the following type.
     30   typedef int Handle;
     31 
     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   typedef base::Closure DBLoadedCallback;
     40 
     41   // |callback| will only be invoked on error, and only if
     42   // |callback.is_null()| evaluates to false.
     43   //
     44   // The ownership of |wdbs| is shared, with the primary owner being the
     45   // WebDataServiceWrapper, and secondary owners being subclasses of
     46   // WebDataServiceBase, which receive |wdbs| upon construction. The
     47   // WebDataServiceWrapper handles the initializing and shutting down and of
     48   // the |wdbs| object.
     49   // WebDataServiceBase is destroyed on |ui_thread|.
     50   WebDataServiceBase(scoped_refptr<WebDatabaseService> wdbs,
     51                      const ProfileErrorCallback& callback,
     52                      const scoped_refptr<base::MessageLoopProxy>& ui_thread);
     53 
     54   // Cancel any pending request. You need to call this method if your
     55   // WebDataServiceConsumer is about to be deleted.
     56   virtual void CancelRequest(Handle h);
     57 
     58   // Shutdown the web data service. The service can no longer be used after this
     59   // call.
     60   virtual void ShutdownOnUIThread();
     61 
     62   // Initializes the web data service.
     63   virtual void Init();
     64 
     65   // Unloads the database and shuts down service.
     66   void ShutdownDatabase();
     67 
     68   // Register a callback to be notified that the database has loaded. Multiple
     69   // callbacks may be registered, and each will be called at most once
     70   // (following a successful database load), then cleared.
     71   // Note: if the database load is already complete, then the callback will NOT
     72   // be stored or called.
     73   virtual void RegisterDBLoadedCallback(const DBLoadedCallback& callback);
     74 
     75   // Returns true if the database load has completetd successfully, and
     76   // ShutdownOnUIThread has not yet been called.
     77   virtual bool IsDatabaseLoaded();
     78 
     79   // Returns a pointer to the DB (used by SyncableServices). May return NULL if
     80   // the database is not loaded or otherwise unavailable. Must be called on
     81   // DBThread.
     82   virtual WebDatabase* GetDatabase();
     83 
     84  protected:
     85   friend class base::RefCountedDeleteOnMessageLoop<WebDataServiceBase>;
     86   friend class base::DeleteHelper<WebDataServiceBase>;
     87 
     88   virtual ~WebDataServiceBase();
     89 
     90   // Our database service.
     91   scoped_refptr<WebDatabaseService> wdbs_;
     92 
     93  private:
     94   ProfileErrorCallback profile_error_callback_;
     95 
     96   DISALLOW_COPY_AND_ASSIGN(WebDataServiceBase);
     97 };
     98 
     99 #endif  // COMPONENTS_WEBDATA_COMMON_WEB_DATA_SERVICE_BASE_H_
    100