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 "chrome/common/extensions/extension.h"
17#include "chrome/common/extensions/extension_manifest_constants.h"
18#include "chrome/common/extensions/manifest.h"
19#include "chrome/common/extensions/permissions/api_permission.h"
20#include "chrome/common/policy/policy_schema.h"
21#include "extensions/common/install_warning.h"
22
23using extension_manifest_keys::kStorageManagedSchema;
24
25namespace extensions {
26
27StorageSchemaManifestHandler::StorageSchemaManifestHandler() {}
28
29StorageSchemaManifestHandler::~StorageSchemaManifestHandler() {}
30
31// static
32scoped_ptr<policy::PolicySchema> StorageSchemaManifestHandler::GetSchema(
33    const Extension* extension,
34    std::string* error) {
35  if (!extension->HasAPIPermission(APIPermission::kStorage)) {
36    *error = base::StringPrintf("The storage permission is required to use %s",
37                                kStorageManagedSchema);
38    return scoped_ptr<policy::PolicySchema>();
39  }
40  std::string path;
41  extension->manifest()->GetString(kStorageManagedSchema, &path);
42  base::FilePath file = base::FilePath::FromUTF8Unsafe(path);
43  if (file.IsAbsolute() || file.ReferencesParent()) {
44    *error = base::StringPrintf("%s must be a relative path without ..",
45                                kStorageManagedSchema);
46    return scoped_ptr<policy::PolicySchema>();
47  }
48  file = extension->path().AppendASCII(path);
49  if (!base::PathExists(file)) {
50    *error =
51        base::StringPrintf("File does not exist: %s", file.value().c_str());
52    return scoped_ptr<policy::PolicySchema>();
53  }
54  std::string content;
55  if (!file_util::ReadFileToString(file, &content)) {
56    *error = base::StringPrintf("Can't read %s", file.value().c_str());
57    return scoped_ptr<policy::PolicySchema>();
58  }
59  return policy::PolicySchema::Parse(content, error);
60}
61
62bool StorageSchemaManifestHandler::Parse(Extension* extension,
63                                         string16* error) {
64  std::string path;
65  if (!extension->manifest()->GetString(kStorageManagedSchema, &path)) {
66    *error = ASCIIToUTF16(
67        base::StringPrintf("%s must be a string", kStorageManagedSchema));
68    return false;
69  }
70  return true;
71}
72
73bool StorageSchemaManifestHandler::Validate(
74    const Extension* extension,
75    std::string* error,
76    std::vector<InstallWarning>* warnings) const {
77  return !!GetSchema(extension, error);
78}
79
80const std::vector<std::string> StorageSchemaManifestHandler::Keys() const {
81  return SingleKey(kStorageManagedSchema);
82}
83
84}  // namespace extensions
85