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

ash / system / time / calendar_list_model.cc [blame]

// 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 "ash/system/time/calendar_list_model.h"

#include <iterator>
#include <optional>
#include <string>

#include "ash/ash_export.h"
#include "ash/calendar/calendar_client.h"
#include "ash/calendar/calendar_controller.h"
#include "ash/public/cpp/session/session_observer.h"
#include "ash/shell.h"
#include "ash/system/time/calendar_event_fetch_types.h"
#include "ash/system/time/calendar_metrics.h"
#include "ash/system/time/calendar_utils.h"
#include "base/check_is_test.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "google_apis/calendar/calendar_api_response_types.h"
#include "google_apis/common/api_error_codes.h"

namespace {

// Compares two calendars by primary field first, then alphabetically by
// summary (i.e. Calendar name) if neither calendar is primary.
bool CompareByPrimaryAndSummary(
    google_apis::calendar::SingleCalendar calendar_one,
    google_apis::calendar::SingleCalendar calendar_two) {
  if (calendar_one.primary()) {
    return true;
  }
  if (calendar_two.primary()) {
    return false;
  }
  return base::CompareCaseInsensitiveASCII(calendar_one.summary(),
                                           calendar_two.summary()) < 0;
}

void FilterForSelectedCalendars(ash::CalendarList& calendars) {
  calendars.remove_if([](google_apis::calendar::SingleCalendar calendar) {
    return !calendar.selected();
  });
}

}  // namespace

namespace ash {

CalendarListModel::CalendarListModel()
    : timeout_(nullptr), session_observer_(this) {}

CalendarListModel::~CalendarListModel() = default;

void CalendarListModel::OnSessionStateChanged(
    session_manager::SessionState state) {
  ClearCacheAndCancelFetch();
}

void CalendarListModel::OnActiveUserSessionChanged(
    const AccountId& account_id) {
  ClearCacheAndCancelFetch();
}

void CalendarListModel::AddObserver(Observer* observer) {
  if (observer) {
    observers_.AddObserver(observer);
  }
}

void CalendarListModel::RemoveObserver(Observer* observer) {
  if (observer) {
    observers_.RemoveObserver(observer);
  }
}

void CalendarListModel::FetchCalendars() {
  if (!calendar_utils::ShouldFetchCalendarData()) {
    return;
  }

  CancelFetch();

  fetch_in_progress_ = true;
  fetch_start_time_ = base::TimeTicks::Now();

  CalendarClient* client = Shell::Get()->calendar_controller()->GetClient();

  // Bail out early if there is no CalendarClient. This will be the case in
  // most unit tests.
  if (!client) {
    CHECK_IS_TEST();
    return;
  }

  cancel_closure_ = client->GetCalendarList(base::BindOnce(
      &CalendarListModel::OnCalendarListFetched, weak_factory_.GetWeakPtr()));
  CHECK(cancel_closure_);

  timeout_.Start(FROM_HERE, calendar_utils::kCalendarDataFetchTimeout,
                 base::BindOnce(&CalendarListModel::OnCalendarListFetchTimeout,
                                weak_factory_.GetWeakPtr()));
}

void CalendarListModel::CancelFetch() {
  timeout_.Stop();
  if (cancel_closure_) {
    std::move(cancel_closure_).Run();
  }
}

CalendarList CalendarListModel::GetCachedCalendarList() {
  // Since the calendar list is kept until it is replaced during a re-fetch
  // (or removed during a session change), we check is_cached_ before returning
  // the list.
  if (get_is_cached()) {
    return calendar_list_;
  }
  CalendarList empty_calendar_list;
  return empty_calendar_list;
}

void CalendarListModel::OnCalendarListFetched(
    google_apis::ApiErrorCode error,
    std::unique_ptr<google_apis::calendar::CalendarList> calendars) {
  // Cancel timeout timer unless it was previously stopped on Cancel.
  if (error != google_apis::CANCELLED) {
    timeout_.Stop();
  }

  calendar_metrics::RecordCalendarListFetchDuration(base::TimeTicks::Now() -
                                                    fetch_start_time_);
  calendar_metrics::RecordCalendarListFetchErrorCode(error);
  calendar_metrics::RecordCalendarListFetchTimeout(false);

  if (error == google_apis::HTTP_SUCCESS) {
    if (calendars && !calendars->items().empty()) {
      ClearCachedCalendarList();
      for (auto& calendar : calendars->items()) {
        calendar_list_.push_back(*calendar.get());
      }
      FilterForSelectedCalendars(calendar_list_);

      calendar_metrics::RecordTotalSelectedCalendars(calendar_list_.size());

      // The ordering of the calendar list is not always consistent between
      // API calls, so calendar lists are sorted to maintain consistency of
      // which events are shown. The sorter also moves the primary calendar
      // to the top of the list.
      calendar_list_.sort(CompareByPrimaryAndSummary);
      if (calendar_list_.size() > calendar_utils::kMultipleCalendarsLimit) {
        calendar_list_.resize(calendar_utils::kMultipleCalendarsLimit);
      }
      is_cached_ = true;
    }
  }
  // In case of error, we fallback to a previously cached calendar list if it
  // exists. So we still notify observers of completion in all cases.
  fetch_in_progress_ = false;
  for (auto& observer : observers_) {
    observer.OnCalendarListFetchComplete();
  }
}

void CalendarListModel::OnCalendarListFetchTimeout() {
  calendar_metrics::RecordCalendarListFetchTimeout(true);

  fetch_in_progress_ = false;
  for (auto& observer : observers_) {
    observer.OnCalendarListFetchComplete();
  }
}

void CalendarListModel::ClearCachedCalendarList() {
  calendar_list_.clear();
  is_cached_ = false;
}

void CalendarListModel::ClearCacheAndCancelFetch() {
  CancelFetch();
  fetch_in_progress_ = false;
  ClearCachedCalendarList();
}

}  // namespace ash