OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/renderer/resource_bundle_source_map.h" | 5 #include "extensions/renderer/resource_bundle_source_map.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "ui/base/resource/resource_bundle.h" | 8 #include "ui/base/resource/resource_bundle.h" |
9 | 9 |
10 namespace extensions { | 10 namespace extensions { |
11 | 11 |
12 ResourceBundleSourceMap::ResourceBundleSourceMap( | 12 ResourceBundleSourceMap::ResourceBundleSourceMap( |
13 const ui::ResourceBundle* resource_bundle) | 13 const ui::ResourceBundle* resource_bundle) |
14 : resource_bundle_(resource_bundle) { | 14 : resource_bundle_(resource_bundle) { |
15 } | 15 } |
16 | 16 |
17 ResourceBundleSourceMap::~ResourceBundleSourceMap() { | 17 ResourceBundleSourceMap::~ResourceBundleSourceMap() { |
18 } | 18 } |
19 | 19 |
20 void ResourceBundleSourceMap::RegisterSource(const std::string& name, | 20 void ResourceBundleSourceMap::RegisterSource(const std::string& name, |
21 int resource_id) { | 21 int resource_id) { |
22 resource_id_map_[name] = resource_id; | 22 resource_id_map_[name] = resource_id; |
23 } | 23 } |
24 | 24 |
25 v8::Handle<v8::Value> ResourceBundleSourceMap::GetSource( | 25 v8::Local<v8::Value> ResourceBundleSourceMap::GetSource( |
26 v8::Isolate* isolate, | 26 v8::Isolate* isolate, |
27 const std::string& name) { | 27 const std::string& name) { |
28 if (!Contains(name)) { | 28 if (!Contains(name)) { |
29 NOTREACHED() << "No module is registered with name \"" << name << "\""; | 29 NOTREACHED() << "No module is registered with name \"" << name << "\""; |
30 return v8::Undefined(isolate); | 30 return v8::Undefined(isolate); |
31 } | 31 } |
32 base::StringPiece resource = | 32 base::StringPiece resource = |
33 resource_bundle_->GetRawDataResource(resource_id_map_[name]); | 33 resource_bundle_->GetRawDataResource(resource_id_map_[name]); |
34 if (resource.empty()) { | 34 if (resource.empty()) { |
35 NOTREACHED() | 35 NOTREACHED() |
36 << "Module resource registered as \"" << name << "\" not found"; | 36 << "Module resource registered as \"" << name << "\" not found"; |
37 return v8::Undefined(isolate); | 37 return v8::Undefined(isolate); |
38 } | 38 } |
39 return ConvertString(isolate, resource); | 39 return ConvertString(isolate, resource); |
40 } | 40 } |
41 | 41 |
42 bool ResourceBundleSourceMap::Contains(const std::string& name) { | 42 bool ResourceBundleSourceMap::Contains(const std::string& name) { |
43 return !!resource_id_map_.count(name); | 43 return !!resource_id_map_.count(name); |
44 } | 44 } |
45 | 45 |
46 v8::Handle<v8::String> ResourceBundleSourceMap::ConvertString( | 46 v8::Local<v8::String> ResourceBundleSourceMap::ConvertString( |
47 v8::Isolate* isolate, | 47 v8::Isolate* isolate, |
48 const base::StringPiece& string) { | 48 const base::StringPiece& string) { |
49 // v8 takes ownership of the StaticV8ExternalOneByteStringResource (see | 49 // v8 takes ownership of the StaticV8ExternalOneByteStringResource (see |
50 // v8::String::NewExternal()). | 50 // v8::String::NewExternal()). |
51 return v8::String::NewExternal( | 51 return v8::String::NewExternal( |
52 isolate, new StaticV8ExternalOneByteStringResource(string)); | 52 isolate, new StaticV8ExternalOneByteStringResource(string)); |
53 } | 53 } |
54 | 54 |
55 } // namespace extensions | 55 } // namespace extensions |
OLD | NEW |