[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 5 | #include "extensions/renderer/resource_bundle_source_map.h" |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 6 | |
7 | #include "ui/base/resource/resource_bundle.h" | ||||
8 | |||||
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 9 | namespace extensions { |
10 | |||||
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 11 | ResourceBundleSourceMap::ResourceBundleSourceMap( |
12 | const ui::ResourceBundle* resource_bundle) | ||||
13 | : resource_bundle_(resource_bundle) { | ||||
14 | } | ||||
15 | |||||
16 | ResourceBundleSourceMap::~ResourceBundleSourceMap() { | ||||
17 | } | ||||
18 | |||||
19 | void ResourceBundleSourceMap::RegisterSource(const std::string& name, | ||||
20 | int resource_id) { | ||||
21 | resource_id_map_[name] = resource_id; | ||||
22 | } | ||||
23 | |||||
24 | v8::Handle<v8::Value> ResourceBundleSourceMap::GetSource( | ||||
[email protected] | 6f59d3b | 2013-12-02 12:50:50 | [diff] [blame] | 25 | v8::Isolate* isolate, |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 26 | const std::string& name) { |
27 | if (!Contains(name)) | ||||
[email protected] | 6f59d3b | 2013-12-02 12:50:50 | [diff] [blame] | 28 | return v8::Undefined(isolate); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 29 | int resource_id = resource_id_map_[name]; |
[email protected] | 6f59d3b | 2013-12-02 12:50:50 | [diff] [blame] | 30 | return ConvertString(isolate, |
31 | resource_bundle_->GetRawDataResource(resource_id)); | ||||
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 32 | } |
33 | |||||
34 | bool ResourceBundleSourceMap::Contains(const std::string& name) { | ||||
[email protected] | 46babb2 | 2012-03-21 04:09:23 | [diff] [blame] | 35 | return !!resource_id_map_.count(name); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 36 | } |
37 | |||||
38 | v8::Handle<v8::String> ResourceBundleSourceMap::ConvertString( | ||||
[email protected] | 6f59d3b | 2013-12-02 12:50:50 | [diff] [blame] | 39 | v8::Isolate* isolate, |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 40 | const base::StringPiece& string) { |
41 | // v8 takes ownership of the StaticV8ExternalAsciiStringResource (see | ||||
42 | // v8::String::NewExternal()). | ||||
43 | return v8::String::NewExternal( | ||||
[email protected] | 6f59d3b | 2013-12-02 12:50:50 | [diff] [blame] | 44 | isolate, new StaticV8ExternalAsciiStringResource(string)); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 45 | } |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 46 | |
47 | } // namespace extensions |