1 // Copyright 2013 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.
5 #include "chrome/common/extensions/api/storage/storage_schema_manifest_handler.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "extensions/common/extension.h"
17 #include "extensions/common/install_warning.h"
18 #include "extensions/common/manifest.h"
19 #include "extensions/common/manifest_constants.h"
20 #include "extensions/common/manifest_handlers/permissions_parser.h"
21 #include "extensions/common/permissions/api_permission.h"
22 #include "extensions/common/permissions/api_permission_set.h"
23 #include "extensions/common/permissions/permissions_info.h"
25 #if defined(ENABLE_CONFIGURATION_POLICY)
26 #include "components/policy/core/common/schema.h"
29 using extensions::manifest_keys::kStorageManagedSchema
;
31 namespace extensions
{
33 StorageSchemaManifestHandler::StorageSchemaManifestHandler() {}
35 StorageSchemaManifestHandler::~StorageSchemaManifestHandler() {}
37 #if defined(ENABLE_CONFIGURATION_POLICY)
39 policy::Schema
StorageSchemaManifestHandler::GetSchema(
40 const Extension
* extension
,
43 extension
->manifest()->GetString(kStorageManagedSchema
, &path
);
44 base::FilePath file
= base::FilePath::FromUTF8Unsafe(path
);
45 if (file
.IsAbsolute() || file
.ReferencesParent()) {
46 *error
= base::StringPrintf("%s must be a relative path without ..",
47 kStorageManagedSchema
);
48 return policy::Schema();
50 file
= extension
->path().AppendASCII(path
);
51 if (!base::PathExists(file
)) {
53 base::StringPrintf("File does not exist: %s", file
.value().c_str());
54 return policy::Schema();
57 if (!base::ReadFileToString(file
, &content
)) {
58 *error
= base::StringPrintf("Can't read %s", file
.value().c_str());
59 return policy::Schema();
61 return policy::Schema::Parse(content
, error
);
65 bool StorageSchemaManifestHandler::Parse(Extension
* extension
,
66 base::string16
* error
) {
68 if (!extension
->manifest()->GetString(kStorageManagedSchema
, &path
)) {
69 *error
= base::ASCIIToUTF16(
70 base::StringPrintf("%s must be a string", kStorageManagedSchema
));
74 // If an extension declares the "storage.managed_schema" key then it gets
75 // the "storage" permission implicitly.
76 PermissionsParser::AddAPIPermission(extension
, APIPermission::kStorage
);
81 bool StorageSchemaManifestHandler::Validate(
82 const Extension
* extension
,
84 std::vector
<InstallWarning
>* warnings
) const {
85 #if defined(ENABLE_CONFIGURATION_POLICY)
86 return GetSchema(extension
, error
).valid();
92 const std::vector
<std::string
> StorageSchemaManifestHandler::Keys() const {
93 return SingleKey(kStorageManagedSchema
);
96 } // namespace extensions