Chromium Code Reviews| 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 "base/strings/string_piece.h" | |
| 9 #include "extensions/renderer/static_v8_external_one_byte_string_resource.h" | |
| 8 #include "ui/base/resource/resource_bundle.h" | 10 #include "ui/base/resource/resource_bundle.h" |
| 9 | 11 |
| 10 namespace extensions { | 12 namespace extensions { |
| 11 | 13 |
| 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 | |
| 12 ResourceBundleSourceMap::ResourceBundleSourceMap( | 26 ResourceBundleSourceMap::ResourceBundleSourceMap( |
| 13 const ui::ResourceBundle* resource_bundle) | 27 const ui::ResourceBundle* resource_bundle) |
| 14 : resource_bundle_(resource_bundle) { | 28 : resource_bundle_(resource_bundle) { |
| 15 } | 29 } |
| 16 | 30 |
| 17 ResourceBundleSourceMap::~ResourceBundleSourceMap() { | 31 ResourceBundleSourceMap::~ResourceBundleSourceMap() { |
| 18 } | 32 } |
| 19 | 33 |
| 20 void ResourceBundleSourceMap::RegisterSource(const std::string& name, | 34 void ResourceBundleSourceMap::RegisterSource(const std::string& name, |
| 21 int resource_id) { | 35 int resource_id) { |
| 22 resource_id_map_[name] = resource_id; | 36 resource_id_map_[name] = resource_id; |
| 23 } | 37 } |
| 24 | 38 |
| 25 v8::Local<v8::Value> ResourceBundleSourceMap::GetSource( | 39 v8::Local<v8::Value> ResourceBundleSourceMap::GetSource( |
| 26 v8::Isolate* isolate, | 40 v8::Isolate* isolate, |
| 27 const std::string& name) { | 41 const std::string& name) const { |
| 28 if (!Contains(name)) { | 42 if (!Contains(name)) { |
|
Devlin
2016/04/12 17:01:25
nit: another double-lookup.
lazyboy
2016/04/12 17:29:28
Done.
| |
| 29 NOTREACHED() << "No module is registered with name \"" << name << "\""; | 43 NOTREACHED() << "No module is registered with name \"" << name << "\""; |
| 30 return v8::Undefined(isolate); | 44 return v8::Undefined(isolate); |
| 31 } | 45 } |
| 46 int resource_id = resource_id_map_.find(name)->second; | |
| 32 base::StringPiece resource = | 47 base::StringPiece resource = |
| 33 resource_bundle_->GetRawDataResource(resource_id_map_[name]); | 48 resource_bundle_->GetRawDataResource(resource_id); |
| 34 if (resource.empty()) { | 49 if (resource.empty()) { |
| 35 NOTREACHED() | 50 NOTREACHED() |
| 36 << "Module resource registered as \"" << name << "\" not found"; | 51 << "Module resource registered as \"" << name << "\" not found"; |
| 37 return v8::Undefined(isolate); | 52 return v8::Undefined(isolate); |
| 38 } | 53 } |
| 39 return ConvertString(isolate, resource); | 54 return ConvertString(isolate, resource); |
| 40 } | 55 } |
| 41 | 56 |
| 42 bool ResourceBundleSourceMap::Contains(const std::string& name) { | 57 bool ResourceBundleSourceMap::Contains(const std::string& name) const { |
| 43 return !!resource_id_map_.count(name); | 58 return !!resource_id_map_.count(name); |
| 44 } | 59 } |
| 45 | 60 |
| 46 v8::Local<v8::String> ResourceBundleSourceMap::ConvertString( | |
| 47 v8::Isolate* isolate, | |
| 48 const base::StringPiece& string) { | |
| 49 // v8 takes ownership of the StaticV8ExternalOneByteStringResource (see | |
| 50 // v8::String::NewExternal()). | |
| 51 return v8::String::NewExternal( | |
| 52 isolate, new StaticV8ExternalOneByteStringResource(string)); | |
| 53 } | |
| 54 | |
| 55 } // namespace extensions | 61 } // namespace extensions |
| OLD | NEW |