Home | History | Annotate | Download | only in permissions
      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 "extensions/common/permissions/manifest_permission_set.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/strings/string_number_conversions.h"
      9 #include "base/values.h"
     10 #include "extensions/common/error_utils.h"
     11 #include "extensions/common/manifest_constants.h"
     12 #include "extensions/common/manifest_handler.h"
     13 #include "extensions/common/permissions/manifest_permission.h"
     14 
     15 namespace {
     16 
     17 using extensions::ErrorUtils;
     18 using extensions::ManifestPermission;
     19 using extensions::ManifestPermissionSet;
     20 using extensions::ManifestHandler;
     21 namespace errors = extensions::manifest_errors;
     22 
     23 bool CreateManifestPermission(
     24     const std::string& permission_name,
     25     const base::Value* permission_value,
     26     ManifestPermissionSet* manifest_permissions,
     27     base::string16* error,
     28     std::vector<std::string>* unhandled_permissions) {
     29 
     30   scoped_ptr<ManifestPermission> permission(
     31       ManifestHandler::CreatePermission(permission_name));
     32 
     33   if (!permission) {
     34     if (unhandled_permissions)
     35       unhandled_permissions->push_back(permission_name);
     36     else
     37       LOG(WARNING) << "Unknown permission[" << permission_name << "].";
     38     return true;
     39   }
     40 
     41   if (!permission->FromValue(permission_value)) {
     42     if (error) {
     43       *error = ErrorUtils::FormatErrorMessageUTF16(
     44           errors::kInvalidPermission, permission_name);
     45       return false;
     46     }
     47     LOG(WARNING) << "Parse permission failed.";
     48     return true;
     49   } else {
     50     manifest_permissions->insert(permission.release());
     51     return true;
     52   }
     53 }
     54 
     55 }  // namespace
     56 
     57 namespace extensions {
     58 
     59 // static
     60 bool ManifestPermissionSet::ParseFromJSON(
     61     const base::ListValue* permissions,
     62     ManifestPermissionSet* manifest_permissions,
     63     base::string16* error,
     64     std::vector<std::string>* unhandled_permissions) {
     65   for (size_t i = 0; i < permissions->GetSize(); ++i) {
     66     std::string permission_name;
     67     const base::Value* permission_value = NULL;
     68     if (!permissions->GetString(i, &permission_name)) {
     69       const base::DictionaryValue* dict = NULL;
     70       // permission should be a string or a single key dict.
     71       if (!permissions->GetDictionary(i, &dict) || dict->size() != 1) {
     72         if (error) {
     73           *error = ErrorUtils::FormatErrorMessageUTF16(
     74               errors::kInvalidPermission, base::IntToString(i));
     75           return false;
     76         }
     77         LOG(WARNING) << "Permission is not a string or single key dict.";
     78         continue;
     79       }
     80       base::DictionaryValue::Iterator it(*dict);
     81       permission_name = it.key();
     82       permission_value = &it.value();
     83     }
     84 
     85     if (!CreateManifestPermission(permission_name, permission_value,
     86                                   manifest_permissions, error,
     87                                   unhandled_permissions))
     88       return false;
     89   }
     90   return true;
     91 }
     92 
     93 }  // namespace extensions
     94