Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / extensions / common / manifest_handlers / web_accessible_resources_info.cc
blob8da6bf5c7ae7319b16976a5e910fff393f9b350b
1 // Copyright 2014 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 "extensions/common/manifest_handlers/web_accessible_resources_info.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/values.h"
11 #include "extensions/common/error_utils.h"
12 #include "extensions/common/manifest.h"
13 #include "extensions/common/manifest_constants.h"
15 namespace extensions {
17 namespace keys = manifest_keys;
18 namespace errors = manifest_errors;
20 namespace {
22 const WebAccessibleResourcesInfo* GetResourcesInfo(const Extension* extension) {
23 return static_cast<WebAccessibleResourcesInfo*>(
24 extension->GetManifestData(keys::kWebAccessibleResources));
27 } // namespace
29 WebAccessibleResourcesInfo::WebAccessibleResourcesInfo() {
32 WebAccessibleResourcesInfo::~WebAccessibleResourcesInfo() {
35 // static
36 bool WebAccessibleResourcesInfo::IsResourceWebAccessible(
37 const Extension* extension,
38 const std::string& relative_path) {
39 // For old manifest versions which do not specify web_accessible_resources
40 // we always allow resource loads.
41 if (extension->manifest_version() < 2 &&
42 !WebAccessibleResourcesInfo::HasWebAccessibleResources(extension))
43 return true;
45 const WebAccessibleResourcesInfo* info = GetResourcesInfo(extension);
46 return info &&
47 extension->ResourceMatches(
48 info->web_accessible_resources_, relative_path);
51 // static
52 bool WebAccessibleResourcesInfo::HasWebAccessibleResources(
53 const Extension* extension) {
54 const WebAccessibleResourcesInfo* info = GetResourcesInfo(extension);
55 return info && info->web_accessible_resources_.size() > 0;
58 WebAccessibleResourcesHandler::WebAccessibleResourcesHandler() {
61 WebAccessibleResourcesHandler::~WebAccessibleResourcesHandler() {
64 bool WebAccessibleResourcesHandler::Parse(Extension* extension,
65 base::string16* error) {
66 scoped_ptr<WebAccessibleResourcesInfo> info(new WebAccessibleResourcesInfo);
67 const base::ListValue* list_value = NULL;
68 if (!extension->manifest()->GetList(keys::kWebAccessibleResources,
69 &list_value)) {
70 *error = base::ASCIIToUTF16(errors::kInvalidWebAccessibleResourcesList);
71 return false;
73 for (size_t i = 0; i < list_value->GetSize(); ++i) {
74 std::string relative_path;
75 if (!list_value->GetString(i, &relative_path)) {
76 *error = ErrorUtils::FormatErrorMessageUTF16(
77 errors::kInvalidWebAccessibleResource, base::IntToString(i));
78 return false;
80 URLPattern pattern(URLPattern::SCHEME_EXTENSION);
81 if (pattern.Parse(extension->url().spec()) != URLPattern::PARSE_SUCCESS) {
82 *error = ErrorUtils::FormatErrorMessageUTF16(
83 errors::kInvalidURLPatternError, extension->url().spec());
84 return false;
86 while (relative_path[0] == '/')
87 relative_path = relative_path.substr(1, relative_path.length() - 1);
88 pattern.SetPath(pattern.path() + relative_path);
89 info->web_accessible_resources_.AddPattern(pattern);
91 extension->SetManifestData(keys::kWebAccessibleResources, info.release());
92 return true;
95 const std::vector<std::string> WebAccessibleResourcesHandler::Keys() const {
96 return SingleKey(keys::kWebAccessibleResources);
99 } // namespace extensions