Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: extensions/browser/api/lock_screen_data/data_item.h

Issue 2934293003: The chrome.lockScreen.data API implementation (Closed)
Patch Set: . Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h"
15
16 namespace content {
17 class BrowserContext;
18 }
19
20 namespace base {
21 class DictionaryValue;
22 class SequencedTaskRunner;
23 } // namespace base
24
25 namespace extensions {
26
27 class ValueStoreCache;
28
29 namespace lock_screen_data {
30
31 enum class OperationResult;
32
33 // Wrapper around a file backed lock screen data item.
34 class DataItem {
35 public:
36 using WriteCallback = base::Callback<void(OperationResult result)>;
37 using ReadCallback =
38 base::Callback<void(OperationResult result,
39 std::unique_ptr<std::vector<char>> data)>;
40 using RegisteredValuesCallback =
41 base::Callback<void(OperationResult result,
42 std::unique_ptr<base::DictionaryValue> values)>;
43
44 // Gets all registered data items for the extension with the provided
45 // extension ID - the items are returned as a DictionaryValue with keys set
46 // to data item IDs.
47 static void GetRegisteredValuesForExtension(
48 content::BrowserContext* context,
49 ValueStoreCache* value_store_cache,
50 base::SequencedTaskRunner* task_runner,
51 const std::string& extension_id,
52 const RegisteredValuesCallback& callback);
53
54 // Clears data item value store for the extension with the provided extension
55 // ID.
56 static void DeleteAllItemsForExtension(content::BrowserContext* context,
57 ValueStoreCache* value_store_cache,
58 base::SequencedTaskRunner* task_runner,
59 const std::string& extension_id,
60 const base::Closure& callback);
61
62 // |id| - Data item ID.
63 // |extension_id| - The extension that owns the item.
64 // |context| - The browser context to which the owning extension is attached.
65 // |value_store_cache| - Used to retrieve value store for the extension with
66 // |extension_id| ID. Not owned - the caller should ensure the value store
67 // cache outlives the data item.
68 // |task_runner| - The task runner on which value store should be used. Note
69 // that the Data item does not retain a reference to the task runner -
70 // the caller should ensure |task_runner| outlives the data item.
71 // |crypto_key| - Symmetric AES key for encrypting/decrypting data item
72 // content.
73 DataItem(const std::string& id,
74 const std::string& extension_id,
75 content::BrowserContext* context,
76 ValueStoreCache* value_store_cache,
77 base::SequencedTaskRunner* task_runner,
78 const std::string& crypto_key);
79 virtual ~DataItem();
80
81 // Resisters the data item in the persistent data item storage.
82 virtual void Register(const WriteCallback& callback);
83
84 // Sets the data item content, saving it to persistent data storage.
85 // This will fail if the data item is not registered.
86 virtual void Write(const std::vector<char>& data,
87 const WriteCallback& callback);
88
89 // Gets the data item content from the persistent data storage.
90 // This will fail is the item is not registered.
91 virtual void Read(const ReadCallback& callback);
92
93 // Unregisters the data item, and clears previously persisted data item
94 // content.
95 virtual void Delete(const WriteCallback& callback);
96
97 const std::string& id() const { return id_; }
98
99 const std::string& extension_id() const { return extension_id_; }
100
101 private:
102 // Internal callback for write operations - wraps |callback| to ensure
103 // |callback| is not run after |this| has been destroyed.
104 void OnWriteDone(const WriteCallback& callback,
105 std::unique_ptr<OperationResult> result);
106
107 // Internal callback for the read operation - wraps |callback| to ensure
108 // |callback| is not run after |this| has been destroyed.
109 void OnReadDone(const ReadCallback& callback,
110 std::unique_ptr<OperationResult> result,
111 std::unique_ptr<std::vector<char>> data);
112
113 // The data item ID.
114 std::string id_;
115 // The ID of the extension that owns the data item.
116 std::string extension_id_;
117
118 content::BrowserContext* context_;
119 // Cache used to retrieve the values store to which the data item should be
120 // saved - the value stores are mapped by the extension ID.
121 ValueStoreCache* value_store_cache_;
122 // Task runner on which value store should be accessed.
123 base::SequencedTaskRunner* task_runner_;
124 // They symmetric AES key that should be used to encrypt data item content
125 // when the content is written to the storage, and to decrypt item content
126 // when reading it from the storage.
127 const std::string crypto_key_;
128
129 base::WeakPtrFactory<DataItem> weak_ptr_factory_;
130
131 DISALLOW_COPY_AND_ASSIGN(DataItem);
132 };
133
134 } // namespace lock_screen_data
135 } // namespace extensions
136
137 #endif // EXTENSIONS_BROWSER_API_LOCK_SCREEN_DATA_DATA_ITEM_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698