blob: 6f4931c68c129359f4b0c5961681679e279015e5 [file] [log] [blame]
rdevlin.cronin892cc672016-12-19 20:00:181// 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 Shenbcd734d2017-11-15 20:40:588#include "third_party/zlib/google/compression_utils.h"
rdevlin.cronin892cc672016-12-19 20:00:189
10namespace extensions {
11
12StringSourceMap::StringSourceMap() {}
13StringSourceMap::~StringSourceMap() {}
14
15v8::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
24bool StringSourceMap::Contains(const std::string& name) const {
25 return sources_.find(name) != sources_.end();
26}
27
28void StringSourceMap::RegisterModule(const std::string& name,
Yuzhu Shenbcd734d2017-11-15 20:40:5829 const std::string& source,
30 bool gzipped) {
rdevlin.cronin892cc672016-12-19 20:00:1831 CHECK_EQ(0u, sources_.count(name)) << "A module for '" << name
32 << "' already exists.";
Yuzhu Shenbcd734d2017-11-15 20:40:5833 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.cronin892cc672016-12-19 20:00:1841}
42
43} // namespace extensions