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