[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 | |
kalman | 714075f | 2015-04-09 17:57:07 | [diff] [blame] | 7 | #include "base/logging.h" |
lazyboy | ce33396 | 2016-04-12 18:22:04 | [diff] [blame] | 8 | #include "base/strings/string_piece.h" |
9 | #include "extensions/renderer/static_v8_external_one_byte_string_resource.h" | ||||
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 10 | #include "ui/base/resource/resource_bundle.h" |
11 | |||||
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 12 | namespace extensions { |
13 | |||||
lazyboy | ce33396 | 2016-04-12 18:22:04 | [diff] [blame] | 14 | namespace { |
15 | |||||
16 | v8::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] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 26 | ResourceBundleSourceMap::ResourceBundleSourceMap( |
27 | const ui::ResourceBundle* resource_bundle) | ||||
28 | : resource_bundle_(resource_bundle) { | ||||
29 | } | ||||
30 | |||||
31 | ResourceBundleSourceMap::~ResourceBundleSourceMap() { | ||||
32 | } | ||||
33 | |||||
34 | void ResourceBundleSourceMap::RegisterSource(const std::string& name, | ||||
35 | int resource_id) { | ||||
36 | resource_id_map_[name] = resource_id; | ||||
37 | } | ||||
38 | |||||
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 39 | v8::Local<v8::Value> ResourceBundleSourceMap::GetSource( |
[email protected] | 6f59d3b | 2013-12-02 12:50:50 | [diff] [blame] | 40 | v8::Isolate* isolate, |
lazyboy | ce33396 | 2016-04-12 18:22:04 | [diff] [blame] | 41 | 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()) { | ||||
kalman | 714075f | 2015-04-09 17:57:07 | [diff] [blame] | 44 | NOTREACHED() << "No module is registered with name \"" << name << "\""; |
[email protected] | 6f59d3b | 2013-12-02 12:50:50 | [diff] [blame] | 45 | return v8::Undefined(isolate); |
kalman | 714075f | 2015-04-09 17:57:07 | [diff] [blame] | 46 | } |
47 | base::StringPiece resource = | ||||
lazyboy | ce33396 | 2016-04-12 18:22:04 | [diff] [blame] | 48 | resource_bundle_->GetRawDataResource(resource_id_iter->second); |
kalman | 714075f | 2015-04-09 17:57:07 | [diff] [blame] | 49 | 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] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 55 | } |
56 | |||||
lazyboy | ce33396 | 2016-04-12 18:22:04 | [diff] [blame] | 57 | bool ResourceBundleSourceMap::Contains(const std::string& name) const { |
[email protected] | 46babb2 | 2012-03-21 04:09:23 | [diff] [blame] | 58 | return !!resource_id_map_.count(name); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 59 | } |
60 | |||||
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 61 | } // namespace extensions |