• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "extensions/common/manifest_handlers/nacl_modules_handler.h"
6 
7 #include "base/lazy_instance.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h"
12 #include "extensions/common/error_utils.h"
13 #include "extensions/common/manifest_constants.h"
14 
15 namespace extensions {
16 
17 namespace keys = manifest_keys;
18 namespace errors = manifest_errors;
19 
20 namespace {
21 
22 struct NaClModuleData : Extension::ManifestData {
23   // Optional list of NaCl modules and associated properties.
24   NaClModuleInfo::List nacl_modules_;
25 };
26 
27 }  // namespace
28 
29 // static
GetNaClModules(const Extension * extension)30 const NaClModuleInfo::List* NaClModuleInfo::GetNaClModules(
31     const Extension* extension) {
32   NaClModuleData* data = static_cast<NaClModuleData*>(
33       extension->GetManifestData(keys::kNaClModules));
34   return data ? &data->nacl_modules_ : NULL;
35 }
36 
NaClModulesHandler()37 NaClModulesHandler::NaClModulesHandler() {
38 }
39 
~NaClModulesHandler()40 NaClModulesHandler::~NaClModulesHandler() {
41 }
42 
Parse(Extension * extension,base::string16 * error)43 bool NaClModulesHandler::Parse(Extension* extension, base::string16* error) {
44   const base::ListValue* list_value = NULL;
45   if (!extension->manifest()->GetList(keys::kNaClModules, &list_value)) {
46     *error = base::ASCIIToUTF16(errors::kInvalidNaClModules);
47     return false;
48   }
49 
50   scoped_ptr<NaClModuleData> nacl_module_data(new NaClModuleData);
51 
52   for (size_t i = 0; i < list_value->GetSize(); ++i) {
53     const base::DictionaryValue* module_value = NULL;
54     if (!list_value->GetDictionary(i, &module_value)) {
55       *error = base::ASCIIToUTF16(errors::kInvalidNaClModules);
56       return false;
57     }
58 
59     // Get nacl_modules[i].path.
60     std::string path_str;
61     if (!module_value->GetString(keys::kNaClModulesPath, &path_str)) {
62       *error = ErrorUtils::FormatErrorMessageUTF16(
63           errors::kInvalidNaClModulesPath, base::IntToString(i));
64       return false;
65     }
66 
67     // Get nacl_modules[i].mime_type.
68     std::string mime_type;
69     if (!module_value->GetString(keys::kNaClModulesMIMEType, &mime_type)) {
70       *error = ErrorUtils::FormatErrorMessageUTF16(
71           errors::kInvalidNaClModulesMIMEType, base::IntToString(i));
72       return false;
73     }
74 
75     nacl_module_data->nacl_modules_.push_back(NaClModuleInfo());
76     nacl_module_data->nacl_modules_.back().url =
77         extension->GetResourceURL(path_str);
78     nacl_module_data->nacl_modules_.back().mime_type = mime_type;
79   }
80 
81   extension->SetManifestData(keys::kNaClModules, nacl_module_data.release());
82   return true;
83 }
84 
Keys() const85 const std::vector<std::string> NaClModulesHandler::Keys() const {
86   return SingleKey(keys::kNaClModules);
87 }
88 
89 }  // namespace extensions
90