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

Unified Diff: extensions/browser/api/lock_screen_data/data_item.h

Issue 2934293003: The chrome.lockScreen.data API implementation (Closed)
Patch Set: remove FilePath*UTF8Unsafe methods Created 3 years, 6 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 side-by-side diff with in-line comments
Download patch
Index: extensions/browser/api/lock_screen_data/data_item.h
diff --git a/extensions/browser/api/lock_screen_data/data_item.h b/extensions/browser/api/lock_screen_data/data_item.h
new file mode 100644
index 0000000000000000000000000000000000000000..482e8c536aace8ad53e78d7236226d163f036122
--- /dev/null
+++ b/extensions/browser/api/lock_screen_data/data_item.h
@@ -0,0 +1,110 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef EXTENSIONS_BROWSER_API_LOCK_SCREEN_DATA_DATA_ITEM_H_
+#define EXTENSIONS_BROWSER_API_LOCK_SCREEN_DATA_DATA_ITEM_H_
+
+#include <memory>
+#include <string>
+#include <vector>
+
+#include "base/callback.h"
+#include "base/files/file_path.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/weak_ptr.h"
+
+namespace base {
+class DictionaryValue;
+class SequencedTaskRunner;
+class Value;
+}
+
+namespace extensions {
+namespace lock_screen_data {
+
+enum class OperationResult;
+
+// Wrapper around a file backed lock screen data item.
+class DataItem {
+ public:
+ using WriteCallback = base::Callback<void(OperationResult result)>;
+ using ReadCallback =
+ base::Callback<void(OperationResult result,
+ std::unique_ptr<std::vector<char>> data)>;
+ using DeleteCallback = base::Callback<void(OperationResult result)>;
+
+ explicit DataItem(const std::string& id);
+ virtual ~DataItem();
+
+ // Converts the data item to a dictionary value.
+ std::unique_ptr<base::DictionaryValue> ToValue();
+
+ // Initializes the data item from a value - value is expected to be a
+ // dictionary value, with id property set to this DataItem's id.
+ // Returns whether the item was successfully initialized.
+ bool InitFromValue(const base::Value& value);
+
+ // Encrypts |data| with |crypto_key| and writes is to the file backing this
+ // data item - it will fail id |backing_file_| is empty.
+ // Note - callback will be used only if the operation cannot be finished
+ // synchronously, in which case the method returns OperationResult::kPending.
+ 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.
+ const std::vector<char>& data,
+ const std::string& crypto_key,
+ const scoped_refptr<base::SequencedTaskRunner>& task_runner,
+ const WriteCallback& callback);
+
+ // Reads data from the backing file and decrypts it using |crypto_key|.
+ // It will fail if |backing_file_| is empty.
+ // Note - callback will be used only if the operation cannot be finished
+ // synchronously, in which case the method returns OperationResult::kPending.
+ virtual OperationResult Read(
+ const std::string& crypto_key,
+ const scoped_refptr<base::SequencedTaskRunner>& task_runner,
+ const ReadCallback& callback);
+
+ // Resets this data item, and deletes the backing file.
+ // Note - callback will be used only if the operation cannot be finished
+ // synchronously, in which case the method returns OperationResult::kPending.
+ virtual OperationResult Delete(
+ const scoped_refptr<base::SequencedTaskRunner>& task_runner,
+ const DeleteCallback& callback);
+
+ const std::string& id() const { return id_; }
+
+ const base::FilePath& backing_file() const { return backing_file_; }
+
+ void set_backing_file(const base::FilePath& backing_file) {
+ backing_file_ = backing_file;
+ }
+
+ private:
+ // Internal callback for write operation - wraps |callback| to ensure
+ // |callback| is not run after |this| has been destroyed.
+ void OnWriteDone(const WriteCallback& callback,
+ std::unique_ptr<OperationResult> result);
+
+ // Internal callback for read operation - wraps |callback| to ensure
+ // |callback| is not run after |this| has been destroyed.
+ void OnReadDone(const ReadCallback& callback,
+ std::unique_ptr<OperationResult> result,
+ std::unique_ptr<std::vector<char>> data);
+
+ // Internal callback for delete operation - wraps |callback| to ensure
+ // |callback| is not run after |this| has been destroyed.
+ void OnDeleteDone(const DeleteCallback& callback,
+ std::unique_ptr<OperationResult> result);
+
+ std::string id_;
+ base::FilePath backing_file_;
+
+ base::WeakPtrFactory<DataItem> weak_ptr_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(DataItem);
+};
+
+} // namespace lock_screen_data
+} // namespace extensions
+
+#endif // EXTENSIONS_BROWSER_API_LOCK_SCREEN_DATA_DATA_ITEM_H_

Powered by Google App Engine
This is Rietveld 408576698