美文网首页
Chromium内核源码随机分析

Chromium内核源码随机分析

作者: 艺术类架构师 | 来源:发表于2023-02-01 07:45 被阅读0次

膜拜一下:谷歌浏览器内核应该是全世界最牛逼的基建

这篇文章来看看谷歌浏览器针对android_webview出现crash时做了哪些事情
aw_crash_reporter_client 这个文件做了一个事情就是发报告给安卓客户端
crash_keys 这个类是追踪报告的指标key:http-method-type,url,javascript click event type

<main id="js-repo-pjax-container" style="box-sizing: border-box; display: block;"> <turbo-frame id="repo-content-turbo-frame" target="_top" data-turbo-action="advance" class="" src="https://github.com/chromium/chromium/blob/main/android_webview/common/crash_reporter/crash_keys.cc" complete="" style="box-sizing: border-box; display: block;">| // Copyright 2017 The Chromium Authors |
|  | // Use of this source code is governed by a BSD-style license that can be |
|  | // found in the LICENSE file. |
|  |  |
|  | #include "android_webview/common/crash_reporter/crash_keys.h" |
|  |  |
|  | #include "components/crash/core/common/crash_key.h" |
|  | #include "components/gwp_asan/common/crash_key_name.h" |
|  |  |
|  | namespace android_webview { |
|  | namespace crash_keys { |
|  |  |
|  | const char kAppPackageName[] = "app-package-name"; |
|  | const char kAppPackageVersionCode[] = "app-package-version-code"; |
|  | const char kAppProcessName[] = "app-process-name"; |
|  |  |
|  | const char kAndroidSdkInt[] = "android-sdk-int"; |
|  |  |
|  | const char kSupportLibraryWebkitVersion[] = "androidx-webkit-version"; |
|  |  |
|  | extern const char kWeblayerWebViewCompatMode[] = |
|  | "WEBLAYER_WEB_VIEW_COMPAT_MODE"; |
|  |  |
|  | // clang-format off |
|  | const char* const kWebViewCrashKeyAllowList[] = { |
|  | kAppPackageName, |
|  | kAppPackageVersionCode, |
|  | kAppProcessName, |
|  | kAndroidSdkInt, |
|  | kSupportLibraryWebkitVersion, |
|  |  |
|  | // process type |
|  | "ptype", |
|  |  |
|  | // Java exception stack traces |
|  | "exception_info", |
|  |  |
|  | // gpu |
|  | "gpu-driver", |
|  | "gpu-psver", |
|  | "gpu-vsver", |
|  | "gpu-gl-vendor", |
|  | "gpu-gl-renderer", |
|  | "gr-context-type", |
|  | "oop_read_failure", |
|  | "gpu-gl-error-message", |
|  |  |
|  | // components/viz |
|  | "viz_deserialization", |
|  |  |
|  | // content/: |
|  | "bad_message_reason", |
|  | "discardable-memory-allocated", |
|  | "discardable-memory-free", |
|  | "mojo-message-error", |
|  | "total-discardable-memory-allocated", |
|  |  |
|  | // Navigation |
|  | "VerifyDidCommit-prev_ldwb", |
|  | "VerifyDidCommit-prev_ldwbu", |
|  | "VerifyDidCommit-base_url_fdu_type", |
|  | "VerifyDidCommit-data_url_empty", |
|  | "VerifyDidCommit-history_url_fdu_type", |
|  |  |
|  | "VerifyDidCommit-intended_browser", |
|  | "VerifyDidCommit-intended_renderer", |
|  |  |
|  | "VerifyDidCommit-method_post_browser", |
|  | "VerifyDidCommit-method_post_renderer", |
|  | "VerifyDidCommit-original_method_post", |
|  |  |
|  | "VerifyDidCommit-unreachable_browser", |
|  | "VerifyDidCommit-unreachable_renderer", |
|  |  |
|  | "VerifyDidCommit-post_id_matches", |
|  | "VerifyDidCommit-post_id_-1_browser", |
|  | "VerifyDidCommit-post_id_-1_renderer", |
|  |  |
|  | "VerifyDidCommit-override_ua_browser", |
|  | "VerifyDidCommit-override_ua_renderer", |
|  |  |
|  | "VerifyDidCommit-code_browser", |
|  | "VerifyDidCommit-code_renderer", |
|  |  |
|  | "VerifyDidCommit-suh_browser", |
|  | "VerifyDidCommit-suh_renderer", |
|  |  |
|  | "VerifyDidCommit-gesture_browser", |
|  | "VerifyDidCommit-gesture_renderer", |
|  |  |
|  | "VerifyDidCommit-replace_browser", |
|  | "VerifyDidCommit-replace_renderer", |
|  |  |
|  | "VerifyDidCommit-url_relation", |
|  | "VerifyDidCommit-url_browser_type", |
|  | "VerifyDidCommit-url_renderer_type", |
|  |  |
|  | "VerifyDidCommit-is_same_document", |
|  | "VerifyDidCommit-is_history_api", |
|  | "VerifyDidCommit-renderer_initiated", |
|  | "VerifyDidCommit-is_subframe", |
|  | "VerifyDidCommit-is_form_submission", |
|  | "VerifyDidCommit-is_error_page", |
|  | "VerifyDidCommit-net_error", |
|  |  |
|  | "VerifyDidCommit-is_server_redirect", |
|  | "VerifyDidCommit-redirects_size", |
|  |  |
|  | "VerifyDidCommit-entry_offset", |
|  | "VerifyDidCommit-entry_count", |
|  | "VerifyDidCommit-last_committed_index", |
|  |  |
|  | "VerifyDidCommit-is_reload", |
|  | "VerifyDidCommit-is_restore", |
|  | "VerifyDidCommit-is_history", |
|  | "VerifyDidCommit-has_valid_page_state", |
|  |  |
|  | "VerifyDidCommit-has_gesture", |
|  | "VerifyDidCommit-was_click", |
|  |  |
|  | "VerifyDidCommit-original_same_doc", |
|  |  |
|  | "VerifyDidCommit-committed_real_load", |
|  | "VerifyDidCommit-last_url_type", |
|  |  |
|  | "VerifyDidCommit-last_method", |
|  | "VerifyDidCommit-last_code", |
|  |  |
|  | "VerifyDidCommit-has_si_url", |
|  |  |
|  | // services/network |
|  | "network_deserialization", |
|  |  |
|  | // GWP-ASan |
|  | gwp_asan::kMallocCrashKey, |
|  | gwp_asan::kPartitionAllocCrashKey, |
|  |  |
|  | // crash keys needed for recording finch trials |
|  | "variations", |
|  | "num-experiments", |
|  |  |
|  | // sandbox/linux |
|  | "seccomp-sigsys", |
|  |  |
|  | kWeblayerWebViewCompatMode, |
|  |  |
|  | nullptr}; |
|  | // clang-format on |
|  |  |
|  | void InitCrashKeysForWebViewTesting() { |
|  | crash_reporter::InitializeCrashKeys(); |
|  | } |
|  |  |
|  | } // namespace crash_keys |
|  |  |
|  | } // namespace android_webview |

