1 // Copyright 2017 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 CONTENT_BROWSER_RENDERER_HOST_WEB_DATABASE_HOST_IMPL_H_ 6 #define CONTENT_BROWSER_RENDERER_HOST_WEB_DATABASE_HOST_IMPL_H_ 7 8 #include <string> 9 10 #include "base/callback_forward.h" 11 #include "base/strings/string16.h" 12 #include "build/build_config.h" 13 #include "content/common/content_export.h" 14 #include "mojo/public/cpp/bindings/pending_receiver.h" 15 #include "mojo/public/cpp/bindings/remote.h" 16 #include "storage/browser/database/database_tracker.h" 17 #include "third_party/blink/public/mojom/webdatabase/web_database.mojom.h" 18 19 namespace url { 20 class Origin; 21 } // namespace url 22 23 namespace content { 24 25 class CONTENT_EXPORT WebDatabaseHostImpl 26 : public blink::mojom::WebDatabaseHost, 27 public storage::DatabaseTracker::Observer { 28 public: 29 WebDatabaseHostImpl(int process_id, 30 scoped_refptr<storage::DatabaseTracker> db_tracker); 31 ~WebDatabaseHostImpl() override; 32 33 static void Create( 34 int process_id, 35 scoped_refptr<storage::DatabaseTracker> db_tracker, 36 mojo::PendingReceiver<blink::mojom::WebDatabaseHost> receiver); 37 38 private: 39 FRIEND_TEST_ALL_PREFIXES(WebDatabaseHostImplTest, BadMessagesUnauthorized); 40 FRIEND_TEST_ALL_PREFIXES(WebDatabaseHostImplTest, BadMessagesInvalid); 41 FRIEND_TEST_ALL_PREFIXES(WebDatabaseHostImplTest, ProcessShutdown); 42 43 // blink::mojom::WebDatabaseHost: 44 void OpenFile(const base::string16& vfs_file_name, 45 int32_t desired_flags, 46 OpenFileCallback callback) override; 47 48 void DeleteFile(const base::string16& vfs_file_name, 49 bool sync_dir, 50 DeleteFileCallback callback) override; 51 52 void GetFileAttributes(const base::string16& vfs_file_name, 53 GetFileAttributesCallback callback) override; 54 55 void GetFileSize(const base::string16& vfs_file_name, 56 GetFileSizeCallback callback) override; 57 58 void SetFileSize(const base::string16& vfs_file_name, 59 int64_t expected_size, 60 SetFileSizeCallback callback) override; 61 62 void GetSpaceAvailable(const url::Origin& origin, 63 GetSpaceAvailableCallback callback) override; 64 65 void Opened(const url::Origin& origin, 66 const base::string16& database_name, 67 const base::string16& database_description, 68 int64_t estimated_size) override; 69 70 void Modified(const url::Origin& origin, 71 const base::string16& database_name) override; 72 73 void Closed(const url::Origin& origin, 74 const base::string16& database_name) override; 75 76 void HandleSqliteError(const url::Origin& origin, 77 const base::string16& database_name, 78 int32_t error) override; 79 80 // DatabaseTracker::Observer callbacks (tracker sequence) 81 void OnDatabaseSizeChanged(const std::string& origin_identifier, 82 const base::string16& database_name, 83 int64_t database_size) override; 84 void OnDatabaseScheduledForDeletion( 85 const std::string& origin_identifier, 86 const base::string16& database_name) override; 87 88 void DatabaseDeleteFile(const base::string16& vfs_file_name, 89 bool sync_dir, 90 DeleteFileCallback callback, 91 int reschedule_count); 92 93 // Helper function to get the mojo interface for the WebDatabase on the 94 // render process. Creates the WebDatabase connection if it does not already 95 // exist. 96 blink::mojom::WebDatabase& GetWebDatabase(); 97 98 // blink::mojom::WebDatabaseHost methods called after ValidateOrigin() 99 // successfully validates the origin. 100 void OpenFileValidated(const base::string16& vfs_file_name, 101 int32_t desired_flags, 102 OpenFileCallback callback); 103 104 void GetFileAttributesValidated(const base::string16& vfs_file_name, 105 GetFileAttributesCallback callback); 106 107 void GetFileSizeValidated(const base::string16& vfs_file_name, 108 GetFileSizeCallback callback); 109 110 void SetFileSizeValidated(const base::string16& vfs_file_name, 111 int64_t expected_size, 112 SetFileSizeCallback callback); 113 114 void GetSpaceAvailableValidated(const url::Origin& origin, 115 GetSpaceAvailableCallback callback); 116 117 void OpenedValidated(const url::Origin& origin, 118 const base::string16& database_name, 119 const base::string16& database_description, 120 int64_t estimated_size); 121 122 void ModifiedValidated(const url::Origin& origin, 123 const base::string16& database_name); 124 125 void ClosedValidated(const url::Origin& origin, 126 const base::string16& database_name); 127 128 void HandleSqliteErrorValidated(const url::Origin& origin, 129 const base::string16& database_name, 130 int32_t error); 131 132 // Asynchronously calls |callback| but only if |process_id_| has permission to 133 // access the passed |origin|. Must be called from within the context of a 134 // mojo call. Invalid calls will report a bad message, which will terminate 135 // the calling process. 136 void ValidateOrigin(const url::Origin& origin, base::OnceClosure callback); 137 138 // As above, but for calls where the origin is embedded in a VFS filename. 139 // Empty filenames signalling a temp file are permitted. 140 void ValidateOrigin(const base::string16& vfs_file_name, 141 base::OnceClosure callback); 142 // Our render process host ID, used to bind to the correct render process. 143 const int process_id_; 144 145 // True if and only if this instance was added as an observer 146 // to DatabaseTracker. 147 bool observer_added_; 148 149 // Keeps track of all DB connections opened by this renderer 150 storage::DatabaseConnections database_connections_; 151 152 // Interface to the render process WebDatabase. 153 mojo::Remote<blink::mojom::WebDatabase> database_provider_; 154 155 // The database tracker for the current browser context. 156 const scoped_refptr<storage::DatabaseTracker> db_tracker_; 157 158 // Note: This should remain the last member so it'll be destroyed and 159 // invalidate its weak pointers before any other members are destroyed. 160 base::WeakPtrFactory<WebDatabaseHostImpl> weak_ptr_factory_{this}; 161 }; 162 163 } // namespace content 164 165 #endif // CONTENT_BROWSER_RENDERER_HOST_WEB_DATABASE_HOST_IMPL_H_ 166