summaryrefslogtreecommitdiffstats
path: root/components/webdata/common/web_data_service_backend.h
blob: 0e1d8f247e7024761e519ddd4eabdb5f1bd1f299 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_WEBDATA_COMMON_WEB_DATA_SERVICE_BACKEND_H_
#define COMPONENTS_WEBDATA_COMMON_WEB_DATA_SERVICE_BACKEND_H_

#include "base/basictypes.h"
#include "base/callback_forward.h"
#include "base/compiler_specific.h"
#include "base/files/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "components/webdata/common/web_database_service.h"
#include "content/public/browser/browser_thread.h"


class WebDatabase;
class WebDatabaseTable;
class WebDataRequest;
class WebDataRequestManager;

namespace tracked_objects {
class Location;
}

// WebDataServiceBackend handles all database tasks posted by
// WebDatabaseService. It is refcounted to allow asynchronous destruction on the
// DB thread.

class WebDataServiceBackend
    : public base::RefCountedThreadSafe<
          WebDataServiceBackend,
          content::BrowserThread::DeleteOnDBThread> {
 public:
  class Delegate {
    public:
    virtual ~Delegate() {}

    // Invoked when the backend has finished loading the db.
    virtual void DBLoaded(sql::InitStatus status) = 0;
  };

  explicit WebDataServiceBackend(const base::FilePath& path,
                                 Delegate* delegate);

  // Must call only before InitDatabaseWithCallback.
  void AddTable(scoped_ptr<WebDatabaseTable> table);

  // Initializes the database and notifies caller via callback when complete.
  // Callback is called synchronously.
  void InitDatabase();

  // Opens the database file from the profile path if an init has not yet been
  // attempted. Separated from the constructor to ease construction/destruction
  // of this object on one thread but database access on the DB thread. Returns
  // the status of the database.
  sql::InitStatus LoadDatabaseIfNecessary();

  // Shuts down database. |should_reinit| tells us whether or not it should be
  // possible to re-initialize the DB after the shutdown.
  void ShutdownDatabase(bool should_reinit);

  // Task wrappers to run database tasks.
  void DBWriteTaskWrapper(
      const WebDatabaseService::WriteTask& task,
      scoped_ptr<WebDataRequest> request);
  void DBReadTaskWrapper(
      const WebDatabaseService::ReadTask& task,
      scoped_ptr<WebDataRequest> request);

  const scoped_refptr<WebDataRequestManager>& request_manager() {
    return request_manager_;
  }

  WebDatabase* database() { return db_.get(); }

 private:
  friend struct content::BrowserThread::DeleteOnThread<
      content::BrowserThread::DB>;
  friend class base::DeleteHelper<WebDataServiceBackend>;

  virtual ~WebDataServiceBackend();

  // Commit the current transaction.
  void Commit();

  // Path to database file.
  base::FilePath db_path_;

  // The tables that participate in managing the database. These are
  // owned here but other than that this class does nothing with
  // them. Their initialization is in whatever factory creates
  // WebDatabaseService, and lookup by type is provided by the
  // WebDatabase class. The tables need to be owned by this refcounted
  // object, or they themselves would need to be refcounted. Owning
  // them here rather than having WebDatabase own them makes for
  // easier unit testing of WebDatabase.
  ScopedVector<WebDatabaseTable> tables_;

  scoped_ptr<WebDatabase> db_;

  // Keeps track of all pending requests made to the db.
  scoped_refptr<WebDataRequestManager> request_manager_;

  // State of database initialization. Used to prevent the executing of tasks
  // before the db is ready.
  sql::InitStatus init_status_;

  // True if an attempt has been made to load the database (even if the attempt
  // fails), used to avoid continually trying to reinit if the db init fails.
  bool init_complete_;

  // Delegate. See the class definition above for more information.
  scoped_ptr<Delegate> delegate_;

  DISALLOW_COPY_AND_ASSIGN(WebDataServiceBackend);
};

#endif  // COMPONENTS_WEBDATA_COMMON_WEB_DATA_SERVICE_BACKEND_H_