<details class="details-reset details-overlay details-overlay-dark" id="jumpto-line-details-dialog" style="box-sizing: border-box; display: block;"><summary data-hotkey="l" aria-label="Jump to line" role="button" style="box-sizing: border-box; display: list-item; cursor: pointer; list-style: none; transition: color 80ms cubic-bezier(0.33, 1, 0.68, 1) 0s, background-color, box-shadow, border-color;"></summary></details></turbo-frame> </main>

<footer class="footer width-full container-xl p-responsive" style="box-sizing: border-box; display: block; max-width: 1280px; margin-right: auto; margin-left: auto; width: 1280px; padding-right: var(--base-size-16, 16px)  !important; padding-left: var(--base-size-16, 16px)  !important;">

## Footer

[](https://github.com/ "GitHub")

</footer>

/ Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "android_webview/common/crash_reporter/aw_crash_reporter_client.h"

#include <stdint.h>

#include "android_webview/common/aw_descriptors.h"
#include "android_webview/common/aw_paths.h"
#include "android_webview/common/aw_switches.h"
#include "android_webview/common/crash_reporter/crash_keys.h"
#include "android_webview/common_jni_headers/AwCrashReporterClient_jni.h"
#include "base/android/build_info.h"
#include "base/android/java_exception_reporter.h"
#include "base/android/jni_android.h"
#include "base/base_paths_android.h"
#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/path_service.h"
#include "base/scoped_native_library.h"
#include "components/crash/core/app/crash_reporter_client.h"
#include "components/crash/core/app/crashpad.h"
#include "components/version_info/android/channel_getter.h"
#include "components/version_info/version_info.h"
#include "components/version_info/version_info_values.h"

using base::android::AttachCurrentThread;

namespace android_webview {

namespace {

class AwCrashReporterClient : public crash_reporter::CrashReporterClient {
 public:
  AwCrashReporterClient() = default;

  AwCrashReporterClient(const AwCrashReporterClient&) = delete;
  AwCrashReporterClient& operator=(const AwCrashReporterClient&) = delete;

  // crash_reporter::CrashReporterClient implementation.
  bool IsRunningUnattended() override { return false; }
  bool GetCollectStatsConsent() override {
    // TODO(jperaza): Crashpad uses GetCollectStatsConsent() to enable or
    // disable upload of crash reports. However, Crashpad does not yet support
    // upload on Android, so this return value currently has no effect and
    // WebView's own uploader will determine consent before uploading. If and
    // when Crashpad supports upload on Android, consent can be determined here,
    // or WebView can continue uploading reports itself.
    return false;
  }

  void GetProductNameAndVersion(std::string* product_name,
                                std::string* version,
                                std::string* channel) override {
    *product_name = "AndroidWebView";
    *version = PRODUCT_VERSION;
    *channel =
        version_info::GetChannelString(version_info::android::GetChannel());
  }

  bool GetCrashDumpLocation(base::FilePath* crash_dir) override {
    //将Dump文件上传到指定目录
    return base::PathService::Get(android_webview::DIR_CRASH_DUMPS, crash_dir);
  }

  void GetSanitizationInformation(const char* const** crash_key_allowlist,
                                  void** target_module,
                                  bool* sanitize_stacks) override {
    *crash_key_allowlist = crash_keys::kWebViewCrashKeyAllowList;
#if defined(COMPONENT_BUILD)
    *target_module = nullptr;
#else
    *target_module = reinterpret_cast<void*>(&EnableCrashReporter);
#endif
    *sanitize_stacks = true;
  }


//整个dump文件
  unsigned int GetCrashDumpPercentage() override { return 100; }

  bool GetBrowserProcessType(std::string* ptype) override {
    if (!base::CommandLine::ForCurrentProcess()->HasSwitch(

            switches::kWebViewSandboxedRenderer)) {
      // In single process mode the renderer and browser are in the same
      // process. The process type is "webview" to distinguish this case,
      // and for backwards compatibility.
//webview是沙箱模式,无痕模式
      *ptype = "webview";
    } else {
      // Otherwise, in multi process mode, "browser" suffices.
      *ptype = "browser";
    }
    return true;
  }

  bool ShouldWriteMinidumpToLog() override { return true; }

  bool JavaExceptionFilter(
      const base::android::JavaRef<jthrowable>& java_exception) {
//打印导致crash的java异常信息
    return Java_AwCrashReporterClient_stackTraceContainsWebViewCode(
        AttachCurrentThread(), java_exception);
  }

  static AwCrashReporterClient* Get() {
    static base::NoDestructor<AwCrashReporterClient> crash_reporter_client;
    return crash_reporter_client.get();
  }
};

bool g_enabled;

}  // namespace

void EnableCrashReporter(const std::string& process_type) {
  if (g_enabled) {
    NOTREACHED() << "EnableCrashReporter called more than once";
    return;
  }

  AwCrashReporterClient* client = AwCrashReporterClient::Get();
  crash_reporter::SetCrashReporterClient(client);
  crash_reporter::InitializeCrashpad(process_type.empty(), process_type);
  if (process_type.empty()) {
    base::android::InitJavaExceptionReporter();
  } else {
   //为子进程进行打印
    base::android::InitJavaExceptionReporterForChildProcess();
  }
  base::android::SetJavaExceptionFilter(base::BindRepeating(
      &AwCrashReporterClient::JavaExceptionFilter, base::Unretained(client)));
  g_enabled = true;
}

bool CrashReporterEnabled() {
  return g_enabled;
}

}  // namespace android_webview

相关文章

网友评论

      本文标题:Chromium内核源码随机分析

      本文链接:https://www.haomeiwen.com/subject/zhfihdtx.html