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 CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_EXTERNAL_OBJECT_STORAGE_H_
6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_EXTERNAL_OBJECT_STORAGE_H_
7 
8 #include <stdint.h>
9 #include <map>
10 #include <string>
11 #include <vector>
12 
13 #include "base/callback_forward.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/optional.h"
16 #include "base/time/time.h"
17 #include "content/browser/indexed_db/indexed_db_external_object.h"
18 #include "content/browser/indexed_db/indexed_db_leveldb_coding.h"
19 #include "storage/common/file_system/file_system_mount_option.h"
20 #include "third_party/leveldatabase/src/include/leveldb/status.h"
21 
22 namespace content {
23 
24 // This file contains all of the classes & types used to store external objects
25 // (such as blobs) in IndexedDB. Currently it is messy because this is
26 // mid-refactor, but it will be cleaned up over time.
27 
28 enum class BlobWriteResult {
29   // There was an error writing the blobs.
30   kFailure,
31   // The blobs were written, and phase two should be scheduled asynchronously.
32   // The returned status will be ignored.
33   kRunPhaseTwoAsync,
34   // The blobs were written, and phase two should be run now. The returned
35   // status will be correctly propagated.
36   kRunPhaseTwoAndReturnResult,
37 };
38 
39 // This callback is used to signify that writing blobs is complete. The
40 // BlobWriteResult signifies if the operation succeeded or not, and the returned
41 // status is used to handle errors in the next part of the transcation commit
42 // lifecycle. Note: The returned status can only be used when the result is
43 // |kRunPhaseTwoAndReturnResult|.
44 using BlobWriteCallback = base::OnceCallback<leveldb::Status(BlobWriteResult)>;
45 
46 // This object represents a change in the database involving adding or removing
47 // external objects. if external_objects() is empty, then objects are to be
48 // deleted, and if external_objects() is populated, then objects are two be
49 // written (and also possibly deleted if there were already objects).
50 class IndexedDBExternalObjectChangeRecord {
51  public:
52   IndexedDBExternalObjectChangeRecord(const std::string& object_store_data_key);
53   ~IndexedDBExternalObjectChangeRecord();
54 
object_store_data_key()55   const std::string& object_store_data_key() const {
56     return object_store_data_key_;
57   }
58   void SetExternalObjects(
59       std::vector<IndexedDBExternalObject>* external_objects);
mutable_external_objects()60   std::vector<IndexedDBExternalObject>& mutable_external_objects() {
61     return external_objects_;
62   }
external_objects()63   const std::vector<IndexedDBExternalObject>& external_objects() const {
64     return external_objects_;
65   }
66   std::unique_ptr<IndexedDBExternalObjectChangeRecord> Clone() const;
67 
68  private:
69   std::string object_store_data_key_;
70   std::vector<IndexedDBExternalObject> external_objects_;
71   DISALLOW_COPY_AND_ASSIGN(IndexedDBExternalObjectChangeRecord);
72 };
73 
74 // Reports that the recovery and/or active journals have been processed, and
75 // blob files have been deleted.
76 using BlobFilesCleanedCallback = base::RepeatingClosure;
77 
78 // Reports that there are (or are not) active blobs.
79 using ReportOutstandingBlobsCallback =
80     base::RepeatingCallback<void(/*outstanding_blobs=*/bool)>;
81 
82 }  // namespace content
83 
84 #endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_EXTERNAL_OBJECT_STORAGE_H_
85