blob: 008cff225154a83ecdabc384acbc3b83c17f7937 [file] [log] [blame]
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromecast/browser/cast_feature_update_observer.h"
#include "base/bind.h"
#include "base/check.h"
#include "chromecast/base/pref_names.h"
#include "chromecast/common/mojom/constants.mojom.h"
#include "chromecast/external_mojo/external_service_support/external_connector.h"
#include "components/prefs/pref_service.h"
namespace chromecast {
CastFeatureUpdateObserver::CastFeatureUpdateObserver(
external_service_support::ExternalConnector* connector,
PrefService* pref_service)
: connector_(connector), pref_service_(pref_service) {
DCHECK(connector_);
DCHECK(pref_service_);
BindFeatureUpdateService();
}
CastFeatureUpdateObserver::~CastFeatureUpdateObserver() = default;
void CastFeatureUpdateObserver::BindFeatureUpdateService() {
feature_update_service_.reset();
receiver_.reset();
connector_->BindInterface(
mojom::kChromecastServiceName,
feature_update_service_.BindNewPipeAndPassReceiver());
feature_update_service_->RegisterFeatureUpdateObserver(
receiver_.BindNewPipeAndPassRemote());
// Right now we are in the process of making the `cast_service` manage the
// lifecycle of `cast_browser`. Until that is done, `cast_service` has a
// shorter lifecycle than `cast_browser`, so we need to handle disconnects
// here.
// TODO(crbug/1285360): remove once process lifecycles are inverted.
receiver_.set_disconnect_handler(
base::BindOnce(&CastFeatureUpdateObserver::BindFeatureUpdateService,
base::Unretained(this)));
}
void CastFeatureUpdateObserver::OnFeaturesUpdated(base::Value features) {
pref_service_->Set(prefs::kLatestDCSFeatures, std::move(features));
pref_service_->CommitPendingWrite();
}
} // namespace chromecast