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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
// 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/gcm_internals_ui.h"
#include <memory>
#include <vector>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "chrome/browser/gcm/gcm_profile_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/url_constants.h"
#include "components/gcm_driver/gcm_client.h"
#include "components/gcm_driver/gcm_driver.h"
#include "components/gcm_driver/gcm_internals_constants.h"
#include "components/gcm_driver/gcm_internals_helper.h"
#include "components/gcm_driver/gcm_profile_service.h"
#include "components/grit/gcm_internals_resources.h"
#include "components/grit/gcm_internals_resources_map.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"
#include "ui/webui/webui_util.h"
GCMInternalsUIConfig::GCMInternalsUIConfig()
: DefaultWebUIConfig(content::kChromeUIScheme,
chrome::kChromeUIGCMInternalsHost) {}
namespace {
// Class acting as a controller of the chrome://gcm-internals WebUI.
class GcmInternalsUIMessageHandler : public content::WebUIMessageHandler {
public:
GcmInternalsUIMessageHandler();
GcmInternalsUIMessageHandler(const GcmInternalsUIMessageHandler&) = delete;
GcmInternalsUIMessageHandler& operator=(const GcmInternalsUIMessageHandler&) =
delete;
~GcmInternalsUIMessageHandler() override;
// WebUIMessageHandler implementation.
void RegisterMessages() override;
void OnJavascriptDisallowed() override;
private:
// Return all of the GCM related infos to the gcm-internals page by calling
// Javascript callback function
// |gcm-internals.returnInfo()|.
void ReturnResults(Profile* profile,
gcm::GCMProfileService* profile_service,
const gcm::GCMClient::GCMStatistics* stats);
// Request all of the GCM related infos through gcm profile service.
void RequestAllInfo(const base::Value::List& args);
// Enables/disables GCM activity recording through gcm profile service.
void SetRecording(const base::Value::List& args);
// Callback function of the request for all gcm related infos.
void RequestGCMStatisticsFinished(const gcm::GCMClient::GCMStatistics& args);
// Factory for creating references in callbacks.
base::WeakPtrFactory<GcmInternalsUIMessageHandler> weak_ptr_factory_{this};
};
GcmInternalsUIMessageHandler::GcmInternalsUIMessageHandler() = default;
GcmInternalsUIMessageHandler::~GcmInternalsUIMessageHandler() = default;
void GcmInternalsUIMessageHandler::ReturnResults(
Profile* profile,
gcm::GCMProfileService* profile_service,
const gcm::GCMClient::GCMStatistics* stats) {
base::Value::Dict results = gcm_driver::SetGCMInternalsInfo(
stats, profile_service, profile->GetPrefs());
FireWebUIListener(gcm_driver::kSetGcmInternalsInfo, results);
}
void GcmInternalsUIMessageHandler::RequestAllInfo(
const base::Value::List& list) {
AllowJavascript();
if (list.size() != 1) {
NOTREACHED();
}
const bool clear_logs = list[0].GetBool();
gcm::GCMDriver::ClearActivityLogs clear_activity_logs =
clear_logs ? gcm::GCMDriver::CLEAR_LOGS : gcm::GCMDriver::KEEP_LOGS;
Profile* profile = Profile::FromWebUI(web_ui());
gcm::GCMProfileService* profile_service =
gcm::GCMProfileServiceFactory::GetForProfile(profile);
if (!profile_service || !profile_service->driver()) {
ReturnResults(profile, nullptr, nullptr);
} else {
profile_service->driver()->GetGCMStatistics(
base::BindOnce(
&GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished,
weak_ptr_factory_.GetWeakPtr()),
clear_activity_logs);
}
}
void GcmInternalsUIMessageHandler::SetRecording(const base::Value::List& list) {
if (list.size() != 1) {
NOTREACHED();
}
const bool recording = list[0].GetBool();
Profile* profile = Profile::FromWebUI(web_ui());
gcm::GCMProfileService* profile_service =
gcm::GCMProfileServiceFactory::GetForProfile(profile);
if (!profile_service) {
ReturnResults(profile, nullptr, nullptr);
return;
}
// Get fresh stats after changing recording setting.
profile_service->driver()->SetGCMRecording(
base::BindRepeating(
&GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished,
weak_ptr_factory_.GetWeakPtr()),
recording);
}
void GcmInternalsUIMessageHandler::RequestGCMStatisticsFinished(
const gcm::GCMClient::GCMStatistics& stats) {
Profile* profile = Profile::FromWebUI(web_ui());
DCHECK(profile);
gcm::GCMProfileService* profile_service =
gcm::GCMProfileServiceFactory::GetForProfile(profile);
DCHECK(profile_service);
ReturnResults(profile, profile_service, &stats);
}
void GcmInternalsUIMessageHandler::RegisterMessages() {
// It is safe to use base::Unretained here, since web_ui owns this message
// handler.
web_ui()->RegisterMessageCallback(
gcm_driver::kGetGcmInternalsInfo,
base::BindRepeating(&GcmInternalsUIMessageHandler::RequestAllInfo,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
gcm_driver::kSetGcmInternalsRecording,
base::BindRepeating(&GcmInternalsUIMessageHandler::SetRecording,
base::Unretained(this)));
}
void GcmInternalsUIMessageHandler::OnJavascriptDisallowed() {
// Invalidate weak ptrs in order to cancel callbacks from the
// GCMProfileServiceFactory. If the page is being navigated away from, this
// prevents such callbacks from triggering a CHECK by trying to run JS code
// on some other page. If the page is refreshed, this prevents the callbacks
// from triggering a CHECK by trying to run JS code on the refreshed page when
// the JS side is not yet ready, which can lead to a broken UI experience.
weak_ptr_factory_.InvalidateWeakPtrs();
}
} // namespace
GCMInternalsUI::GCMInternalsUI(content::WebUI* web_ui)
: content::WebUIController(web_ui) {
// Set up the chrome://gcm-internals source.
content::WebUIDataSource* html_source =
content::WebUIDataSource::CreateAndAdd(Profile::FromWebUI(web_ui),
chrome::kChromeUIGCMInternalsHost);
// Add required resources.
webui::SetupWebUIDataSource(html_source, kGcmInternalsResources,
IDR_GCM_INTERNALS_GCM_INTERNALS_HTML);
web_ui->AddMessageHandler(std::make_unique<GcmInternalsUIMessageHandler>());
}
GCMInternalsUI::~GCMInternalsUI() = default;
|