OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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/browser/extensions/api/storage/storage_schema_manifest_handler. h" | |
6 | |
7 #include "base/file_util.h" | |
8 #include "base/files/file_path.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/string16.h" | |
11 #include "base/stringprintf.h" | |
12 #include "base/utf_string_conversions.h" | |
13 #include "chrome/browser/policy/policy_schema.h" | |
14 #include "chrome/common/extensions/extension.h" | |
15 #include "chrome/common/extensions/extension_manifest_constants.h" | |
16 #include "chrome/common/extensions/manifest.h" | |
17 #include "chrome/common/extensions/permissions/api_permission.h" | |
18 | |
19 namespace extensions { | |
20 | |
21 namespace { | |
22 | |
23 const char kMissingPermission[] = | |
24 "The storage permission is required to use storage.managed_schema"; | |
25 const char kInvalidValue[] = | |
26 "Invalid value for storage.managed_schema - must be a string"; | |
27 const char kInvalidPath[] = | |
28 "storage.managed_schema must be a relative path without .."; | |
29 | |
30 } // namespace | |
31 | |
32 StorageSchemaManifestHandler::StorageSchemaManifestHandler() {} | |
33 | |
34 StorageSchemaManifestHandler::~StorageSchemaManifestHandler() {} | |
35 | |
36 bool StorageSchemaManifestHandler::Parse(Extension* extension, | |
37 string16* error) { | |
38 std::string path; | |
bartfab (slow)
2013/05/22 11:00:09
Nit: #include <string>
Joao da Silva
2013/05/22 12:26:20
Done.
| |
39 if (!extension->manifest()->GetString( | |
40 extension_manifest_keys::kStorageManagedSchema, &path)) { | |
41 *error = ASCIIToUTF16(kInvalidValue); | |
42 return false; | |
43 } | |
44 return true; | |
45 } | |
46 | |
47 bool StorageSchemaManifestHandler::Validate( | |
48 const Extension* extension, | |
49 std::string* error, | |
50 std::vector<InstallWarning>* warnings) const { | |
bartfab (slow)
2013/05/22 11:00:09
Nit 1: #include <vector>
Nit 2: #include "chrome/c
Joao da Silva
2013/05/22 12:26:20
Done.
| |
51 if (!extension->HasAPIPermission(APIPermission::kStorage)) { | |
52 *error = kMissingPermission; | |
53 return false; | |
54 } | |
55 std::string path; | |
56 extension->manifest()->GetString( | |
57 extension_manifest_keys::kStorageManagedSchema, &path); | |
58 base::FilePath file = base::FilePath::FromUTF8Unsafe(path); | |
59 if (file.IsAbsolute() || file.ReferencesParent()) { | |
60 *error = kInvalidPath; | |
61 return false; | |
62 } | |
63 file = extension->path().AppendASCII(path); | |
64 if (!file_util::PathExists(file)) { | |
65 *error = | |
66 base::StringPrintf("File does not exist: %s", file.value().c_str()); | |
67 return false; | |
68 } | |
69 std::string content; | |
70 if (!file_util::ReadFileToString(file, &content)) { | |
71 *error = base::StringPrintf("Can't read %s", file.value().c_str()); | |
72 return false; | |
73 } | |
74 scoped_ptr<policy::PolicySchema> schema = | |
75 policy::PolicySchema::Parse(content, error); | |
76 return !!schema; | |
77 } | |
78 | |
79 const std::vector<std::string> StorageSchemaManifestHandler::Keys() const { | |
80 return SingleKey(extension_manifest_keys::kStorageManagedSchema); | |
81 } | |
82 | |
83 } // namespace extensions | |
OLD | NEW |