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/nacl_modules_handler.h"
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"
15 namespace extensions
{
17 namespace keys
= manifest_keys
;
18 namespace errors
= manifest_errors
;
22 struct NaClModuleData
: Extension::ManifestData
{
23 // Optional list of NaCl modules and associated properties.
24 NaClModuleInfo::List nacl_modules_
;
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
;
37 NaClModulesHandler::NaClModulesHandler() {
40 NaClModulesHandler::~NaClModulesHandler() {
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
);
50 scoped_ptr
<NaClModuleData
> nacl_module_data(new NaClModuleData
);
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
);
59 // Get nacl_modules[i].path.
61 if (!module_value
->GetString(keys::kNaClModulesPath
, &path_str
)) {
62 *error
= ErrorUtils::FormatErrorMessageUTF16(
63 errors::kInvalidNaClModulesPath
, base::IntToString(i
));
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
));
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
;
81 extension
->SetManifestData(keys::kNaClModules
, nacl_module_data
.release());
85 const std::vector
<std::string
> NaClModulesHandler::Keys() const {
86 return SingleKey(keys::kNaClModules
);
89 } // namespace extensions