Skip to content

Commit

Permalink
carrier_lock: Import topic subscription request
Browse files Browse the repository at this point in the history
This commit defines topic subscription request locally and changes the
FCM class to use this local implementation instead of gcm driver.
It can be removed when subscription is supported by GCM driver.

Bug: b:262288980
Test: CarrierLockSubscribeFcmTopic

Change-Id: Ibc62ebdcfd2067400b16c80d24c90d44ce03ccc6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4773581
Reviewed-by: Regan Hsu <hsuregan@chromium.org>
Commit-Queue: Michał Mazur <michamazur@google.com>
Cr-Commit-Position: refs/heads/main@{#1209892}
  • Loading branch information
Michal Mazur authored and Chromium LUCI CQ committed Oct 15, 2023
1 parent 0924ef8 commit 8f9c3ae
Show file tree
Hide file tree
Showing 8 changed files with 707 additions and 36 deletions.
3 changes: 3 additions & 0 deletions chromeos/ash/components/carrier_lock/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ component("carrier_lock") {
"psm_claim_verifier.h",
"psm_claim_verifier_impl.cc",
"psm_claim_verifier_impl.h",
"topic_subscription_request.cc",
"topic_subscription_request.h",
]
}

Expand All @@ -57,6 +59,7 @@ source_set("unit_tests") {
"provisioning_config_fetcher_unittest.cc",
"psm_claim_verifier_unittest.cc",
"psm_claim_verifier_unittest.h",
"topic_subscription_request_unittest.cc",
]

deps = [
Expand Down
7 changes: 7 additions & 0 deletions chromeos/ash/components/carrier_lock/fcm_topic_subscriber.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CARRIER_LOCK)

// Return the Token received in response
virtual std::string const token() = 0;

protected:
friend class FcmTopicSubscriberTest;

void set_is_testing(bool testing) { is_testing_ = testing; }
bool is_testing() { return is_testing_; }
bool is_testing_ = false;
};

} // namespace ash::carrier_lock
Expand Down
58 changes: 26 additions & 32 deletions chromeos/ash/components/carrier_lock/fcm_topic_subscriber_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ FcmTopicSubscriberImpl::FcmTopicSubscriberImpl(
std::string sender_id,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
: gcm_driver_(gcm_driver),
url_loader_factory_(url_loader_factory),
app_id_(app_id),
sender_id_(sender_id),
weakptr_factory_(this) {
Expand Down Expand Up @@ -101,7 +102,17 @@ void FcmTopicSubscriberImpl::OnGetToken(
instance_id::InstanceID::Result result) {
if (result == instance_id::InstanceID::Result::SUCCESS) {
token_ = token;
ReturnSuccess();
if (is_testing()) {
// The unit tests use FakeGCMDriverForInstanceID which does not implement
// GetGCMStatistics method and the call below times out. Returning success
// here is needed to continue with the values defined in unit test.
ReturnSuccess();
return;
}
gcm_driver_->GetGCMStatistics(
base::BindOnce(&FcmTopicSubscriberImpl::OnGetGcmStatistics,
weakptr_factory_.GetWeakPtr()),
gcm::GCMDriver::KEEP_LOGS);
return;
}

Expand All @@ -126,47 +137,30 @@ void FcmTopicSubscriberImpl::OnGetToken(
}
}

void FcmTopicSubscriberImpl::OnGetGcmStatistics(
const gcm::GCMClient::GCMStatistics& statistics) {
android_id_ = statistics.android_id;
android_secret_ = statistics.android_secret;
ReturnSuccess();
}

void FcmTopicSubscriberImpl::Subscribe(Result request_token_result) {
request_callback_ = std::move(subscribe_callback_);
if (request_token_result != Result::kSuccess) {
ReturnError(request_token_result);
return;
}
if (topic_.empty()) {
if (topic_.empty() || !android_secret_) {
ReturnError(Result::kInvalidInput);
return;
}

// TODO Implement SubscribeToTopic request in GCM driver
ReturnSuccess();
}

void FcmTopicSubscriberImpl::OnSubscribe(
instance_id::InstanceID::Result result) {
if (result == instance_id::InstanceID::Result::SUCCESS) {
ReturnSuccess();
return;
}

LOG(ERROR) << "Failed to subscribe to FCM topic";
switch (result) {
case instance_id::InstanceID::INVALID_PARAMETER:
ReturnError(Result::ERR_INVALID_INPUT);
return;
case instance_id::InstanceID::DISABLED:
ReturnError(Result::ERR_INITIALIZATION);
return;
case instance_id::InstanceID::NETWORK_ERROR:
ReturnError(Result::ERR_CONNECTION);
return;
case instance_id::InstanceID::SERVER_ERROR:
ReturnError(Result::ERR_SERVER_INTERNAL);
return;
case instance_id::InstanceID::ASYNC_OPERATION_PENDING:
case instance_id::InstanceID::UNKNOWN_ERROR:
default:
ReturnError(Result::ERR_REQUEST_FAILED);
}
TopicSubscriptionRequest::RequestInfo request_info(
android_id_, android_secret_, app_id_, token_, topic_,
/*unsubscribe=*/false);
subscription_request_ = std::make_unique<TopicSubscriptionRequest>(
request_info, url_loader_factory_, std::move(request_callback_));
subscription_request_->Start();
}

void FcmTopicSubscriberImpl::ReturnError(Result err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chromeos/ash/components/carrier_lock/fcm_topic_subscriber.h"
#include "chromeos/ash/components/carrier_lock/topic_subscription_request.h"
#include "components/gcm_driver/gcm_app_handler.h"
#include "components/gcm_driver/instance_id/instance_id.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
Expand Down Expand Up @@ -45,10 +46,12 @@ class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CARRIER_LOCK)
std::string const token() override;

private:
friend class FcmTopicSubscriberTest;

void Subscribe(Result request_token_result);
void OnGetToken(const std::string& token,
instance_id::InstanceID::Result result);
void OnSubscribe(instance_id::InstanceID::Result result);
void OnGetGcmStatistics(const gcm::GCMClient::GCMStatistics& statistics);

void ReturnError(Result);
void ReturnSuccess();
Expand All @@ -68,7 +71,11 @@ class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CARRIER_LOCK)

raw_ptr<gcm::GCMDriver> gcm_driver_;
std::unique_ptr<instance_id::InstanceIDDriver> instance_id_driver_;
std::unique_ptr<TopicSubscriptionRequest> subscription_request_;
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;

uint64_t android_id_ = 0;
uint64_t android_secret_ = 0;
std::string app_id_;
std::string sender_id_;
std::string token_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#include "base/test/test_future.h"
#include "base/time/time.h"
#include "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "services/network/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gtest/include/gtest/gtest.h"

Expand All @@ -22,6 +25,9 @@ const char kEmbeddedAppIdKey[] = "gcmb";
const char kFcmAppId[] = "com.google.chromeos.carrier_lock";
const char kFcmSenderId[] = "1067228791894";
const char kFcmTopic[] = "/topics/testtopic";
const uint64_t kTestAndroidSecret = 1234;
const uint64_t kTestAndroidId = 1234;
const char kFcmUrl[] = "https://android.clients.google.com/c2dm/register3";

} // namespace

Expand All @@ -37,14 +43,22 @@ class FcmTopicSubscriberTest : public testing::Test {
protected:
// testing::Test:
void SetUp() override {
fcm_ = std::make_unique<FcmTopicSubscriberImpl>(&gcm_driver_, kFcmAppId,
kFcmSenderId, nullptr);
shared_factory_ =
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&test_url_loader_factory_);
fcm_ = std::make_unique<FcmTopicSubscriberImpl>(
&gcm_driver_, kFcmAppId, kFcmSenderId, shared_factory_);
fcm_->android_id_ = kTestAndroidId;
fcm_->android_secret_ = kTestAndroidSecret;
fcm_->set_is_testing(true);
}

void TearDown() override { fcm_.reset(); }

std::unique_ptr<FcmTopicSubscriber> fcm_;
std::unique_ptr<FcmTopicSubscriberImpl> fcm_;
base::test::TaskEnvironment task_environment_;
scoped_refptr<network::SharedURLLoaderFactory> shared_factory_;
network::TestURLLoaderFactory test_url_loader_factory_;
instance_id::FakeGCMDriverForInstanceID gcm_driver_;
};

