File: birch_calendar_provider.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (155 lines) | stat: -rw-r--r-- 5,379 bytes parent folder | download | duplicates (6)
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
// 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 "chrome/browser/ui/ash/birch/birch_calendar_provider.h"

#include <string>
#include <vector>

#include "ash/birch/birch_item.h"
#include "ash/birch/birch_model.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/time/calendar_utils.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chrome/browser/ui/ash/birch/birch_calendar_fetcher.h"
#include "google_apis/calendar/calendar_api_requests.h"
#include "google_apis/calendar/calendar_api_response_types.h"
#include "google_apis/common/api_error_codes.h"
#include "google_apis/common/auth_service.h"
#include "google_apis/common/request_sender.h"
#include "google_apis/gaia/gaia_constants.h"
#include "ui/base/l10n/l10n_util.h"

namespace ash {

namespace {

BirchCalendarItem::ResponseStatus GetItemResponseStatus(
    const google_apis::calendar::CalendarEvent::ResponseStatus&
        response_status) {
  switch (response_status) {
    case google_apis::calendar::CalendarEvent::ResponseStatus::kUnknown:
    case google_apis::calendar::CalendarEvent::ResponseStatus::kDeclined:
      // Treat an unknown response as a declined birch calendar item.
      return BirchCalendarItem::ResponseStatus::kDeclined;
    case google_apis::calendar::CalendarEvent::ResponseStatus::kAccepted:
      return BirchCalendarItem::ResponseStatus::kAccepted;
    case google_apis::calendar::CalendarEvent::ResponseStatus::kTentative:
      return BirchCalendarItem::ResponseStatus::kTentative;
    case google_apis::calendar::CalendarEvent::ResponseStatus::kNeedsAction:
      return BirchCalendarItem::ResponseStatus::kNeedsAction;
  }
}

}  // namespace

BirchCalendarProvider::BirchCalendarProvider(Profile* profile)
    : profile_(profile) {}

BirchCalendarProvider::~BirchCalendarProvider() = default;

void BirchCalendarProvider::Initialize() {
  fetcher_ = std::make_unique<BirchCalendarFetcher>(profile_);
}

void BirchCalendarProvider::Shutdown() {
  fetcher_->Shutdown();
}

void BirchCalendarProvider::RequestBirchDataFetch() {
  VLOG(1) << "BirchCalendarProvider::RequestBirchDataFetch";
  if (!calendar_utils::ShouldFetchCalendarData()) {
    Shell::Get()->birch_model()->SetCalendarItems({});
    Shell::Get()->birch_model()->SetAttachmentItems({});
    return;
  }

  if (is_fetching_) {
    return;
  }
  is_fetching_ = true;

  // Get all events from 2 hours ago until 1 day in the future.
  base::Time now = base::Time::Now();
  base::Time start_time = now - base::Hours(2);
  base::Time end_time = now + base::Days(1);

  fetcher_->GetCalendarEvents(
      start_time, end_time,
      base::BindOnce(&BirchCalendarProvider::OnEventsFetched,
                     weak_factory_.GetWeakPtr()));
}

void BirchCalendarProvider::OnEventsFetched(
    google_apis::ApiErrorCode error,
    std::unique_ptr<google_apis::calendar::EventList> events) {
  VLOG(1) << "BirchCalendarProvider::OnEventsFetched error " << error
          << " size " << (events ? events->items().size() : -1);
  is_fetching_ = false;
  auto* birch_model = Shell::Get()->birch_model();

  if (error != google_apis::HTTP_SUCCESS) {
    LOG(ERROR) << "Failed to fetch calendar events, error "
               << static_cast<int>(error);
    birch_model->SetCalendarItems({});
    return;
  }

  // There might not be any events.
  if (!events) {
    birch_model->SetCalendarItems({});
    return;
  }

  std::vector<BirchCalendarItem> calendar_items;
  std::vector<BirchAttachmentItem> attachment_items;
  for (const auto& item : events->items()) {
    if (!item) {
      continue;
    }

    BirchCalendarItem::ResponseStatus response_status =
        GetItemResponseStatus(item->self_response_status());
    // Declined events and event attachments should not be shown.
    if (response_status == BirchCalendarItem::ResponseStatus::kDeclined) {
      continue;
    }

    // Calendar events with no summary should say "(No title)" like they do in
    // the web UI.
    std::string summary = item->summary();
    if (summary.empty()) {
      summary = l10n_util::GetStringUTF8(IDS_ASH_BIRCH_CALENDAR_NO_TITLE);
    }

    // Convert the data from google_apis format to birch format.
    BirchCalendarItem birch_item(
        base::UTF8ToUTF16(summary), item->start_time().date_time(),
        item->end_time().date_time(), GURL(item->html_link()),
        item->conference_data_uri(), item->id(), item->all_day_event(),
        response_status);
    calendar_items.push_back(std::move(birch_item));

    // Attachments are stored as separate items.
    for (const auto& attachment : item->attachments()) {
      BirchAttachmentItem birch_attachment(
          base::UTF8ToUTF16(attachment.title()), attachment.file_url(),
          attachment.icon_link(), item->start_time().date_time(),
          item->end_time().date_time(), attachment.file_id());
      attachment_items.push_back(std::move(birch_attachment));
    }
  }
  birch_model->SetCalendarItems(std::move(calendar_items));
  birch_model->SetAttachmentItems(std::move(attachment_items));
}

void BirchCalendarProvider::SetFetcherForTest(
    std::unique_ptr<BirchCalendarFetcher> fetcher) {
  fetcher_ = std::move(fetcher);
}

}  // namespace ash