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/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" 24 25#if defined(ENABLE_CONFIGURATION_POLICY) 26#include "components/policy/core/common/schema.h" 27#endif 28 29using extensions::manifest_keys::kStorageManagedSchema; 30 31namespace extensions { 32 33StorageSchemaManifestHandler::StorageSchemaManifestHandler() {} 34 35StorageSchemaManifestHandler::~StorageSchemaManifestHandler() {} 36 37#if defined(ENABLE_CONFIGURATION_POLICY) 38// static 39policy::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 65bool 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; 72 } 73 74 // If an extension declares the "storage.managed_schema" key then it gets 75 // the "storage" permission implicitly. 76 PermissionsParser::AddAPIPermission(extension, APIPermission::kStorage); 77 78 return true; 79} 80 81bool 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 90} 91 92const std::vector<std::string> StorageSchemaManifestHandler::Keys() const { 93 return SingleKey(kStorageManagedSchema); 94} 95 96} // namespace extensions 97