blob: 954a6d73acb8c765d8646aa3c1c380e25be811e2 [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
7#include "ui/base/resource/resource_bundle.h"
8
[email protected]e6893672014-05-01 17:29:139namespace extensions {
10
[email protected]ecde1912012-03-16 06:25:3111ResourceBundleSourceMap::ResourceBundleSourceMap(
12 const ui::ResourceBundle* resource_bundle)
13 : resource_bundle_(resource_bundle) {
14}
15
16ResourceBundleSourceMap::~ResourceBundleSourceMap() {
17}
18
19void ResourceBundleSourceMap::RegisterSource(const std::string& name,
20 int resource_id) {
21 resource_id_map_[name] = resource_id;
22}
23
24v8::Handle<v8::Value> ResourceBundleSourceMap::GetSource(
[email protected]6f59d3b2013-12-02 12:50:5025 v8::Isolate* isolate,
[email protected]ecde1912012-03-16 06:25:3126 const std::string& name) {
27 if (!Contains(name))
[email protected]6f59d3b2013-12-02 12:50:5028 return v8::Undefined(isolate);
[email protected]ecde1912012-03-16 06:25:3129 int resource_id = resource_id_map_[name];
[email protected]6f59d3b2013-12-02 12:50:5030 return ConvertString(isolate,
31 resource_bundle_->GetRawDataResource(resource_id));
[email protected]ecde1912012-03-16 06:25:3132}
33
34bool ResourceBundleSourceMap::Contains(const std::string& name) {
[email protected]46babb22012-03-21 04:09:2335 return !!resource_id_map_.count(name);
[email protected]ecde1912012-03-16 06:25:3136}
37
38v8::Handle<v8::String> ResourceBundleSourceMap::ConvertString(
[email protected]6f59d3b2013-12-02 12:50:5039 v8::Isolate* isolate,
[email protected]ecde1912012-03-16 06:25:3140 const base::StringPiece& string) {
41 // v8 takes ownership of the StaticV8ExternalAsciiStringResource (see
42 // v8::String::NewExternal()).
43 return v8::String::NewExternal(
[email protected]6f59d3b2013-12-02 12:50:5044 isolate, new StaticV8ExternalAsciiStringResource(string));
[email protected]ecde1912012-03-16 06:25:3145}
[email protected]e6893672014-05-01 17:29:1346
47} // namespace extensions