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
|
// 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 "chrome/browser/ui/webui/local_state/local_state_ui.h"
#include <memory>
#include <string>
#include <vector>
#include "base/functional/bind.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/url_constants.h"
#include "components/grit/dev_ui_components_resources.h"
#include "components/local_state/local_state_utils.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_controller.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/browser/web_ui_message_handler.h"
namespace {
// UI Handler for chrome://local-state. Displays the Local State file as JSON.
class LocalStateUIHandler : public content::WebUIMessageHandler {
public:
LocalStateUIHandler() = default;
LocalStateUIHandler(const LocalStateUIHandler&) = delete;
LocalStateUIHandler& operator=(const LocalStateUIHandler&) = delete;
~LocalStateUIHandler() override = default;
// content::WebUIMessageHandler:
void RegisterMessages() override;
private:
// Called from JS when the page has loaded. Serializes local state prefs and
// sends them to the page.
void HandleRequestJson(const base::Value::List& args);
#if BUILDFLAG(IS_CHROMEOS)
// On ChromeOS, the local state file contains some information about other
// user accounts which we don't want to expose to other users. In that case,
// this will filter out the prefs to only include variations and UMA related
// fields, which don't contain PII.
std::vector<std::string> accepted_pref_prefixes_{"variations",
"user_experience_metrics"};
#else
std::vector<std::string> accepted_pref_prefixes_;
#endif // BUILDFLAG(IS_CHROMEOS)
};
void LocalStateUIHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"requestJson",
base::BindRepeating(&LocalStateUIHandler::HandleRequestJson,
base::Unretained(this)));
}
void LocalStateUIHandler::HandleRequestJson(const base::Value::List& args) {
AllowJavascript();
std::optional<std::string> json = local_state_utils::GetPrefsAsJson(
g_browser_process->local_state(), accepted_pref_prefixes_);
if (!json) {
json = "Error loading Local State file.";
}
const base::Value& callback_id = args[0];
ResolveJavascriptCallback(callback_id, base::Value(*json));
}
} // namespace
LocalStateUI::LocalStateUI(content::WebUI* web_ui) : WebUIController(web_ui) {
// Set up the chrome://local-state source.
content::WebUIDataSource* html_source =
content::WebUIDataSource::CreateAndAdd(Profile::FromWebUI(web_ui),
chrome::kChromeUILocalStateHost);
html_source->SetDefaultResource(IDR_LOCAL_STATE_HTML);
html_source->AddResourcePath("local_state.js", IDR_LOCAL_STATE_JS);
web_ui->AddMessageHandler(std::make_unique<LocalStateUIHandler>());
}
LocalStateUI::~LocalStateUI() = default;
|