Expand All @@ -57,6 +71,9 @@ TEST_F(FcmTopicSubscriberTest, CarrierLockSubscribeTopicSuccess) {
base::BindRepeating(&FcmTopicSubscriberTest::NotificationCallback,
base::Unretained(this)),
future.GetCallback());
EXPECT_TRUE(test_url_loader_factory_.SimulateResponseForPendingRequest(
GURL(kFcmUrl), network::URLLoaderCompletionStatus(net::OK),
network::CreateURLResponseHead(net::HTTP_OK), std::string("{}")));

// Wait for callback
EXPECT_EQ(Result::kSuccess, future.Get());
Expand All @@ -70,6 +87,9 @@ TEST_F(FcmTopicSubscriberTest, CarrierLockTestNotifications) {
// Request token and subscribe with valid topic
fcm_->SubscribeTopic(kFcmTopic, notifications.GetCallback(),
future.GetCallback());
EXPECT_TRUE(test_url_loader_factory_.SimulateResponseForPendingRequest(
GURL(kFcmUrl), network::URLLoaderCompletionStatus(net::OK),
network::CreateURLResponseHead(net::HTTP_OK), std::string("{}")));

// Wait for subscription callback
EXPECT_EQ(Result::kSuccess, future.Take());
Expand Down Expand Up @@ -146,6 +166,9 @@ TEST_F(FcmTopicSubscriberTest, CarrierLockGetTokenAndSubscribe) {
base::BindRepeating(&FcmTopicSubscriberTest::NotificationCallback,
base::Unretained(this)),
future.GetCallback());
EXPECT_TRUE(test_url_loader_factory_.SimulateResponseForPendingRequest(
GURL(kFcmUrl), network::URLLoaderCompletionStatus(net::OK),
network::CreateURLResponseHead(net::HTTP_OK), std::string("{}")));

// Wait for callback
EXPECT_EQ(Result::kSuccess, future.Take());
Expand Down

0 comments on commit 8f9c3ae

Please sign in to comment.