Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / common / extensions / api / storage / storage_schema_manifest_handler.cc
blob861bd1bd8e47d54becded7bab26cb4119cb1b21b
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"
7 #include <string>
8 #include <vector>
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"
27 #endif
29 using extensions::manifest_keys::kStorageManagedSchema;
31 namespace extensions {
33 StorageSchemaManifestHandler::StorageSchemaManifestHandler() {}
35 StorageSchemaManifestHandler::~StorageSchemaManifestHandler() {}
37 #if defined(ENABLE_CONFIGURATION_POLICY)
38 // static
39 policy::Schema StorageSchemaManifestHandler::GetSchema(
40 const Extension* extension,
41 std::string* error) {
42 std::string path;
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)) {
52 *error =
53 base::StringPrintf("File does not exist: %s", file.value().c_str());
54 return policy::Schema();
56 std::string content;
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);
63 #endif
65 bool StorageSchemaManifestHandler::Parse(Extension* extension,
66 base::string16* error) {
67 std::string path;
68 if (!extension->manifest()->GetString(kStorageManagedSchema, &path)) {
69 *error = base::ASCIIToUTF16(
70 base::StringPrintf("%s must be a string", kStorageManagedSchema));
71 return false;
74 // If an extension declares the "storage.managed_schema" key then it gets
75 // the "storage" permission implicitly.
76 PermissionsParser::AddAPIPermission(extension, APIPermission::kStorage);
78 return true;
81 bool StorageSchemaManifestHandler::Validate(
82 const Extension* extension,
83 std::string* error,
84 std::vector<InstallWarning>* warnings) const {
85 #if defined(ENABLE_CONFIGURATION_POLICY)
86 return GetSchema(extension, error).valid();
87 #else
88 return true;
89 #endif
92 const std::vector<std::string> StorageSchemaManifestHandler::Keys() const {
93 return SingleKey(kStorageManagedSchema);
96 } // namespace extensions