1 // Copyright 2014 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_INDEXED_DB_INDEXED_DB_EXTERNAL_OBJECT_H_
6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_EXTERNAL_OBJECT_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <string>
12 #include <vector>
13 
14 #include "base/callback.h"
15 #include "base/files/file_path.h"
16 #include "base/optional.h"
17 #include "base/strings/string16.h"
18 #include "base/time/time.h"
19 #include "content/browser/indexed_db/indexed_db_leveldb_coding.h"
20 #include "content/common/content_export.h"
21 #include "mojo/public/cpp/bindings/shared_remote.h"
22 #include "storage/browser/blob/blob_data_handle.h"
23 #include "third_party/blink/public/mojom/blob/blob.mojom.h"
24 #include "third_party/blink/public/mojom/indexeddb/indexeddb.mojom.h"
25 
26 namespace content {
27 
28 class CONTENT_EXPORT IndexedDBExternalObject {
29  public:
30   // Used for files with unknown size.
31   const static int64_t kUnknownSize = -1;
32 
33   // Partially converts a list of |objects| to their mojo representation. The
34   // mojo representation won't be complete until later
35   // IndexedDBDispatcherHost::CreateAllExternalObjects is also called with the
36   // same parameters.
37   static void ConvertToMojo(
38       const std::vector<IndexedDBExternalObject>& objects,
39       std::vector<blink::mojom::IDBExternalObjectPtr>* mojo_objects);
40 
41   IndexedDBExternalObject();
42   // These two are used for Blobs.
43   IndexedDBExternalObject(mojo::PendingRemote<blink::mojom::Blob> blob_remote,
44                           const std::string& uuid,
45                           const base::string16& type,
46                           int64_t size);
47   IndexedDBExternalObject(const base::string16& type,
48                           int64_t size,
49                           int64_t blob_number);
50   // These two are used for Files.
51   // The |last_modified| time here is stored in two places - first in the
52   // leveldb database, and second as the last_modified time of the file written
53   // to disk. If these don't match, then something modified the file on disk and
54   // it should be considered corrupt.
55   IndexedDBExternalObject(mojo::PendingRemote<blink::mojom::Blob> blob_remote,
56                           const std::string& uuid,
57                           const base::string16& file_name,
58                           const base::string16& type,
59                           const base::Time& last_modified,
60                           const int64_t size);
61   IndexedDBExternalObject(int64_t blob_number,
62                           const base::string16& type,
63                           const base::string16& file_name,
64                           const base::Time& last_modified,
65                           const int64_t size);
66   // These are for Native File System handles.
67   IndexedDBExternalObject(
68       mojo::PendingRemote<blink::mojom::NativeFileSystemTransferToken>
69           token_remote);
70   IndexedDBExternalObject(std::vector<uint8_t> native_file_system_token);
71 
72   IndexedDBExternalObject(const IndexedDBExternalObject& other);
73   ~IndexedDBExternalObject();
74   IndexedDBExternalObject& operator=(const IndexedDBExternalObject& other);
75 
76   // These values are serialized to disk.
77   enum class ObjectType : uint8_t {
78     kBlob = 0,
79     kFile = 1,
80     kNativeFileSystemHandle = 2,
81     kMaxValue = kNativeFileSystemHandle
82   };
object_type()83   ObjectType object_type() const { return object_type_; }
is_remote_valid()84   bool is_remote_valid() const { return blob_remote_.is_bound(); }
uuid()85   const std::string& uuid() const {
86     DCHECK(is_remote_valid());
87     return uuid_;
88   }
89   void Clone(mojo::PendingReceiver<blink::mojom::Blob> receiver) const;
remote()90   mojo::SharedRemote<blink::mojom::Blob> remote() const { return blob_remote_; }
type()91   const base::string16& type() const { return type_; }
size()92   int64_t size() const { return size_; }
file_name()93   const base::string16& file_name() const { return file_name_; }
indexed_db_file_path()94   const base::FilePath indexed_db_file_path() const {
95     return indexed_db_file_path_;
96   }
blob_number()97   int64_t blob_number() const { return blob_number_; }
last_modified()98   const base::Time& last_modified() const { return last_modified_; }
native_file_system_token()99   const std::vector<uint8_t> native_file_system_token() const {
100     return native_file_system_token_;
101   }
is_native_file_system_remote_valid()102   bool is_native_file_system_remote_valid() const {
103     return token_remote_.is_bound();
104   }
native_file_system_token_remote()105   blink::mojom::NativeFileSystemTransferToken* native_file_system_token_remote()
106       const {
107     return token_remote_.get();
108   }
mark_used_callback()109   const base::RepeatingClosure& mark_used_callback() const {
110     return mark_used_callback_;
111   }
release_callback()112   const base::RepeatingClosure& release_callback() const {
113     return release_callback_;
114   }
115 
116   void set_size(int64_t size);
117   void set_indexed_db_file_path(const base::FilePath& file_path);
118   void set_last_modified(const base::Time& time);
119   void set_native_file_system_token(std::vector<uint8_t> token);
120   void set_blob_number(int64_t blob_number);
121   void set_mark_used_callback(base::RepeatingClosure mark_used_callback);
122   void set_release_callback(base::RepeatingClosure release_callback);
123 
124  private:
125   ObjectType object_type_;
126 
127   // Always for Blob; sometimes for File.
128   mojo::SharedRemote<blink::mojom::Blob> blob_remote_;
129   // If blob_remote_ is true, this is the blob's uuid.
130   std::string uuid_;
131   // Mime type.
132   base::string16 type_;
133   // This is the path of the file that was copied into the IndexedDB system.
134   // Only populated when reading from the database.
135   base::FilePath indexed_db_file_path_;
136   // -1 if unknown for File.
137   int64_t size_ = kUnknownSize;
138   // Only for File.
139   base::string16 file_name_;
140   // Only for File; valid only if size is.
141   base::Time last_modified_;
142 
143   // Only for Native File System handle.
144   mojo::SharedRemote<blink::mojom::NativeFileSystemTransferToken> token_remote_;
145   std::vector<uint8_t> native_file_system_token_;
146 
147   // Valid only when this comes out of the database. Only for Blob and File.
148   int64_t blob_number_ = DatabaseMetaDataKey::kInvalidBlobNumber;
149   base::RepeatingClosure mark_used_callback_;
150   base::RepeatingClosure release_callback_;
151 };
152 
153 }  // namespace content
154 
155 #endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_EXTERNAL_OBJECT_H_
156