blob: a260b543f341693e63358994f4cb13e95b88582d [file] [log] [blame]
[email protected]e6893672014-05-01 17:29:131// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]ecde1912012-03-16 06:25:312// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e6893672014-05-01 17:29:135#include "extensions/renderer/resource_bundle_source_map.h"
[email protected]ecde1912012-03-16 06:25:316
kalman714075f2015-04-09 17:57:077#include "base/logging.h"
lazyboyce333962016-04-12 18:22:048#include "base/strings/string_piece.h"
9#include "extensions/renderer/static_v8_external_one_byte_string_resource.h"
[email protected]ecde1912012-03-16 06:25:3110#include "ui/base/resource/resource_bundle.h"
11
[email protected]e6893672014-05-01 17:29:1312namespace extensions {
13
lazyboyce333962016-04-12 18:22:0414namespace {
15
16v8::Local<v8::String> ConvertString(v8::Isolate* isolate,
17 const base::StringPiece& string) {
18 // v8 takes ownership of the StaticV8ExternalOneByteStringResource (see
19 // v8::String::NewExternal()).
20 return v8::String::NewExternal(
21 isolate, new StaticV8ExternalOneByteStringResource(string));
22}
23
24} // namespace
25
[email protected]ecde1912012-03-16 06:25:3126ResourceBundleSourceMap::ResourceBundleSourceMap(
27 const ui::ResourceBundle* resource_bundle)
28 : resource_bundle_(resource_bundle) {
29}
30
31ResourceBundleSourceMap::~ResourceBundleSourceMap() {
32}
33
34void ResourceBundleSourceMap::RegisterSource(const std::string& name,
35 int resource_id) {
36 resource_id_map_[name] = resource_id;
37}
38
tfarinaf85316f2015-04-29 17:03:4039v8::Local<v8::Value> ResourceBundleSourceMap::GetSource(
[email protected]6f59d3b2013-12-02 12:50:5040 v8::Isolate* isolate,
lazyboyce333962016-04-12 18:22:0441 const std::string& name) const {
42 const auto& resource_id_iter = resource_id_map_.find(name);
43 if (resource_id_iter == resource_id_map_.end()) {
kalman714075f2015-04-09 17:57:0744 NOTREACHED() << "No module is registered with name \"" << name << "\"";
[email protected]6f59d3b2013-12-02 12:50:5045 return v8::Undefined(isolate);
kalman714075f2015-04-09 17:57:0746 }
47 base::StringPiece resource =
lazyboyce333962016-04-12 18:22:0448 resource_bundle_->GetRawDataResource(resource_id_iter->second);
kalman714075f2015-04-09 17:57:0749 if (resource.empty()) {
50 NOTREACHED()
51 << "Module resource registered as \"" << name << "\" not found";
52 return v8::Undefined(isolate);
53 }
54 return ConvertString(isolate, resource);
[email protected]ecde1912012-03-16 06:25:3155}
56
lazyboyce333962016-04-12 18:22:0457bool ResourceBundleSourceMap::Contains(const std::string& name) const {
[email protected]46babb22012-03-21 04:09:2358 return !!resource_id_map_.count(name);
[email protected]ecde1912012-03-16 06:25:3159}
60
[email protected]e6893672014-05-01 17:29:1361} // namespace extensions