OLD | NEW |
---|---|
(Empty) | |
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 EXTENSIONS_BROWSER_API_LOCK_SCREEN_DATA_DATA_ITEM_H_ | |
6 #define EXTENSIONS_BROWSER_API_LOCK_SCREEN_DATA_DATA_ITEM_H_ | |
7 | |
8 #include <memory> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/callback.h" | |
13 #include "base/files/file_path.h" | |
14 #include "base/memory/ref_counted.h" | |
15 #include "base/memory/weak_ptr.h" | |
16 | |
17 namespace base { | |
18 class DictionaryValue; | |
19 class SequencedTaskRunner; | |
20 class Value; | |
21 } | |
22 | |
23 namespace extensions { | |
24 namespace lock_screen_data { | |
25 | |
26 enum class OperationResult; | |
27 | |
28 // Wrapper around a file backed lock screen data item. | |
29 class DataItem { | |
30 public: | |
31 using WriteCallback = base::Callback<void(OperationResult result)>; | |
32 using ReadCallback = | |
33 base::Callback<void(OperationResult result, | |
34 std::unique_ptr<std::vector<char>> data)>; | |
35 using DeleteCallback = base::Callback<void(OperationResult result)>; | |
36 | |
37 explicit DataItem(const std::string& id); | |
38 virtual ~DataItem(); | |
39 | |
40 // Converts the data item to a dictionary value. | |
41 std::unique_ptr<base::DictionaryValue> ToValue(); | |
42 | |
43 // Initializes the data item from a value - value is expected to be a | |
44 // dictionary value, with id property set to this DataItem's id. | |
45 // Returns whether the item was successfully initialized. | |
46 bool InitFromValue(const base::Value& value); | |
47 | |
48 // Encrypts |data| with |crypto_key| and writes is to the file backing this | |
49 // data item - it will fail id |backing_file_| is empty. | |
50 // Note - callback will be used only if the operation cannot be finished | |
51 // synchronously, in which case the method returns OperationResult::kPending. | |
52 virtual OperationResult Write( | |
rkc
2017/06/22 18:35:47
Why return a value at all? Why not just directly c
tbarzic
2017/06/26 22:21:42
Done.
| |
53 const std::vector<char>& data, | |
54 const std::string& crypto_key, | |
55 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | |
56 const WriteCallback& callback); | |
57 | |
58 // Reads data from the backing file and decrypts it using |crypto_key|. | |
59 // It will fail if |backing_file_| is empty. | |
60 // Note - callback will be used only if the operation cannot be finished | |
61 // synchronously, in which case the method returns OperationResult::kPending. | |
62 virtual OperationResult Read( | |
63 const std::string& crypto_key, | |
64 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | |
65 const ReadCallback& callback); | |
66 | |
67 // Resets this data item, and deletes the backing file. | |
68 // Note - callback will be used only if the operation cannot be finished | |
69 // synchronously, in which case the method returns OperationResult::kPending. | |
70 virtual OperationResult Delete( | |
71 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | |
72 const DeleteCallback& callback); | |
73 | |
74 const std::string& id() const { return id_; } | |
75 | |
76 const base::FilePath& backing_file() const { return backing_file_; } | |
77 | |
78 void set_backing_file(const base::FilePath& backing_file) { | |
79 backing_file_ = backing_file; | |
80 } | |
81 | |
82 private: | |
83 // Internal callback for write operation - wraps |callback| to ensure | |
84 // |callback| is not run after |this| has been destroyed. | |
85 void OnWriteDone(const WriteCallback& callback, | |
86 std::unique_ptr<OperationResult> result); | |
87 | |
88 // Internal callback for read operation - wraps |callback| to ensure | |
89 // |callback| is not run after |this| has been destroyed. | |
90 void OnReadDone(const ReadCallback& callback, | |
91 std::unique_ptr<OperationResult> result, | |
92 std::unique_ptr<std::vector<char>> data); | |
93 | |
94 // Internal callback for delete operation - wraps |callback| to ensure | |
95 // |callback| is not run after |this| has been destroyed. | |
96 void OnDeleteDone(const DeleteCallback& callback, | |
97 std::unique_ptr<OperationResult> result); | |
98 | |
99 std::string id_; | |
100 base::FilePath backing_file_; | |
101 | |
102 base::WeakPtrFactory<DataItem> weak_ptr_factory_; | |
103 | |
104 DISALLOW_COPY_AND_ASSIGN(DataItem); | |
105 }; | |
106 | |
107 } // namespace lock_screen_data | |
108 } // namespace extensions | |
109 | |
110 #endif // EXTENSIONS_BROWSER_API_LOCK_SCREEN_DATA_DATA_ITEM_H_ | |
OLD | NEW |