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
16class WebDatabase;
17class WebDatabaseService;
18class WebDatabaseTable;
19
20namespace base {
21class Thread;
22}
23
24// Base for WebDataService class hierarchy.
25// WebDataServiceBase is destroyed on the UI thread.
26class 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 without actually shutting down the service.  This can
66  // be used to temporarily reduce the browser process' memory footprint.
67  void UnloadDatabase();
68
69  // Unloads the database permanently and shuts down service.
70  void ShutdownDatabase();
71
72  // Register a callback to be notified that the database has loaded. Multiple
73  // callbacks may be registered, and each will be called at most once
74  // (following a successful database load), then cleared.
75  // Note: if the database load is already complete, then the callback will NOT
76  // be stored or called.
77  virtual void RegisterDBLoadedCallback(const DBLoadedCallback& callback);
78
79  // Returns true if the database load has completetd successfully, and
80  // ShutdownOnUIThread has not yet been called.
81  virtual bool IsDatabaseLoaded();
82
83  // Returns a pointer to the DB (used by SyncableServices). May return NULL if
84  // the database is not loaded or otherwise unavailable. Must be called on
85  // DBThread.
86  virtual WebDatabase* GetDatabase();
87
88 protected:
89  friend class base::RefCountedDeleteOnMessageLoop<WebDataServiceBase>;
90  friend class base::DeleteHelper<WebDataServiceBase>;
91
92  virtual ~WebDataServiceBase();
93
94  // Our database service.
95  scoped_refptr<WebDatabaseService> wdbs_;
96
97 private:
98  ProfileErrorCallback profile_error_callback_;
99
100  DISALLOW_COPY_AND_ASSIGN(WebDataServiceBase);
101};
102
103#endif  // COMPONENTS_WEBDATA_COMMON_WEB_DATA_SERVICE_BASE_H_
104