chromium/components/language/ios/browser/ios_language_detection_tab_helper_observer_bridge.mm

// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "components/language/ios/browser/ios_language_detection_tab_helper_observer_bridge.h"

#include "base/check_op.h"

namespace language {

IOSLanguageDetectionTabHelperObserverBridge::
    IOSLanguageDetectionTabHelperObserverBridge(
        IOSLanguageDetectionTabHelper* tab_helper,
        id<IOSLanguageDetectionTabHelperObserving> owner)
    : tab_helper_(tab_helper), owner_(owner) {
  DCHECK(tab_helper_);
  tab_helper_->AddObserver(this);
}

IOSLanguageDetectionTabHelperObserverBridge::
    ~IOSLanguageDetectionTabHelperObserverBridge() {
  if (tab_helper_) {
    tab_helper_->RemoveObserver(this);
  }
}

void IOSLanguageDetectionTabHelperObserverBridge::OnLanguageDetermined(
    const translate::LanguageDetectionDetails& details) {
  [owner_ iOSLanguageDetectionTabHelper:tab_helper_
                   didDetermineLanguage:details];
}

void IOSLanguageDetectionTabHelperObserverBridge::
    IOSLanguageDetectionTabHelperWasDestroyed(
        IOSLanguageDetectionTabHelper* tab_helper) {
  DCHECK_EQ(tab_helper_, tab_helper);
  tab_helper_->RemoveObserver(this);
  tab_helper_ = nullptr;
}

}  // namespace language