// 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 "ios/chrome/browser/url_loading/model/url_loading_notifier_browser_agent.h"

#import "ios/chrome/browser/url_loading/model/url_loading_observer.h"
#import "ios/chrome/browser/url_loading/model/url_loading_params.h"

UrlLoadingNotifierBrowserAgent::UrlLoadingNotifierBrowserAgent(Browser* browser)
    : BrowserUserData(browser) {}

UrlLoadingNotifierBrowserAgent::~UrlLoadingNotifierBrowserAgent() {}

void UrlLoadingNotifierBrowserAgent::AddObserver(UrlLoadingObserver* observer) {
  observers_.AddObserver(observer);
}

void UrlLoadingNotifierBrowserAgent::RemoveObserver(
    UrlLoadingObserver* observer) {
  observers_.RemoveObserver(observer);
}

void UrlLoadingNotifierBrowserAgent::TabWillLoadUrl(
    const UrlLoadParams& params,
    base::WeakPtr<web::WebState> web_state) {
  for (auto& observer : observers_) {
    observer.TabWillLoadUrl(params, web_state);
  }
}

void UrlLoadingNotifierBrowserAgent::TabFailedToLoadUrl(
    const GURL& url,
    ui::PageTransition transition_type,
    base::WeakPtr<web::WebState> web_state) {
  for (auto& observer : observers_) {
    observer.TabFailedToLoadUrl(url, transition_type, web_state);
  }
}

void UrlLoadingNotifierBrowserAgent::TabDidPrerenderUrl(
    const GURL& url,
    ui::PageTransition transition_type,
    base::WeakPtr<web::WebState> web_state) {
  for (auto& observer : observers_) {
    observer.TabDidPrerenderUrl(url, transition_type, web_state);
  }
}

void UrlLoadingNotifierBrowserAgent::TabDidReloadUrl(
    const GURL& url,
    ui::PageTransition transition_type,
    base::WeakPtr<web::WebState> web_state) {
  for (auto& observer : observers_) {
    observer.TabDidReloadUrl(url, transition_type, web_state);
  }
}

void UrlLoadingNotifierBrowserAgent::TabDidLoadUrl(
    const GURL& url,
    ui::PageTransition transition_type,
    base::WeakPtr<web::WebState> web_state) {
  for (auto& observer : observers_) {
    observer.TabDidLoadUrl(url, transition_type, web_state);
  }
}

void UrlLoadingNotifierBrowserAgent::NewTabWillLoadUrl(const GURL& url,
                                                       bool user_initiated) {
  for (auto& observer : observers_) {
    observer.NewTabWillLoadUrl(url, user_initiated);
  }
}

void UrlLoadingNotifierBrowserAgent::NewTabDidLoadUrl(const GURL& url,
                                                      bool user_initiated) {
  for (auto& observer : observers_) {
    observer.NewTabDidLoadUrl(url, user_initiated);
  }
}

void UrlLoadingNotifierBrowserAgent::WillSwitchToTabWithUrl(
    const GURL& url,
    int new_web_state_index) {
  for (auto& observer : observers_) {
    observer.WillSwitchToTabWithUrl(url, new_web_state_index);
  }
}

void UrlLoadingNotifierBrowserAgent::DidSwitchToTabWithUrl(
    const GURL& url,
    int new_web_state_index) {
  for (auto& observer : observers_) {
    observer.DidSwitchToTabWithUrl(url, new_web_state_index);
  }
}
