summaryrefslogtreecommitdiffstats
path: root/webkit/support/simple_database_system.h
blob: 02bb66a501e9eb9c1ea7bd6ee610300e4ea863b0 (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
// Copyright (c) 2011 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 WEBKIT_SUPPORT_SIMPLE_DATABASE_SYSTEM_H_
#define WEBKIT_SUPPORT_SIMPLE_DATABASE_SYSTEM_H_

#include "base/file_path.h"
#include "base/hash_tables.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_temp_dir.h"
#include "base/platform_file.h"
#include "base/string16.h"
#include "base/synchronization/lock.h"
#include "base/task.h"
#include "base/threading/thread.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDatabaseObserver.h"
#include "webkit/database/database_connections.h"
#include "webkit/database/database_tracker.h"

namespace base {
class MessageLoopProxy;
class WaitableEvent;
}

class SimpleDatabaseSystem : public webkit_database::DatabaseTracker::Observer,
                             public WebKit::WebDatabaseObserver {
 public:
  static SimpleDatabaseSystem* GetInstance();

  SimpleDatabaseSystem();
  ~SimpleDatabaseSystem();

  // WebDatabaseObserver implementation, these are called on the script
  // execution context thread on which the database is opened. This may be
  // the main thread or background WebWorker threads.
  virtual void databaseOpened(const WebKit::WebDatabase& database);
  virtual void databaseModified(const WebKit::WebDatabase& database);
  virtual void databaseClosed(const WebKit::WebDatabase& database);

  // SQLite VFS related methods, these are called on webcore's
  // background database threads via the WebKitClient impl.
  base::PlatformFile OpenFile(const string16& vfs_file_name, int desired_flags);
  int DeleteFile(const string16& vfs_file_name, bool sync_dir);
  uint32 GetFileAttributes(const string16& vfs_file_name);
  int64 GetFileSize(const string16& vfs_file_name);

  // For use by LayoutTestController, called on the main thread.
  void ClearAllDatabases();
  void SetDatabaseQuota(int64 quota);

 private:
  // Used by our WebDatabaseObserver impl, only called on the db_thread
  void DatabaseOpened(const string16& origin_identifier,
                      const string16& database_name,
                      const string16& description,
                      int64 estimated_size);
  void DatabaseModified(const string16& origin_identifier,
                        const string16& database_name);
  void DatabaseClosed(const string16& origin_identifier,
                      const string16& database_name);

  // DatabaseTracker::Observer implementation
  virtual void OnDatabaseSizeChanged(const string16& origin_identifier,
                                     const string16& database_name,
                                     int64 database_size,
                                     int64 space_available);
  virtual void OnDatabaseScheduledForDeletion(const string16& origin_identifier,
                                              const string16& database_name);

  // Used by our public SQLite VFS methods, only called on the db_thread.
  void VfsOpenFile(const string16& vfs_file_name, int desired_flags,
                   base::PlatformFile* result, base::WaitableEvent* done_event);
  void VfsDeleteFile(const string16& vfs_file_name, bool sync_dir,
                     int* result, base::WaitableEvent* done_event);
  void VfsGetFileAttributes(const string16& vfs_file_name,
                            uint32* result, base::WaitableEvent* done_event);
  void VfsGetFileSize(const string16& vfs_file_name,
                      int64* result, base::WaitableEvent* done_event);

  FilePath GetFullFilePathForVfsFile(const string16& vfs_file_name);

  void ResetTracker();
  void ThreadCleanup(base::WaitableEvent* done_event);

  // Where the tracker database file and per origin database files reside.
  ScopedTempDir temp_dir_;

  // All access to the db_tracker (except for its construction) and
  // vfs operations are serialized on a background thread.
  base::Thread db_thread_;
  scoped_refptr<base::MessageLoopProxy> db_thread_proxy_;
  scoped_refptr<webkit_database::DatabaseTracker> db_tracker_;

  // Data members to support waiting for all connections to be closed.
  scoped_refptr<webkit_database::DatabaseConnectionsWrapper> open_connections_;

  static SimpleDatabaseSystem* instance_;
};

DISABLE_RUNNABLE_METHOD_REFCOUNT(SimpleDatabaseSystem);

#endif  // WEBKIT_SUPPORT_SIMPLE_DATABASE_SYSTEM_H_