Home | History | Annotate | Download | only in extensions
      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/common/extensions/web_accessible_resources_handler.h"
      6 
      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 "chrome/common/extensions/extension_manifest_constants.h"
     12 #include "chrome/common/extensions/manifest.h"
     13 #include "extensions/common/error_utils.h"
     14 
     15 namespace extensions {
     16 
     17 namespace keys = extension_manifest_keys;
     18 namespace errors = extension_manifest_errors;
     19 
     20 namespace {
     21 
     22 const WebAccessibleResourcesInfo* GetResourcesInfo(const Extension* extension) {
     23   return static_cast<WebAccessibleResourcesInfo*>(
     24       extension->GetManifestData(keys::kWebAccessibleResources));
     25 }
     26 
     27 }  // namespace
     28 
     29 WebAccessibleResourcesInfo::WebAccessibleResourcesInfo() {
     30 }
     31 
     32 WebAccessibleResourcesInfo::~WebAccessibleResourcesInfo() {
     33 }
     34 
     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;
     44 
     45   const WebAccessibleResourcesInfo* info = GetResourcesInfo(extension);
     46   return info &&
     47          extension->ResourceMatches(
     48              info->web_accessible_resources_, relative_path);
     49 }
     50 
     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;
     56 }
     57 
     58 WebAccessibleResourcesHandler::WebAccessibleResourcesHandler() {
     59 }
     60 
     61 WebAccessibleResourcesHandler::~WebAccessibleResourcesHandler() {
     62 }
     63 
     64 bool WebAccessibleResourcesHandler::Parse(Extension* extension,
     65                                           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 = ASCIIToUTF16(errors::kInvalidWebAccessibleResourcesList);
     71     return false;
     72   }
     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;
     79     }
     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;
     85     }
     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);
     90   }
     91   extension->SetManifestData(keys::kWebAccessibleResources, info.release());
     92   return true;
     93 }
     94 
     95 const std::vector<std::string> WebAccessibleResourcesHandler::Keys() const {
     96   return SingleKey(keys::kWebAccessibleResources);
     97 }
     98 
     99 }  // namespace extensions
    100