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.
4
5 #include "chrome/common/extensions/api/storage/storage_schema_manifest_handler.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/file_util.h"
11 #include "base/files/file_path.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/permissions/api_permission.h"
21 #include "extensions/common/permissions/api_permission_set.h"
22 #include "extensions/common/permissions/permissions_data.h"
23 #include "extensions/common/permissions/permissions_info.h"
24
25 #if defined(ENABLE_CONFIGURATION_POLICY)
26 #include "components/policy/core/common/schema.h"
27 #endif
28
29 using extensions::manifest_keys::kStorageManagedSchema;
30
31 namespace extensions {
32
StorageSchemaManifestHandler()33 StorageSchemaManifestHandler::StorageSchemaManifestHandler() {}
34
~StorageSchemaManifestHandler()35 StorageSchemaManifestHandler::~StorageSchemaManifestHandler() {}
36
37 #if defined(ENABLE_CONFIGURATION_POLICY)
38 // static
GetSchema(const Extension * extension,std::string * error)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();
49 }
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();
55 }
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();
60 }
61 return policy::Schema::Parse(content, error);
62 }
63 #endif
64
Parse(Extension * extension,base::string16 * error)65 bool StorageSchemaManifestHandler::Parse(Extension* extension,
66 base::string16* error) {
67 std::string path;
68 if (!extension->manifest()->GetString(kStorageManagedSchema, &path)) {
69 *error = ASCIIToUTF16(
70 base::StringPrintf("%s must be a string", kStorageManagedSchema));
71 return false;
72 }
73
74 // If an extension declares the "storage.managed_schema" key then it gets
75 // the "storage" permission implicitly.
76 APIPermissionSet* permission_set =
77 PermissionsData::GetInitialAPIPermissions(extension);
78 permission_set->insert(APIPermission::kStorage);
79
80 return true;
81 }
82
Validate(const Extension * extension,std::string * error,std::vector<InstallWarning> * warnings) const83 bool StorageSchemaManifestHandler::Validate(
84 const Extension* extension,
85 std::string* error,
86 std::vector<InstallWarning>* warnings) const {
87 #if defined(ENABLE_CONFIGURATION_POLICY)
88 return GetSchema(extension, error).valid();
89 #else
90 return true;
91 #endif
92 }
93
Keys() const94 const std::vector<std::string> StorageSchemaManifestHandler::Keys() const {
95 return SingleKey(kStorageManagedSchema);
96 }
97
98 } // namespace extensions
99