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
|
// Copyright 2021 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/ash/calendar/calendar_client_impl.h"
#include <string>
#include <utility>
#include "ash/constants/ash_pref_names.h"
#include "ash/constants/web_app_id_constants.h"
#include "ash/glanceables/glanceables_metrics.h"
#include "base/containers/contains.h"
#include "base/functional/callback_helpers.h"
#include "base/time/time.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/ash/calendar/calendar_keyed_service.h"
#include "chrome/browser/ash/calendar/calendar_keyed_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/policy/content/policy_blocklist_service.h"
#include "components/policy/core/browser/url_blocklist_manager.h"
#include "components/prefs/pref_service.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/services/app_service/public/cpp/app_update.h"
#include "google_apis/common/api_error_codes.h"
#include "url/gurl.h"
namespace ash {
namespace {
constexpr char kCalendarUrl[] = "https://calendar.google.com/";
} // namespace
CalendarClientImpl::CalendarClientImpl(Profile* profile) : profile_(profile) {}
CalendarClientImpl::~CalendarClientImpl() = default;
bool CalendarClientImpl::IsDisabledByAdmin() const {
// 1) Check the Calendar pref.
const auto* const pref_service = profile_->GetPrefs();
if (!pref_service ||
!pref_service->GetBoolean(prefs::kCalendarIntegrationEnabled) ||
!base::Contains(pref_service->GetList(
prefs::kContextualGoogleIntegrationsConfiguration),
prefs::kGoogleCalendarIntegrationName)) {
RecordContextualGoogleIntegrationStatus(
prefs::kGoogleCalendarIntegrationName,
ContextualGoogleIntegrationStatus::kDisabledByPolicy);
return true;
}
// 2) Check if the Calendar app is disabled by policy.
if (!apps::AppServiceProxyFactory::IsAppServiceAvailableForProfile(
profile_)) {
return true;
}
auto calendar_app_readiness = apps::Readiness::kUnknown;
apps::AppServiceProxyFactory::GetForProfile(profile_)
->AppRegistryCache()
.ForOneApp(ash::kGoogleCalendarAppId,
[&calendar_app_readiness](const apps::AppUpdate& update) {
calendar_app_readiness = update.Readiness();
});
if (calendar_app_readiness == apps::Readiness::kDisabledByPolicy) {
RecordContextualGoogleIntegrationStatus(
prefs::kGoogleCalendarIntegrationName,
ContextualGoogleIntegrationStatus::kDisabledByAppBlock);
return true;
}
// 3) Check if the Calendar URL is blocked by policy.
const auto* const policy_blocklist_service =
PolicyBlocklistFactory::GetForBrowserContext(profile_);
if (!policy_blocklist_service ||
policy_blocklist_service->GetURLBlocklistState(GURL(kCalendarUrl)) ==
policy::URLBlocklist::URLBlocklistState::URL_IN_BLOCKLIST) {
RecordContextualGoogleIntegrationStatus(
prefs::kGoogleCalendarIntegrationName,
ContextualGoogleIntegrationStatus::kDisabledByUrlBlock);
return true;
}
RecordContextualGoogleIntegrationStatus(
prefs::kGoogleCalendarIntegrationName,
ContextualGoogleIntegrationStatus::kEnabled);
return false;
}
base::OnceClosure CalendarClientImpl::GetCalendarList(
google_apis::calendar::CalendarListCallback callback) {
if (IsDisabledByAdmin()) {
std::move(callback).Run(google_apis::OTHER_ERROR, /*calendars=*/nullptr);
return base::DoNothing();
}
CalendarKeyedService* service =
CalendarKeyedServiceFactory::GetInstance()->GetService(profile_);
// For non-gaia users this `service` is not set.
if (service) {
return service->GetCalendarList(std::move(callback));
}
std::move(callback).Run(google_apis::OTHER_ERROR, /*calendars=*/nullptr);
return base::DoNothing();
}
base::OnceClosure CalendarClientImpl::GetEventList(
google_apis::calendar::CalendarEventListCallback callback,
const base::Time start_time,
const base::Time end_time) {
if (IsDisabledByAdmin()) {
std::move(callback).Run(google_apis::OTHER_ERROR, /*events=*/nullptr);
return base::DoNothing();
}
CalendarKeyedService* service =
CalendarKeyedServiceFactory::GetInstance()->GetService(profile_);
// For non-gaia users this `service` is not set.
if (service)
return service->GetEventList(std::move(callback), start_time, end_time);
std::move(callback).Run(google_apis::OTHER_ERROR, /*events=*/nullptr);
return base::DoNothing();
}
base::OnceClosure CalendarClientImpl::GetEventList(
google_apis::calendar::CalendarEventListCallback callback,
const base::Time start_time,
const base::Time end_time,
const std::string& calendar_id,
const std::string& calendar_color_id) {
if (IsDisabledByAdmin()) {
std::move(callback).Run(google_apis::OTHER_ERROR, /*events=*/nullptr);
return base::DoNothing();
}
CalendarKeyedService* service =
CalendarKeyedServiceFactory::GetInstance()->GetService(profile_);
// For non-gaia users this `service` is not set.
if (service) {
return service->GetEventList(std::move(callback), start_time, end_time,
calendar_id, calendar_color_id);
}
std::move(callback).Run(google_apis::OTHER_ERROR, /*events=*/nullptr);
return base::DoNothing();
}
} // namespace ash
|