File: resource_bundle_source_map.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (99 lines) | stat: -rw-r--r-- 3,301 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/renderer/resource_bundle_source_map.h"

#include <ostream>
#include <string_view>

#include "base/containers/contains.h"
#include "base/notreached.h"
#include "extensions/renderer/static_v8_external_one_byte_string_resource.h"
#include "third_party/zlib/google/compression_utils.h"
#include "ui/base/resource/resource_bundle.h"
#include "v8/include/v8-primitive.h"

namespace extensions {

namespace {

v8::Local<v8::String> ConvertString(v8::Isolate* isolate,
                                    std::string_view string) {
  // v8 takes ownership of the StaticV8ExternalOneByteStringResource (see
  // v8::String::NewExternalOneByte()).
  return v8::String::NewExternalOneByte(
             isolate, new StaticV8ExternalOneByteStringResource(string))
      .FromMaybe(v8::Local<v8::String>());
}

}  // namespace

ResourceBundleSourceMap::ResourceInfo::ResourceInfo() = default;
ResourceBundleSourceMap::ResourceInfo::ResourceInfo(int in_id) : id(in_id) {}
ResourceBundleSourceMap::ResourceInfo::ResourceInfo(ResourceInfo&& other) =
    default;

ResourceBundleSourceMap::ResourceInfo::~ResourceInfo() = default;

ResourceBundleSourceMap::ResourceInfo& ResourceBundleSourceMap::ResourceInfo::
operator=(ResourceInfo&& other) = default;

ResourceBundleSourceMap::ResourceBundleSourceMap(
    const ui::ResourceBundle* resource_bundle)
    : resource_bundle_(resource_bundle) {
}

ResourceBundleSourceMap::~ResourceBundleSourceMap() {
}

void ResourceBundleSourceMap::RegisterSource(const char* const name,
                                             int resource_id) {
  base::AutoLock lock(lock_);
  resource_map_.emplace(name, resource_id);
}

v8::Local<v8::String> ResourceBundleSourceMap::GetSource(
    v8::Isolate* isolate,
    const std::string& name) const {
  base::AutoLock lock(lock_);
  auto resource_iter = resource_map_.find(name);
  if (resource_iter == resource_map_.end()) {
    DUMP_WILL_BE_NOTREACHED()
        << "No module is registered with name \"" << name << "\"";
    return v8::Local<v8::String>();
  }

  const ResourceInfo& info = resource_iter->second;
  if (info.cached)
    return ConvertString(isolate, *info.cached);

  std::string_view resource = resource_bundle_->GetRawDataResource(info.id);
  if (resource.empty()) {
    DUMP_WILL_BE_NOTREACHED()
        << "Module resource registered as \"" << name << "\" not found";
    return v8::Local<v8::String>();
  }

  bool is_gzipped = resource_bundle_->IsGzipped(info.id);
  if (is_gzipped) {
    info.cached = std::make_unique<std::string>();
    if (!compression::GzipUncompress(resource, info.cached.get())) {
      // Let |info.cached| point to an empty string, so that the next time when
      // the resource is requested, the method returns an empty string directly,
      // instead of trying to uncompress again.
      info.cached->clear();
      return v8::Local<v8::String>();
    }
    resource = *info.cached;
  }

  return ConvertString(isolate, resource);
}

bool ResourceBundleSourceMap::Contains(const std::string& name) const {
  base::AutoLock lock(lock_);
  return base::Contains(resource_map_, name);
}

}  // namespace extensions