summaryrefslogtreecommitdiff
path: root/chromium/components/about_ui/credit_utils.cc
blob: 06c6c372f79042ab978f449e071de8968d413f0c (plain)
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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/about_ui/credit_utils.h"

#include <stdint.h>

#include "base/strings/string_piece.h"
#include "components/grit/components_resources.h"
#include "third_party/brotli/include/brotli/decode.h"
#include "ui/base/resource/resource_bundle.h"

#if defined(OS_ANDROID)
#include "base/android/jni_array.h"
#include "jni/CreditUtils_jni.h"
#endif

namespace about_ui {

std::string GetCredits(bool include_scripts) {
  std::string response;
  base::StringPiece raw_response =
      ui::ResourceBundle::GetSharedInstance().GetRawDataResource(
          IDR_ABOUT_UI_CREDITS_HTML);
  const uint8_t* next_encoded_byte =
      reinterpret_cast<const uint8_t*>(raw_response.data());
  size_t input_size_remaining = raw_response.size();
  BrotliDecoderState* decoder = BrotliDecoderCreateInstance(
      nullptr /* no custom allocator */, nullptr /* no custom deallocator */,
      nullptr /* no custom memory handle */);
  CHECK(!!decoder);
  while (!BrotliDecoderIsFinished(decoder)) {
    size_t output_size_remaining = 0;
    CHECK(BrotliDecoderDecompressStream(
              decoder, &input_size_remaining, &next_encoded_byte,
              &output_size_remaining, nullptr,
              nullptr) != BROTLI_DECODER_RESULT_ERROR);
    const uint8_t* output_buffer =
        BrotliDecoderTakeOutput(decoder, &output_size_remaining);
    response.insert(response.end(), output_buffer,
                    output_buffer + output_size_remaining);
  }
  BrotliDecoderDestroyInstance(decoder);
  if (include_scripts) {
    response +=
        "\n<script src=\"chrome://resources/js/cr.js\"></script>\n"
        "<script src=\"chrome://credits/credits.js\"></script>\n";
  }
  response += "</body>\n</html>";
  return response;
}

#if defined(OS_ANDROID)
static base::android::ScopedJavaLocalRef<jbyteArray>
JNI_CreditUtils_GetJavaWrapperCredits(
    JNIEnv* env,
    const base::android::JavaParamRef<jclass>& clazz) {
  std::string html_content = GetCredits(false);
  const char* html_content_arr = html_content.c_str();
  return base::android::ToJavaByteArray(
      env, reinterpret_cast<const uint8_t*>(html_content_arr),
      html_content.size());
}
#endif

}  // namespace about_ui