1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef EXTENSIONS_BROWSER_API_STORAGE_STORAGE_API_H_
#define EXTENSIONS_BROWSER_API_STORAGE_STORAGE_API_H_
#include "base/compiler_specific.h"
#include "components/value_store/value_store.h"
#include "extensions/browser/api/storage/session_storage_manager.h"
#include "extensions/browser/api/storage/settings_namespace.h"
#include "extensions/browser/api/storage/settings_observer.h"
#include "extensions/browser/api/storage/storage_area_namespace.h"
#include "extensions/browser/api/storage/storage_frontend.h"
#include "extensions/browser/extension_function.h"
namespace extensions {
// Superclass of all settings functions.
class SettingsFunction : public ExtensionFunction {
protected:
SettingsFunction();
~SettingsFunction() override;
// ExtensionFunction:
bool ShouldSkipQuotaLimiting() const override;
bool PreRunValidation(std::string* error) override;
// Returns whether the caller's context has access to the storage or not.
bool IsAccessToStorageAllowed();
StorageAreaNamespace storage_area() const { return storage_area_; }
void OnWriteOperationFinished(StorageFrontend::ResultStatus status);
private:
// The Storage Area the call was for. For example: kLocal if the API call was
// chrome.storage.local, kSync if the API call was chrome.storage.sync, etc.
StorageAreaNamespace storage_area_ = StorageAreaNamespace::kInvalid;
// The settings namespace the call was for. Only includes
// StorageAreaNamespace's that use ValueStore.
settings_namespace::Namespace settings_namespace_ =
settings_namespace::INVALID;
};
class StorageStorageAreaGetFunction : public SettingsFunction {
public:
DECLARE_EXTENSION_FUNCTION("storage.get", STORAGE_GET)
protected:
~StorageStorageAreaGetFunction() override {}
// SettingsFunction:
ResponseAction Run() override;
// Called after getting data from storage. If `defaults` is provided, merges
// the data from `result` into the dictionary. This allows developers to
// provide a fallback for data not present in storage.
void OnGetOperationFinished(std::optional<base::Value::Dict> defaults,
StorageFrontend::GetResult result);
};
class StorageStorageAreaGetKeysFunction : public SettingsFunction {
public:
DECLARE_EXTENSION_FUNCTION("storage.getKeys", STORAGE_GETKEYS)
protected:
~StorageStorageAreaGetKeysFunction() override = default;
// SettingsFunction:
ResponseAction Run() override;
// Called after getting keys from storage.
void OnGetKeysOperationFinished(StorageFrontend::GetKeysResult result);
};
class StorageStorageAreaSetFunction : public SettingsFunction {
public:
DECLARE_EXTENSION_FUNCTION("storage.set", STORAGE_SET)
protected:
~StorageStorageAreaSetFunction() override {}
// SettingsFunction:
ResponseAction Run() override;
// ExtensionFunction:
void GetQuotaLimitHeuristics(QuotaLimitHeuristics* heuristics) const override;
};
class StorageStorageAreaRemoveFunction : public SettingsFunction {
public:
DECLARE_EXTENSION_FUNCTION("storage.remove", STORAGE_REMOVE)
protected:
~StorageStorageAreaRemoveFunction() override {}
// SettingsFunction:
ResponseAction Run() override;
// ExtensionFunction:
void GetQuotaLimitHeuristics(QuotaLimitHeuristics* heuristics) const override;
};
class StorageStorageAreaClearFunction : public SettingsFunction {
public:
DECLARE_EXTENSION_FUNCTION("storage.clear", STORAGE_CLEAR)
protected:
~StorageStorageAreaClearFunction() override {}
// SettingsFunction:
ResponseAction Run() override;
// ExtensionFunction:
void GetQuotaLimitHeuristics(QuotaLimitHeuristics* heuristics) const override;
};
class StorageStorageAreaGetBytesInUseFunction : public SettingsFunction {
public:
DECLARE_EXTENSION_FUNCTION("storage.getBytesInUse", STORAGE_GETBYTESINUSE)
FRIEND_TEST_ALL_PREFIXES(StorageApiUnittest, GetBytesInUseIntOverflow);
protected:
~StorageStorageAreaGetBytesInUseFunction() override {}
// SettingsFunction:
ResponseAction Run() override;
// Called after retrieving bytes from storage.
void OnGetBytesInUseOperationFinished(size_t);
};
class StorageStorageAreaSetAccessLevelFunction : public SettingsFunction {
public:
DECLARE_EXTENSION_FUNCTION("storage.setAccessLevel", STORAGE_SETACCESSLEVEL)
StorageStorageAreaSetAccessLevelFunction() = default;
StorageStorageAreaSetAccessLevelFunction(
const StorageStorageAreaSetAccessLevelFunction&) = delete;
StorageStorageAreaSetAccessLevelFunction& operator=(
const StorageStorageAreaSetAccessLevelFunction&) = delete;
protected:
~StorageStorageAreaSetAccessLevelFunction() override = default;
// SettingsFunction:
ResponseAction Run() override;
};
} // namespace extensions
#endif // EXTENSIONS_BROWSER_API_STORAGE_STORAGE_API_H_
|