Home | History | Annotate | Download | only in renderer
      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
      3 // found in the LICENSE file.
      4 
      5 #include "extensions/renderer/resource_bundle_source_map.h"
      6 
      7 #include "ui/base/resource/resource_bundle.h"
      8 
      9 namespace extensions {
     10 
     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(
     25     v8::Isolate* isolate,
     26     const std::string& name) {
     27   if (!Contains(name))
     28     return v8::Undefined(isolate);
     29   int resource_id = resource_id_map_[name];
     30   return ConvertString(isolate,
     31                        resource_bundle_->GetRawDataResource(resource_id));
     32 }
     33 
     34 bool ResourceBundleSourceMap::Contains(const std::string& name) {
     35   return !!resource_id_map_.count(name);
     36 }
     37 
     38 v8::Handle<v8::String> ResourceBundleSourceMap::ConvertString(
     39     v8::Isolate* isolate,
     40     const base::StringPiece& string) {
     41   // v8 takes ownership of the StaticV8ExternalAsciiStringResource (see
     42   // v8::String::NewExternal()).
     43   return v8::String::NewExternal(
     44       isolate, new StaticV8ExternalAsciiStringResource(string));
     45 }
     46 
     47 }  // namespace extensions
     48