diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-12 14:27:29 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-13 09:35:20 +0000 |
commit | c30a6232df03e1efbd9f3b226777b07e087a1122 (patch) | |
tree | e992f45784689f373bcc38d1b79a239ebe17ee23 /chromium/content/browser/ukm_internals_ui.cc | |
parent | 7b5b123ac58f58ffde0f4f6e488bcd09aa4decd3 (diff) | |
download | qtwebengine-chromium-85-based.tar.gz |
BASELINE: Update Chromium to 85.0.4183.14085-based
Change-Id: Iaa42f4680837c57725b1344f108c0196741f6057
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'chromium/content/browser/ukm_internals_ui.cc')
-rw-r--r-- | chromium/content/browser/ukm_internals_ui.cc | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/chromium/content/browser/ukm_internals_ui.cc b/chromium/content/browser/ukm_internals_ui.cc new file mode 100644 index 00000000000..49dab41ab35 --- /dev/null +++ b/chromium/content/browser/ukm_internals_ui.cc @@ -0,0 +1,101 @@ +// Copyright 2018 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 "content/browser/ukm_internals_ui.h" + +#include <stddef.h> + +#include <memory> +#include <string> +#include <utility> + +#include "base/bind.h" +#include "components/ukm/debug/ukm_debug_data_extractor.h" +#include "components/ukm/ukm_service.h" +#include "content/grit/content_resources.h" +#include "content/public/browser/browser_thread.h" +#include "content/public/browser/content_browser_client.h" +#include "content/public/browser/web_contents.h" +#include "content/public/browser/web_ui.h" +#include "content/public/browser/web_ui_data_source.h" +#include "content/public/browser/web_ui_message_handler.h" +#include "content/public/common/content_client.h" +#include "content/public/common/url_constants.h" + +namespace content { +namespace { + +WebUIDataSource* CreateUkmHTMLSource() { + WebUIDataSource* source = WebUIDataSource::Create(kChromeUIUkmHost); + + source->AddResourcePath("ukm_internals.js", IDR_UKM_INTERNALS_JS); + source->AddResourcePath("ukm_internals.css", IDR_UKM_INTERNALS_CSS); + source->SetDefaultResource(IDR_UKM_INTERNALS_HTML); + return source; +} + +// This class receives javascript messages from the renderer. +// Note that the WebUI infrastructure runs on the UI thread, therefore all of +// this class's methods are expected to run on the UI thread. +class UkmMessageHandler : public WebUIMessageHandler { + public: + explicit UkmMessageHandler(const ukm::UkmService* ukm_service); + ~UkmMessageHandler() override; + + // WebUIMessageHandler: + void RegisterMessages() override; + + private: + void HandleRequestUkmData(const base::ListValue* args); + + const ukm::UkmService* ukm_service_; + + DISALLOW_COPY_AND_ASSIGN(UkmMessageHandler); +}; + +UkmMessageHandler::UkmMessageHandler(const ukm::UkmService* ukm_service) + : ukm_service_(ukm_service) {} + +UkmMessageHandler::~UkmMessageHandler() {} + +void UkmMessageHandler::HandleRequestUkmData(const base::ListValue* args) { + AllowJavascript(); + + // Identifies the callback, used for when resolving. + std::string callback_id; + args->GetString(0, &callback_id); + + base::Value ukm_debug_data = + ukm::debug::UkmDebugDataExtractor::GetStructuredData(ukm_service_); + + ResolveJavascriptCallback(base::Value(callback_id), + std::move(ukm_debug_data)); +} + +void UkmMessageHandler::RegisterMessages() { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + + // We can use base::Unretained() here, as both the callback and this class are + // owned by UkmInternalsUI. + web_ui()->RegisterMessageCallback( + "requestUkmData", + base::BindRepeating(&UkmMessageHandler::HandleRequestUkmData, + base::Unretained(this))); +} + +} // namespace + +// Changes to this class should be in sync with its iOS equivalent +// ios/chrome/browser/ui/webui/ukm_internals_ui.cc +UkmInternalsUI::UkmInternalsUI(WebUI* web_ui) : WebUIController(web_ui) { + ukm::UkmService* ukm_service = GetContentClient()->browser()->GetUkmService(); + web_ui->AddMessageHandler(std::make_unique<UkmMessageHandler>(ukm_service)); + + // Set up the chrome://ukm/ source. + BrowserContext* browser_context = + web_ui->GetWebContents()->GetBrowserContext(); + WebUIDataSource::Add(browser_context, CreateUkmHTMLSource()); +} + +} // namespace content |