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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_ASH_GLANCEABLES_GLANCEABLES_CLASSROOM_CLIENT_IMPL_H_
#define CHROME_BROWSER_UI_ASH_GLANCEABLES_GLANCEABLES_CLASSROOM_CLIENT_IMPL_H_
#include <list>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "ash/glanceables/classroom/glanceables_classroom_client.h"
#include "ash/glanceables/classroom/glanceables_classroom_types.h"
#include "base/containers/flat_map.h"
#include "base/functional/callback_forward.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/types/expected.h"
#include "chrome/browser/ui/ash/glanceables/glanceables_classroom_course_work_item.h"
#include "google_apis/common/api_error_codes.h"
#include "google_apis/common/request_sender.h"
class Profile;
namespace base {
class Clock;
class Time;
} // namespace base
namespace google_apis::classroom {
class Courses;
class CourseWork;
class StudentSubmissions;
} // namespace google_apis::classroom
namespace net {
struct NetworkTrafficAnnotationTag;
} // namespace net
namespace ash {
// Provides implementation for `GlanceablesClassroomClient`. Responsible for
// communication with Google Classroom API.
class GlanceablesClassroomClientImpl : public GlanceablesClassroomClient {
public:
// Provides an instance of `google_apis::RequestSender` for the client.
using CreateRequestSenderCallback =
base::RepeatingCallback<std::unique_ptr<google_apis::RequestSender>(
const std::vector<std::string>& scopes,
const net::NetworkTrafficAnnotationTag& traffic_annotation_tag)>;
using SortComparator = base::RepeatingCallback<bool(
const GlanceablesClassroomCourseWorkItem* lhs,
const GlanceablesClassroomCourseWorkItem* rhs)>;
GlanceablesClassroomClientImpl(
Profile* profile,
base::Clock* clock,
const CreateRequestSenderCallback& create_request_sender_callback);
GlanceablesClassroomClientImpl(const GlanceablesClassroomClientImpl&) =
delete;
GlanceablesClassroomClientImpl& operator=(
const GlanceablesClassroomClientImpl&) = delete;
~GlanceablesClassroomClientImpl() override;
// GlanceablesClassroomClient:
bool IsDisabledByAdmin() const override;
void IsStudentRoleActive(IsRoleEnabledCallback callback) override;
void GetCompletedStudentAssignments(GetAssignmentsCallback callback) override;
void GetStudentAssignmentsWithApproachingDueDate(
GetAssignmentsCallback callback) override;
void GetStudentAssignmentsWithMissedDueDate(
GetAssignmentsCallback callback) override;
void GetStudentAssignmentsWithoutDueDate(
GetAssignmentsCallback callback) override;
void OnGlanceablesBubbleClosed() override;
private:
FRIEND_TEST_ALL_PREFIXES(GlanceablesClassroomClientImplTest, FetchCourses);
FRIEND_TEST_ALL_PREFIXES(GlanceablesClassroomClientImplTest,
FetchCoursesOnHttpError);
FRIEND_TEST_ALL_PREFIXES(GlanceablesClassroomClientImplTest,
FetchCoursesMultiplePages);
FRIEND_TEST_ALL_PREFIXES(GlanceablesClassroomClientImplTest, FetchCourseWork);
FRIEND_TEST_ALL_PREFIXES(GlanceablesClassroomClientImplTest,
FetchCourseWorkAndSubmissions);
FRIEND_TEST_ALL_PREFIXES(GlanceablesClassroomClientImplTest,
FetchCourseWorkOnHttpError);
FRIEND_TEST_ALL_PREFIXES(GlanceablesClassroomClientImplTest,
FetchCourseWorkMultiplePages);
FRIEND_TEST_ALL_PREFIXES(GlanceablesClassroomClientImplTest,
FetchCourseWorkAndSubmissionsMultiplePages);
FRIEND_TEST_ALL_PREFIXES(GlanceablesClassroomClientImplTest,
FetchStudentSubmissions);
FRIEND_TEST_ALL_PREFIXES(GlanceablesClassroomClientImplTest,
FetchStudentSubmissionsOnHttpError);
FRIEND_TEST_ALL_PREFIXES(GlanceablesClassroomClientImplTest,
FetchStudentSubmissionsMultiplePages);
FRIEND_TEST_ALL_PREFIXES(GlanceablesClassroomClientImplTest,
FetchCourseWorkAfterStudentSubmissions);
// Done callback for fetching all courses for student or teacher roles.
using CourseList = std::vector<std::unique_ptr<GlanceablesClassroomCourse>>;
using FetchCoursesCallback =
base::OnceCallback<void(bool success, const CourseList& courses)>;
using CourseWorkInfo =
base::flat_map<std::string, GlanceablesClassroomCourseWorkItem>;
using CourseWorkPerCourse = base::flat_map<std::string, CourseWorkInfo>;
enum class FetchStatus {
// The data needs to be fetched - either because it was never fetched, or
// glanceables bubble was closed since the data was last fetched.
kNotFetched,
// The data fetch is in progress.
kFetching,
// The data fetch is in progress, but the glanceables bubble was closed
// before the fetch finished.
kFetchingInvalidated,
// The data has been fetched.
kFetched
};
// Flavours of course work data handled by the client.
enum class CourseWorkType { kStudent, kTeacher };
// Tracks a course list state - the latest fetched list, the fetch status, and
// the list of callbacks waiting for the list to be fetched.
class CourseListState {
public:
explicit CourseListState(base::Clock* clock);
CourseListState(const CourseListState&) = delete;
CourseListState& operator=(const CourseListState&) = delete;
~CourseListState();
// If the course list is fetched, it runs the `callback` immediately.
// Otherwise, it enqueues the callback to be run when the list gets fetched.
// It updates the fetch status to indicate that the list is to be fetched.
// Returns whether the client should initiate course list request.
bool RunOrEnqueueCallbackAndUpdateFetchStatus(
FetchCoursesCallback callback);
// Called by the `GlanceablesClassroomClientImpl` to update the course list
// state when a course list fetch request completes.
// It updates the cached course list, and updates the fetch state, and runs
// any pending course list callbacks.
// `fetched_courses` - the list of fetched courses, nullptr if the course
// list fetch request failed.
void FinalizeFetch(std::unique_ptr<CourseList> fetched_courses);
const CourseList& courses() const { return courses_; }
private:
// Runs pending callbacks in `callbacks_` and passes them the latest cached
// course list.
void RunCallbacks(bool success);
CourseList courses_;
FetchStatus fetch_status_ = FetchStatus::kNotFetched;
const raw_ptr<base::Clock> clock_;
base::Time last_successful_fetch_time_;
std::vector<FetchCoursesCallback> callbacks_;
};
// Wrapper around course work fetch callback that tracks the number of pending
// course work page requests.
// While individual pages need to be fetched serially, course work fetch may
// require fetching student submissions for course work in each of the course
// work pages. In that case, a page request is deemed complete when all
// required student submissions are fetched. Fetching student submissions for
// a course work page does not block fetch of the next course work page, which
// means that handling of different course work pages may overlap.
class CourseWorkRequest {
public:
explicit CourseWorkRequest(base::OnceClosure callback);
CourseWorkRequest(const CourseWorkRequest&) = delete;
CourseWorkRequest& operator=(const CourseWorkRequest&) = delete;
~CourseWorkRequest();
// Increases the count of pending course work page requests - should be
// called when a fetch for a course work page is initiated.
void IncrementPendingPageCount();
// Decrease the count of pending course work page requests - should be
// called when a fetch for a course work page, including student submissions
// data (when required) completes.
void DecrementPendingPageCount();
// If no more page tokens are pending, runs the `callback_`.
// Returns whether the callback was run. If the callback is run, the object
// can be discarded. and `RespondIfComplete()` should not be called any
// longer.
bool RespondIfComplete();
private:
base::OnceClosure callback_;
int pending_page_requests_ = 0;
};
// Updates the `*fetch_status` in response to the glanceables bubble closing -
// it updates the fetch status to indicate that the data can be refetched when
// requested again.
static void InvalidateFetchStatus(FetchStatus* fetch_status);
// Whether student submissions should be fetched per course work item, or per
// course.
bool ShouldFetchSubmissionsPerCourseWork(
CourseWorkType course_work_type) const;
// Gets a reference to course work data for the provided course work type.
CourseWorkPerCourse& GetCourseWork(CourseWorkType type);
// Called when an API call to get part of course work data for the course work
// type fails.
void SetCourseWorkFetchHadFailure(CourseWorkType type);
// Fetches all courses for student and teacher roles and invokes `callback`
// when done.
void FetchStudentCourses(FetchCoursesCallback callback);
// Fetches all course work items for the specified `course_id` and invokes
// `callback` when done. The course work information is saved in the course
// work map for `course_work_type` (either `student_course_work_` or
// `teacher_course_work_`).
void FetchCourseWork(const std::string& course_id,
CourseWorkType course_work_type,
base::OnceClosure callback);
// Fetches all student submissions for the specified `course_id` and
// `course_work_id` and invokes `callback` when done.
// To requests student submissions for all course work item in the course,
// pass in `course_work_id` value "-".
// The fetched student submissions get added to the course work map for
// `course_work_type` (either `student_course_work_` or
// `teacher_course_work_`).
void FetchStudentSubmissions(const std::string& course_id,
const std::string& course_work_id,
CourseWorkType course_work_type,
base::OnceClosure callback);
// Delays executing `callback` until all student data are fetched.
void InvokeOnceStudentDataFetched(base::OnceClosure callback);
// Fetches one page of courses.
// `page_token` - token specifying the result page to return, comes
// from the previous fetch request. Use an empty string
// to fetch the first page.
// `fetched_courses` - the container to which course items returned during
// course list fetch are saved. This container will be
// passed to `callback` once all items have been
// fetched.
void FetchCoursesPage(const std::string& page_token,
std::unique_ptr<CourseList> fetched_courses);
// Callback for `FetchCoursesPage()`. If `next_page_token()` in the `result`
// is not empty - calls another `FetchCoursesPage()`, otherwise runs done
// `callback`.
void OnCoursesPageFetched(
std::unique_ptr<CourseList> fetched_courses,
const base::Time& request_start_time,
base::expected<std::unique_ptr<google_apis::classroom::Courses>,
google_apis::ApiErrorCode> result);
// Callback for `FetchStudentCourses()`. Triggers fetching course work and
// student submissions for fetched courses and invokes
// `on_course_work_and_student_submissions_fetched` when done.
// `course_work_type` indicates the flavour of course work information that's
// being fetched, and is used to determine the course work map where the
// course work and student submissions whose fetch gets requested should be
// saved.
void OnCoursesFetched(
CourseWorkType course_work_type,
base::OnceClosure on_course_work_and_student_submissions_fetched,
bool success,
const CourseList& target_course_list);
// Fetches one page of course work items.
// `request_id` - the ID for the course work request that's being
// handled. It can be used to get the associated
// `CourseWorkRequest` from `course_work_requests_`.
// `course_id` - identifier of the course.
// `page_token` - token specifying the result page to return, comes from
// the previous fetch request. Use an empty string to
// fetch the first page.
// `page_number` - 1-based page number of this fetch request. Used for
// UMA to track the total number of pages needed to
// fetch.
// `course_work_type` - The flavour of course work information being fetched.
// Determines the course work map where course work
// information gets saved, and whether student
// submissions need to be fetched per course work item.
void FetchCourseWorkPage(int request_id,
const std::string& course_id,
const std::string& page_token,
int page_number,
CourseWorkType course_work_type);
// Callback for `FetchCourseWorkPage()`. If `next_page_token()` in the
// `result` is not empty - calls another `FetchCourseWorkPage()`, otherwise
// runs done `callback`.
void OnCourseWorkPageFetched(
int request_id,
const std::string& course_id,
CourseWorkType course_work_type,
const base::Time& request_start_time,
int page_number,
base::expected<std::unique_ptr<google_apis::classroom::CourseWork>,
google_apis::ApiErrorCode> result);
// Fetches one page of student submissions.
// `course_id` - identifier of the course.
// `course_work_id` - identifier of the course work item. May be "-" to
// request student submissions for all course work in the
// course.
// `page_token` - token specifying the result page to return, comes from
// the previous fetch request. Use an empty string to
// fetch the first page.
// `page_number` - 1-based page number of this fetch request. Used for
// UMA to track the total number of pages needed to
// fetch.
// `course_work_type` - The flavour of course work information being fetched.
// Determines the course work map where student
// submissions information gets saved.
// `callback` - a callback that runs when all student submissions in a
// course have been fetched. This may require multiple
// fetch requests, in this case `callback` gets called
// when the final request completes.
void FetchStudentSubmissionsPage(const std::string& course_id,
const std::string& course_work_id,
const std::string& page_token,
int page_number,
CourseWorkType course_work_type,
base::OnceClosure callback);
// Callback for `FetchStudentSubmissionsPage()`. If `next_page_token()` in the
// `result` is not empty - calls another `FetchStudentSubmissionsPage()`,
// otherwise runs done `callback`.
void OnStudentSubmissionsPageFetched(
const std::string& course_id,
const std::string& course_work_id,
CourseWorkType course_work_type,
const base::Time& request_start_time,
int page_number,
base::OnceClosure callback,
base::expected<
std::unique_ptr<google_apis::classroom::StudentSubmissions>,
google_apis::ApiErrorCode> result);
// Callback for requests to fetch student submissions for all course work
// items within a course work list page. The student submissions fetch is a
// subtask of a course work request, which is identified by `request_id`.
// When processing a page in course work list response, student submissions
// may get requested for each course work item - this callback is called
// when all requested student submission lists have been fetched.
void OnCourseWorkSubmissionsFetched(int request_id,
const std::string& course_id);
// Invokes all pending callbacks from `callbacks_waiting_for_student_data_`
// once all student data are fetched (courses + course work + student
// submissions).
void OnStudentDataFetched(const base::Time& sequence_start_time);
// Selects student assignments that satisfy both filtering predicates below.
// `due_predicate` - returns `true` if passed due date/time
// satisfies filtering requirements.
// `submission_state_predicate` - returns `true` if passed submission state
// satisfies filtering requirements.
// `sort_comparator` - the function used when comparing two
// assignments for sorting.
// `callback` - invoked with filtered results.
void GetFilteredStudentAssignments(
base::RepeatingCallback<bool(const std::optional<base::Time>&)>
due_predicate,
base::RepeatingCallback<bool(GlanceablesClassroomStudentSubmissionState)>
submission_state_predicate,
SortComparator sort_comparator,
GetAssignmentsCallback callback);
// Removes all invalid course work items from `course_work` for courses in
// the course list.
void PruneInvalidCourseWork(const CourseList& courses,
CourseWorkPerCourse& course_work);
// Returns lazily initialized `request_sender_`.
google_apis::RequestSender* GetRequestSender();
// The profile for which this instance was created.
const raw_ptr<Profile> profile_;
// Clock to be used to retrieve current time - expected to be default clock in
// production.
const raw_ptr<base::Clock> clock_;
// Callback passed from `GlanceablesKeyedService` that creates
// `request_sender_`.
const CreateRequestSenderCallback create_request_sender_callback_;
// Helper class that sends requests, handles retries and authentication.
std::unique_ptr<google_apis::RequestSender> request_sender_;
// Available courses for student role.
CourseListState student_courses_;
// All course work information grouped by course id.
CourseWorkPerCourse student_course_work_;
CourseWorkPerCourse teacher_course_work_;
// Fetch status of all student data.
FetchStatus student_data_fetch_status_ = FetchStatus::kNotFetched;
// Whether any of API requests made to fetch student data failed, indicating
// that student data may not be fully fresh.
bool student_data_fetch_had_failure_ = false;
// Pending callbacks awaiting all student data.
std::list<base::OnceClosure> callbacks_waiting_for_student_data_;
// Fetch status of all teacher data.
FetchStatus teacher_data_fetch_status_ = FetchStatus::kNotFetched;
// Whether any of API requests made to fetch teacher data failed, indicating
// that teacher data may not be fully fresh.
bool teacher_data_fetch_had_failure_ = false;
// The next available course work fetch request ID. The IDs will increase
// monotonically with each new request.
int next_course_work_request_id_ = 0;
// In progress course work requests, mapped by the course work request ID.
base::flat_map<int, std::unique_ptr<CourseWorkRequest>> course_work_requests_;
base::WeakPtrFactory<GlanceablesClassroomClientImpl> weak_factory_{this};
};
} // namespace ash
#endif // CHROME_BROWSER_UI_ASH_GLANCEABLES_GLANCEABLES_CLASSROOM_CLIENT_IMPL_H_
|