blob: f0878599e19335ba1bd4f20c099334f00f2fee1d [file] [log] [blame]
[email protected]e6893672014-05-01 17:29:131// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]ecde1912012-03-16 06:25:312// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e6893672014-05-01 17:29:135#include "extensions/renderer/resource_bundle_source_map.h"
[email protected]ecde1912012-03-16 06:25:316
kalman714075f2015-04-09 17:57:077#include "base/logging.h"
Lei Zhang28e01caf2018-10-05 00:26:508#include "base/stl_util.h"
lazyboyce333962016-04-12 18:22:049#include "base/strings/string_piece.h"
10#include "extensions/renderer/static_v8_external_one_byte_string_resource.h"
Yuzhu Shenbcd734d2017-11-15 20:40:5811#include "third_party/zlib/google/compression_utils.h"
[email protected]ecde1912012-03-16 06:25:3112#include "ui/base/resource/resource_bundle.h"
13
[email protected]e6893672014-05-01 17:29:1314namespace extensions {
15
lazyboyce333962016-04-12 18:22:0416namespace {
17
18v8::Local<v8::String> ConvertString(v8::Isolate* isolate,
19 const base::StringPiece& string) {
20 // v8 takes ownership of the StaticV8ExternalOneByteStringResource (see
Dan Elphick378e7ab02018-12-19 17:13:4121 // v8::String::NewExternalOneByte()).
22 return v8::String::NewExternalOneByte(
23 isolate, new StaticV8ExternalOneByteStringResource(string))
24 .FromMaybe(v8::Local<v8::String>());
lazyboyce333962016-04-12 18:22:0425}
26
27} // namespace
28
Yuzhu Shenbcd734d2017-11-15 20:40:5829ResourceBundleSourceMap::ResourceInfo::ResourceInfo() = default;
dpapad1aba5c342019-05-13 23:44:3630ResourceBundleSourceMap::ResourceInfo::ResourceInfo(int in_id) : id(in_id) {}
Yuzhu Shenbcd734d2017-11-15 20:40:5831ResourceBundleSourceMap::ResourceInfo::ResourceInfo(ResourceInfo&& other) =
32 default;
33
34ResourceBundleSourceMap::ResourceInfo::~ResourceInfo() = default;
35
36ResourceBundleSourceMap::ResourceInfo& ResourceBundleSourceMap::ResourceInfo::
37operator=(ResourceInfo&& other) = default;
38
[email protected]ecde1912012-03-16 06:25:3139ResourceBundleSourceMap::ResourceBundleSourceMap(
40 const ui::ResourceBundle* resource_bundle)
41 : resource_bundle_(resource_bundle) {
42}
43
44ResourceBundleSourceMap::~ResourceBundleSourceMap() {
45}
46
dcheng28428172017-02-06 05:39:2947void ResourceBundleSourceMap::RegisterSource(const char* const name,
dpapad1aba5c342019-05-13 23:44:3648 int resource_id) {
49 resource_map_.emplace(name, resource_id);
[email protected]ecde1912012-03-16 06:25:3150}
51
rdevlin.cronin1ed2e892016-12-06 21:35:4352v8::Local<v8::String> ResourceBundleSourceMap::GetSource(
[email protected]6f59d3b2013-12-02 12:50:5053 v8::Isolate* isolate,
lazyboyce333962016-04-12 18:22:0454 const std::string& name) const {
Yuzhu Shenbcd734d2017-11-15 20:40:5855 auto resource_iter = resource_map_.find(name);
56 if (resource_iter == resource_map_.end()) {
kalman714075f2015-04-09 17:57:0757 NOTREACHED() << "No module is registered with name \"" << name << "\"";
rdevlin.cronin1ed2e892016-12-06 21:35:4358 return v8::Local<v8::String>();
kalman714075f2015-04-09 17:57:0759 }
Yuzhu Shenbcd734d2017-11-15 20:40:5860
61 const ResourceInfo& info = resource_iter->second;
62 if (info.cached)
63 return ConvertString(isolate, *info.cached);
64
65 base::StringPiece resource = resource_bundle_->GetRawDataResource(info.id);
kalman714075f2015-04-09 17:57:0766 if (resource.empty()) {
67 NOTREACHED()
68 << "Module resource registered as \"" << name << "\" not found";
rdevlin.cronin1ed2e892016-12-06 21:35:4369 return v8::Local<v8::String>();
kalman714075f2015-04-09 17:57:0770 }
Yuzhu Shenbcd734d2017-11-15 20:40:5871
dpapad1aba5c342019-05-13 23:44:3672 bool is_gzipped = resource_bundle_->IsGzipped(info.id);
73 if (is_gzipped) {
Yuzhu Shenbcd734d2017-11-15 20:40:5874 info.cached = std::make_unique<std::string>();
75 uint32_t size = compression::GetUncompressedSize(resource);
76 info.cached->resize(size);
77 base::StringPiece uncompressed(*info.cached);
78 if (!compression::GzipUncompress(resource, uncompressed)) {
79 // Let |info.cached| point to an empty string, so that the next time when
80 // the resource is requested, the method returns an empty string directly,
81 // instead of trying to uncompress again.
82 info.cached->clear();
83 return v8::Local<v8::String>();
84 }
85 resource = uncompressed;
86 }
87
kalman714075f2015-04-09 17:57:0788 return ConvertString(isolate, resource);
[email protected]ecde1912012-03-16 06:25:3189}
90
lazyboyce333962016-04-12 18:22:0491bool ResourceBundleSourceMap::Contains(const std::string& name) const {
Jan Wilken Dörrie0fd53a22019-06-07 09:55:4692 return base::Contains(resource_map_, name);
[email protected]ecde1912012-03-16 06:25:3193}
94
[email protected]e6893672014-05-01 17:29:1395} // namespace extensions