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
|
// Copyright 2018 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/android_sms/connection_manager.h"
#include <utility>
#include "chrome/browser/ash/android_sms/android_sms_urls.h"
#include "chrome/browser/ash/android_sms/connection_establisher.h"
#include "chrome/browser/profiles/profile.h"
#include "chromeos/ash/components/multidevice/logging/logging.h"
#include "components/session_manager/core/session_manager.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/service_worker_context.h"
#include "content/public/browser/storage_partition.h"
namespace ash {
namespace android_sms {
ConnectionManager::ServiceWorkerProvider::ServiceWorkerProvider() = default;
ConnectionManager::ServiceWorkerProvider::~ServiceWorkerProvider() = default;
content::ServiceWorkerContext* ConnectionManager::ServiceWorkerProvider::Get(
const GURL& url,
Profile* profile) {
return profile->GetDefaultStoragePartition()->GetServiceWorkerContext();
}
ConnectionManager::ConnectionManager(
std::unique_ptr<ConnectionEstablisher> connection_establisher,
Profile* profile,
AndroidSmsAppManager* android_sms_app_manager,
multidevice_setup::MultiDeviceSetupClient* multidevice_setup_client)
: ConnectionManager(std::move(connection_establisher),
profile,
android_sms_app_manager,
multidevice_setup_client,
std::make_unique<ServiceWorkerProvider>()) {}
ConnectionManager::ConnectionManager(
std::unique_ptr<ConnectionEstablisher> connection_establisher,
Profile* profile,
AndroidSmsAppManager* android_sms_app_manager,
multidevice_setup::MultiDeviceSetupClient* multidevice_setup_client,
std::unique_ptr<ServiceWorkerProvider> service_worker_provider)
: connection_establisher_(std::move(connection_establisher)),
profile_(profile),
android_sms_app_manager_(android_sms_app_manager),
multidevice_setup_client_(multidevice_setup_client),
service_worker_provider_(std::move(service_worker_provider)) {
android_sms_app_manager_->AddObserver(this);
multidevice_setup_client_->AddObserver(this);
UpdateConnectionStatus();
}
ConnectionManager::~ConnectionManager() {
android_sms_app_manager_->RemoveObserver(this);
multidevice_setup_client_->RemoveObserver(this);
if (GetCurrentServiceWorkerContext())
GetCurrentServiceWorkerContext()->RemoveObserver(this);
}
void ConnectionManager::StartConnection() {
if (!enabled_pwa_url_) {
return;
}
PA_LOG(INFO) << "ConnectionManager::StartConnection(): Establishing "
<< "connection to PWA at " << *enabled_pwa_url_ << ".";
connection_establisher_->EstablishConnection(
*enabled_pwa_url_,
ConnectionEstablisher::ConnectionMode::kStartConnection,
GetCurrentServiceWorkerContext());
}
void ConnectionManager::OnVersionActivated(int64_t version_id,
const GURL& scope) {
if (!enabled_pwa_url_ || !scope.EqualsIgnoringRef(*enabled_pwa_url_))
return;
prev_active_version_id_ = active_version_id_;
active_version_id_ = version_id;
connection_establisher_->EstablishConnection(
*enabled_pwa_url_,
ConnectionEstablisher::ConnectionMode::kResumeExistingConnection,
GetCurrentServiceWorkerContext());
}
void ConnectionManager::OnVersionRedundant(int64_t version_id,
const GURL& scope) {
if (!enabled_pwa_url_ || !scope.EqualsIgnoringRef(*enabled_pwa_url_))
return;
if (active_version_id_ != version_id)
return;
// If the active version is marked redundant then it cannot handle messages
// anymore, so stop tracking it.
prev_active_version_id_ = active_version_id_;
active_version_id_.reset();
}
void ConnectionManager::OnNoControllees(int64_t version_id, const GURL& scope) {
if (!enabled_pwa_url_ || !scope.EqualsIgnoringRef(*enabled_pwa_url_))
return;
// Set active_version_id_ in case we missed version activated.
// This is unlikely but protects against a case where a Android Messages for
// Web page may have opened before the ConnectionManager is created.
if (!active_version_id_ && prev_active_version_id_ != version_id)
active_version_id_ = version_id;
if (active_version_id_ != version_id)
return;
connection_establisher_->EstablishConnection(
*enabled_pwa_url_,
ConnectionEstablisher::ConnectionMode::kResumeExistingConnection,
GetCurrentServiceWorkerContext());
}
void ConnectionManager::OnInstalledAppUrlChanged() {
UpdateConnectionStatus();
}
void ConnectionManager::OnFeatureStatesChanged(
const multidevice_setup::MultiDeviceSetupClient::FeatureStatesMap&
feature_states_map) {
UpdateConnectionStatus();
}
void ConnectionManager::UpdateConnectionStatus() {
std::optional<GURL> updated_pwa_url =
ConnectionManager::GenerateEnabledPwaUrl();
if (enabled_pwa_url_ == updated_pwa_url)
return;
// If the URL was previously enabled, stop the ServiceWorker. This occurs when
// the feature was disabled and when migrating from the old URL to the new
// one.
if (enabled_pwa_url_) {
PA_LOG(INFO) << "ConnectionManager::UpdateConnectionStatus(): Stopping "
<< "connection to PWA at " << *enabled_pwa_url_ << ".";
connection_establisher_->TearDownConnection(
*enabled_pwa_url_, GetCurrentServiceWorkerContext());
GetCurrentServiceWorkerContext()->RemoveObserver(this);
active_version_id_.reset();
prev_active_version_id_.reset();
}
enabled_pwa_url_ = updated_pwa_url;
if (!enabled_pwa_url_)
return;
GetCurrentServiceWorkerContext()->AddObserver(this);
StartConnection();
}
std::optional<GURL> ConnectionManager::GenerateEnabledPwaUrl() {
const auto it = multidevice_setup_client_->GetFeatureStates().find(
multidevice_setup::mojom::Feature::kMessages);
// If the feature is not enabled, there is no enabled URL.
if (it->second != multidevice_setup::mojom::FeatureState::kEnabledByUser)
return std::nullopt;
// Return the installed app URL if the PWA is installed.
std::optional<GURL> installed_url =
android_sms_app_manager_->GetCurrentAppUrl();
if (installed_url)
return installed_url;
// Otherwise, return the default URL.
return GetAndroidMessagesURL();
}
content::ServiceWorkerContext*
ConnectionManager::GetCurrentServiceWorkerContext() {
if (!enabled_pwa_url_)
return nullptr;
return service_worker_provider_->Get(*enabled_pwa_url_, profile_);
}
void ConnectionManager::SetServiceWorkerProviderForTesting(
std::unique_ptr<ServiceWorkerProvider> service_worker_provider) {
service_worker_provider_ = std::move(service_worker_provider);
}
} // namespace android_sms
} // namespace ash
|