1 // Copyright 2019 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 THIRD_PARTY_BLINK_RENDERER_MODULES_WEBDATABASE_WEB_DATABASE_HOST_H_
6 #define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBDATABASE_WEB_DATABASE_HOST_H_
7 
8 #include <stdint.h>
9 
10 #include "base/files/file.h"
11 #include "base/macros.h"
12 #include "mojo/public/cpp/bindings/pending_remote.h"
13 #include "mojo/public/cpp/bindings/shared_remote.h"
14 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
15 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
16 
17 namespace base {
18 class SingleThreadTaskRunner;
19 }
20 
21 namespace blink {
22 
23 namespace mojom {
24 namespace blink {
25 class WebDatabaseHost;
26 }  // namespace blink
27 }  // namespace mojom
28 
29 class SecurityOrigin;
30 
31 // Singleton owning a remote connection to the mojo::WebDatabaseHost implementor
32 // in the browser process. The remote interface gets bound when initializing the
33 // webdatabase module (from the main thread), but the actual remote object will
34 // be created the first time a method gets invoked (from the Database thread).
35 class WebDatabaseHost {
36   USING_FAST_MALLOC(WebDatabaseHost);
37 
38  public:
39   static WebDatabaseHost& GetInstance();
40 
41   // Should be called once before trying to use this class, so that we make sure
42   // the remote interface binding is done from the main thread before the first
43   // time GetInstance() is invoked (which will happen from the Database thread).
44   void Init();
45 
46   // Init() must have been invoked first before using any of the methods below.
47   base::File OpenFile(const String& vfs_file_name, int desired_flags);
48   int DeleteFile(const String& vfs_file_name, bool sync_dir);
49   int32_t GetFileAttributes(const String& vfs_file_name);
50   int64_t GetFileSize(const String& vfs_file_name);
51   bool SetFileSize(const String& vfs_file_name, int64_t size);
52   int64_t GetSpaceAvailableForOrigin(const SecurityOrigin& origin);
53 
54   void DatabaseOpened(const SecurityOrigin& origin,
55                       const String& database_name,
56                       const String& database_display_name,
57                       uint32_t estimated_size);
58   void DatabaseModified(const SecurityOrigin& origin,
59                         const String& database_name);
60   void DatabaseClosed(const SecurityOrigin& origin,
61                       const String& database_name);
62   void ReportSqliteError(const SecurityOrigin& origin,
63                          const String& database_name,
64                          int error);
65 
66  private:
67   WebDatabaseHost();
68   ~WebDatabaseHost() = default;
69 
70   // Returns an initialized mojom::blink::WebDatabaseHost remote. A connection
71   // will be established after the first call to this method.
72   mojom::blink::WebDatabaseHost& GetWebDatabaseHost();
73 
74   // Needed to bind the pending remote from the constructor, in the main thread.
75   mojo::PendingRemote<mojom::blink::WebDatabaseHost> pending_remote_;
76 
77   // Need a SharedRemote as method calls will happen from the Database thread.
78   mojo::SharedRemote<mojom::blink::WebDatabaseHost> shared_remote_;
79 
80   // Used to ensure that the database gets opened from the main thread, but that
81   // other database-related event is reported from the database thread instead.
82   scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;
83 
84   DISALLOW_COPY_AND_ASSIGN(WebDatabaseHost);
85 };
86 
87 }  // namespace blink
88 
89 #endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBDATABASE_WEB_DATABASE_HOST_H_
90