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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/boca/session_api/session_client_impl.h"
#include "ash/constants/ash_features.h"
#include "base/task/thread_pool.h"
#include "chromeos/ash/components/boca/boca_app_client.h"
#include "chromeos/ash/components/boca/session_api/add_students_request.h"
#include "chromeos/ash/components/boca/session_api/constants.h"
#include "chromeos/ash/components/boca/session_api/create_session_request.h"
#include "chromeos/ash/components/boca/session_api/get_session_request.h"
#include "chromeos/ash/components/boca/session_api/join_session_request.h"
#include "chromeos/ash/components/boca/session_api/remove_student_request.h"
#include "chromeos/ash/components/boca/session_api/renotify_student_request.h"
#include "chromeos/ash/components/boca/session_api/update_session_config_request.h"
#include "chromeos/ash/components/boca/session_api/update_session_request.h"
#include "chromeos/ash/components/boca/session_api/update_student_activities_request.h"
#include "chromeos/ash/components/boca/session_api/upload_token_request.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "google_apis/common/auth_service.h"
#include "google_apis/common/request_sender.h"
#include "student_heartbeat_request.h"
namespace ash::boca {
SessionClientImpl::SessionClientImpl() : sender_(CreateRequestSender()) {}
SessionClientImpl::SessionClientImpl(
std::unique_ptr<google_apis::RequestSender> sender)
: sender_(std::move(sender)) {}
SessionClientImpl::~SessionClientImpl() = default;
std::unique_ptr<google_apis::RequestSender>
SessionClientImpl::CreateRequestSender() {
std::vector<std::string> scopes = {kSchoolToolsAuthScope};
auto url_loader_factory = BocaAppClient::Get()->GetURLLoaderFactory();
auto* identity_manager = BocaAppClient::Get()->GetIdentityManager();
auto auth_service = std::make_unique<google_apis::AuthService>(
identity_manager,
identity_manager->GetPrimaryAccountId(signin::ConsentLevel::kSignin),
url_loader_factory, scopes);
return std::make_unique<google_apis::RequestSender>(
std::move(auth_service), url_loader_factory,
base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(),
/* `USER_VISIBLE` is because the requested/returned data is visible
to the user on Web UI surfaces. */
base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}),
/*custom_user_agent=*/"", kTrafficAnnotation);
}
void SessionClientImpl::CreateSession(
std::unique_ptr<CreateSessionRequest> request) {
sender_->StartRequestWithAuthRetry(std::move(request));
}
void SessionClientImpl::GetSession(std::unique_ptr<GetSessionRequest> request,
bool can_skip_duplicate_request) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (features::IsBocaSequentialSessionLoadEnabled()) {
if (has_blocking_get_session_request_) {
if (!can_skip_duplicate_request ||
pending_get_session_requests_.size() == 0) {
pending_get_session_requests_.push(std::move(request));
}
return;
}
request->set_callback(
base::BindOnce(&SessionClientImpl::OnGetSessionCompleted,
weak_ptr_factory_.GetWeakPtr(), request->callback()));
has_blocking_get_session_request_ = true;
}
sender_->StartRequestWithAuthRetry(std::move(request));
}
void SessionClientImpl::UploadToken(
std::unique_ptr<UploadTokenRequest> request) {
sender_->StartRequestWithAuthRetry(std::move(request));
}
void SessionClientImpl::UpdateSession(
std::unique_ptr<UpdateSessionRequest> request) {
sender_->StartRequestWithAuthRetry(std::move(request));
}
void SessionClientImpl::UpdateSessionConfig(
std::unique_ptr<UpdateSessionConfigRequest> request) {
sender_->StartRequestWithAuthRetry(std::move(request));
}
void SessionClientImpl::UpdateStudentActivity(
std::unique_ptr<UpdateStudentActivitiesRequest> request) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (features::IsBocaSequentialInsertActivityEnabled()) {
if (has_blocking_update_activity_request_) {
pending_update_student_activity_request_ = std::move(request);
return;
}
request->set_callback(
base::BindOnce(&SessionClientImpl::OnInsertStudentActivityCompleted,
weak_ptr_factory_.GetWeakPtr(), request->callback()));
has_blocking_update_activity_request_ = true;
}
sender_->StartRequestWithAuthRetry(std::move(request));
}
void SessionClientImpl::RemoveStudent(
std::unique_ptr<RemoveStudentRequest> request) {
sender_->StartRequestWithAuthRetry(std::move(request));
}
void SessionClientImpl::AddStudents(
std::unique_ptr<AddStudentsRequest> request) {
sender_->StartRequestWithAuthRetry(std::move(request));
}
void SessionClientImpl::JoinSession(
std::unique_ptr<JoinSessionRequest> request) {
sender_->StartRequestWithAuthRetry(std::move(request));
}
void SessionClientImpl::StudentHeartbeat(
std::unique_ptr<StudentHeartbeatRequest> request) {
sender_->StartRequestWithAuthRetry(std::move(request));
}
void SessionClientImpl::RenotifyStudent(
std::unique_ptr<RenotifyStudentRequest> request) {
sender_->StartRequestWithAuthRetry(std::move(request));
}
void SessionClientImpl::OnInsertStudentActivityCompleted(
UpdateStudentActivitiesCallback callback,
base::expected<bool, google_apis::ApiErrorCode> result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
has_blocking_update_activity_request_ = false;
std::move(callback).Run(std::move(result));
if (!pending_update_student_activity_request_) {
return;
}
auto request = std::move(pending_update_student_activity_request_);
request->set_callback(
base::BindOnce(&SessionClientImpl::OnInsertStudentActivityCompleted,
weak_ptr_factory_.GetWeakPtr(), request->callback()));
sender_->StartRequestWithAuthRetry(std::move(request));
}
void SessionClientImpl::OnGetSessionCompleted(
GetSessionCallback callback,
base::expected<std::unique_ptr<::boca::Session>, google_apis::ApiErrorCode>
result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
has_blocking_get_session_request_ = false;
std::move(callback).Run(std::move(result));
if (pending_get_session_requests_.empty()) {
return;
}
auto request = std::move(pending_get_session_requests_.front());
pending_get_session_requests_.pop();
request->set_callback(
base::BindOnce(&SessionClientImpl::OnGetSessionCompleted,
weak_ptr_factory_.GetWeakPtr(), request->callback()));
sender_->StartRequestWithAuthRetry(std::move(request));
}
} // namespace ash::boca
|