Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix threading-related crash in Cobalt telemetry. #689

Merged
merged 1 commit into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cobalt/browser/application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1061,8 +1061,7 @@ Application::~Application() {

// Explicitly delete the global metrics services manager here to give it
// an opportunity to clean up late logs and persist metrics.
metrics::CobaltMetricsServicesManager::DeleteInstance(
metrics_services_manager_);
metrics::CobaltMetricsServicesManager::DeleteInstance();

#if defined(ENABLE_DEBUGGER) && defined(STARBOARD_ALLOWS_MEMORY_TRACKING)
memory_tracker_tool_.reset(NULL);
Expand Down
4 changes: 2 additions & 2 deletions cobalt/browser/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
#include "cobalt/base/event_dispatcher.h"
#include "cobalt/browser/browser_module.h"
#include "cobalt/browser/memory_tracker/tool.h"
#include "cobalt/browser/metrics/cobalt_metrics_services_manager.h"
#include "cobalt/network/network_module.h"
#include "cobalt/persistent_storage/persistent_settings.h"
#include "cobalt/system_window/system_window.h"
#include "components/metrics_services_manager/metrics_services_manager.h"
#include "starboard/time.h"
#if SB_IS(EVERGREEN)
#include "cobalt/updater/updater_module.h"
Expand Down Expand Up @@ -225,7 +225,7 @@ class Application {
void DispatchDeepLink(const char* link, SbTimeMonotonic timestamp);
void DispatchDeepLinkIfNotConsumed();

metrics_services_manager::MetricsServicesManager* metrics_services_manager_;
metrics::CobaltMetricsServicesManager* metrics_services_manager_;

DISALLOW_COPY_AND_ASSIGN(Application);
};
Expand Down
4 changes: 2 additions & 2 deletions cobalt/browser/metrics/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ static_library("metrics") {
sources = [
"cobalt_enabled_state_provider.cc",
"cobalt_enabled_state_provider.h",
"cobalt_h5vcc_metrics_uploader_callback.cc",
"cobalt_h5vcc_metrics_uploader_callback.h",
"cobalt_metrics_log_uploader.cc",
"cobalt_metrics_log_uploader.h",
"cobalt_metrics_service_client.cc",
"cobalt_metrics_service_client.h",
"cobalt_metrics_services_manager.cc",
"cobalt_metrics_services_manager.h",
"cobalt_metrics_services_manager_client.cc",
"cobalt_metrics_services_manager_client.h",
"cobalt_metrics_uploader_callback.h",
Expand Down
28 changes: 0 additions & 28 deletions cobalt/browser/metrics/cobalt_h5vcc_metrics_uploader_callback.cc

This file was deleted.

48 changes: 0 additions & 48 deletions cobalt/browser/metrics/cobalt_h5vcc_metrics_uploader_callback.h

This file was deleted.

5 changes: 3 additions & 2 deletions cobalt/browser/metrics/cobalt_metrics_log_uploader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@

#include "cobalt/browser/metrics/cobalt_metrics_log_uploader.h"

#include "base/logging.h"
#include "cobalt/browser/metrics/cobalt_metrics_uploader_callback.h"
#include "cobalt/h5vcc/h5vcc_metric_type.h"
#include "components/metrics/log_decoder.h"
#include "components/metrics/metrics_log_uploader.h"
#include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h"
#include "third_party/metrics_proto/reporting_info.pb.h"


namespace cobalt {
namespace browser {
namespace metrics {
Expand Down Expand Up @@ -51,7 +52,7 @@ void CobaltMetricsLogUploader::UploadLog(
}

void CobaltMetricsLogUploader::SetOnUploadHandler(
CobaltMetricsUploaderCallback* upload_handler) {
const CobaltMetricsUploaderCallback* upload_handler) {
upload_handler_ = upload_handler;
}

Expand Down
5 changes: 3 additions & 2 deletions cobalt/browser/metrics/cobalt_metrics_log_uploader.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ class CobaltMetricsLogUploader : public ::metrics::MetricsLogUploader {

// Sets the event handler wrapper to be called when metrics are ready for
// upload. This should be the JavaScript H5vcc callback implementation.
void SetOnUploadHandler(CobaltMetricsUploaderCallback* metric_event_handler);
void SetOnUploadHandler(
const CobaltMetricsUploaderCallback* metric_event_handler);

private:
const ::metrics::MetricsLogUploader::MetricServiceType service_type_;
const ::metrics::MetricsLogUploader::UploadCallback on_upload_complete_;
CobaltMetricsUploaderCallback* upload_handler_ = nullptr;
const CobaltMetricsUploaderCallback* upload_handler_ = nullptr;
};

} // namespace metrics
Expand Down
19 changes: 8 additions & 11 deletions cobalt/browser/metrics/cobalt_metrics_log_uploader_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <memory>

#include "base/test/mock_callback.h"
#include "cobalt/browser/metrics/cobalt_metrics_uploader_callback.h"
#include "cobalt/h5vcc/h5vcc_metrics.h"
#include "testing/gmock/include/gmock/gmock.h"
Expand All @@ -24,7 +25,6 @@
#include "third_party/metrics_proto/reporting_info.pb.h"
#include "third_party/zlib/google/compression_utils.h"


namespace cobalt {
namespace browser {
namespace metrics {
Expand All @@ -37,6 +37,7 @@ using ::testing::Eq;
using ::testing::StrEq;
using ::testing::StrictMock;


class CobaltMetricsLogUploaderTest : public ::testing::Test {
public:
void SetUp() override {
Expand All @@ -58,15 +59,10 @@ class CobaltMetricsLogUploaderTest : public ::testing::Test {
int callback_count_ = 0;
};

class MockMetricsUploaderCallback : public CobaltMetricsUploaderCallback {
public:
MOCK_METHOD2(Run, void(const cobalt::h5vcc::H5vccMetricType& type,
const std::string& payload));
};

TEST_F(CobaltMetricsLogUploaderTest, TriggersUploadHandler) {
StrictMock<MockMetricsUploaderCallback> mock_upload_handler;
uploader_->SetOnUploadHandler(&mock_upload_handler);
base::MockCallback<CobaltMetricsUploaderCallback> mock_upload_handler;
const auto cb = mock_upload_handler.Get();
uploader_->SetOnUploadHandler(&cb);
::metrics::ReportingInfo dummy_reporting_info;
::metrics::ChromeUserMetricsExtension uma_log;
uma_log.set_session_id(1234);
Expand Down Expand Up @@ -98,8 +94,9 @@ TEST_F(CobaltMetricsLogUploaderTest, UnknownMetricTypeDoesntTriggerUpload) {
::metrics::MetricsLogUploader::MetricServiceType::UKM,
base::Bind(&CobaltMetricsLogUploaderTest::UploadCompleteCallback,
base::Unretained(this))));
StrictMock<MockMetricsUploaderCallback> mock_upload_handler;
uploader_->SetOnUploadHandler(&mock_upload_handler);
base::MockCallback<CobaltMetricsUploaderCallback> mock_upload_handler;
const auto cb = mock_upload_handler.Get();
uploader_->SetOnUploadHandler(&cb);
::metrics::ReportingInfo dummy_reporting_info;
::metrics::ChromeUserMetricsExtension uma_log;
uma_log.set_session_id(1234);
Expand Down
2 changes: 1 addition & 1 deletion cobalt/browser/metrics/cobalt_metrics_service_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace metrics {
const int kStandardUploadIntervalSeconds = 5 * 60; // 5 minutes.

void CobaltMetricsServiceClient::SetOnUploadHandler(
CobaltMetricsUploaderCallback* uploader_callback) {
const CobaltMetricsUploaderCallback* uploader_callback) {
upload_handler_ = uploader_callback;
if (log_uploader_) {
log_uploader_->SetOnUploadHandler(upload_handler_);
Expand Down
5 changes: 3 additions & 2 deletions cobalt/browser/metrics/cobalt_metrics_service_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class CobaltMetricsServiceClient : public ::metrics::MetricsServiceClient {

// Sets the uploader handler to be called when metrics are ready for
// upload.
void SetOnUploadHandler(CobaltMetricsUploaderCallback* uploader_callback);
void SetOnUploadHandler(
const CobaltMetricsUploaderCallback* uploader_callback);

// Returns the MetricsService instance that this client is associated with.
// With the exception of testing contexts, the returned instance must be valid
Expand Down Expand Up @@ -164,7 +165,7 @@ class CobaltMetricsServiceClient : public ::metrics::MetricsServiceClient {

CobaltMetricsLogUploader* log_uploader_ = nullptr;

CobaltMetricsUploaderCallback* upload_handler_ = nullptr;
const CobaltMetricsUploaderCallback* upload_handler_ = nullptr;

uint32_t custom_upload_interval_ = UINT32_MAX;

Expand Down
98 changes: 98 additions & 0 deletions cobalt/browser/metrics/cobalt_metrics_services_manager.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2023 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "cobalt/browser/metrics/cobalt_metrics_services_manager.h"

#include <memory>

#include "cobalt/browser/metrics/cobalt_metrics_service_client.h"
#include "cobalt/browser/metrics/cobalt_metrics_services_manager_client.h"
#include "components/metrics_services_manager/metrics_services_manager.h"

namespace cobalt {
namespace browser {
namespace metrics {

CobaltMetricsServicesManager* CobaltMetricsServicesManager::instance_ = nullptr;

CobaltMetricsServicesManager::CobaltMetricsServicesManager()
: task_runner_(base::ThreadTaskRunnerHandle::Get()),
metrics_services_manager::MetricsServicesManager(
std::make_unique<CobaltMetricsServicesManagerClient>()) {}


// Static Singleton getter for metrics services manager.
CobaltMetricsServicesManager* CobaltMetricsServicesManager::GetInstance() {
if (instance_ == nullptr) {
instance_ = new CobaltMetricsServicesManager();
}
return instance_;
}

void CobaltMetricsServicesManager::DeleteInstance() { delete instance_; }

void CobaltMetricsServicesManager::SetOnUploadHandler(
const CobaltMetricsUploaderCallback* uploader_callback) {
instance_->task_runner_->PostTask(
FROM_HERE,
base::Bind(&CobaltMetricsServicesManager::SetOnUploadHandlerInternal,
base::Unretained(instance_), uploader_callback));
}

void CobaltMetricsServicesManager::SetOnUploadHandlerInternal(
const CobaltMetricsUploaderCallback* uploader_callback) {
CobaltMetricsServiceClient* client =
static_cast<CobaltMetricsServiceClient*>(GetMetricsServiceClient());
DCHECK(client);
client->SetOnUploadHandler(uploader_callback);
}

void CobaltMetricsServicesManager::ToggleMetricsEnabled(bool is_enabled) {
instance_->task_runner_->PostTask(
FROM_HERE,
base::Bind(&CobaltMetricsServicesManager::ToggleMetricsEnabledInternal,
base::Unretained(instance_), is_enabled));
}
void CobaltMetricsServicesManager::ToggleMetricsEnabledInternal(
bool is_enabled) {
CobaltMetricsServicesManagerClient* client =
static_cast<CobaltMetricsServicesManagerClient*>(
GetMetricsServicesManagerClient());
DCHECK(client);
client->GetEnabledStateProvider()->SetConsentGiven(is_enabled);
client->GetEnabledStateProvider()->SetReportingEnabled(is_enabled);
UpdateUploadPermissions(is_enabled);
}

void CobaltMetricsServicesManager::SetUploadInterval(
uint32_t interval_seconds) {
instance_->task_runner_->PostTask(
FROM_HERE,
base::Bind(&CobaltMetricsServicesManager::SetUploadIntervalInternal,
base::Unretained(instance_), interval_seconds));
}

void CobaltMetricsServicesManager::SetUploadIntervalInternal(
uint32_t interval_seconds) {
browser::metrics::CobaltMetricsServiceClient* client =
static_cast<browser::metrics::CobaltMetricsServiceClient*>(
GetMetricsServiceClient());
DCHECK(client);
client->SetUploadInterval(interval_seconds);
}


} // namespace metrics
} // namespace browser
} // namespace cobalt
Loading