rdevlin.cronin | 892cc67 | 2016-12-19 20:00:18 | [diff] [blame] | 1 | // Copyright 2016 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/string_source_map.h" |
| 6 | |
| 7 | #include "gin/converter.h" |
Yuzhu Shen | bcd734d | 2017-11-15 20:40:58 | [diff] [blame] | 8 | #include "third_party/zlib/google/compression_utils.h" |
rdevlin.cronin | 892cc67 | 2016-12-19 20:00:18 | [diff] [blame] | 9 | |
| 10 | namespace extensions { |
| 11 | |
| 12 | StringSourceMap::StringSourceMap() {} |
| 13 | StringSourceMap::~StringSourceMap() {} |
| 14 | |
| 15 | v8::Local<v8::String> StringSourceMap::GetSource( |
| 16 | v8::Isolate* isolate, |
| 17 | const std::string& name) const { |
| 18 | const auto& iter = sources_.find(name); |
| 19 | if (iter == sources_.end()) |
| 20 | return v8::Local<v8::String>(); |
| 21 | return gin::StringToV8(isolate, iter->second); |
| 22 | } |
| 23 | |
| 24 | bool StringSourceMap::Contains(const std::string& name) const { |
| 25 | return sources_.find(name) != sources_.end(); |
| 26 | } |
| 27 | |
| 28 | void StringSourceMap::RegisterModule(const std::string& name, |
Yuzhu Shen | bcd734d | 2017-11-15 20:40:58 | [diff] [blame] | 29 | const std::string& source, |
| 30 | bool gzipped) { |
rdevlin.cronin | 892cc67 | 2016-12-19 20:00:18 | [diff] [blame] | 31 | CHECK_EQ(0u, sources_.count(name)) << "A module for '" << name |
| 32 | << "' already exists."; |
Yuzhu Shen | bcd734d | 2017-11-15 20:40:58 | [diff] [blame] | 33 | if (!gzipped) { |
| 34 | sources_[name] = source; |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | std::string uncompressed; |
| 39 | CHECK(compression::GzipUncompress(source, &uncompressed)); |
| 40 | sources_[name] = uncompressed; |
rdevlin.cronin | 892cc67 | 2016-12-19 20:00:18 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | } // namespace extensions |