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

fuchsia_web / webengine / browser / navigation_policy_handler.cc [blame]

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

#include "fuchsia_web/webengine/browser/navigation_policy_handler.h"

#include <lib/fidl/cpp/binding.h>

#include "base/fuchsia/fuchsia_logging.h"
#include "content/public/browser/navigation_handle.h"
#include "fuchsia_web/webengine/browser/navigation_policy_throttle.h"

NavigationPolicyHandler::NavigationPolicyHandler(
    fuchsia::web::NavigationPolicyProviderParams params,
    fidl::InterfaceHandle<fuchsia::web::NavigationPolicyProvider> delegate)
    : params_(std::move(params)), provider_(delegate.Bind()) {
  provider_.set_error_handler(fit::bind_member(
      this, &NavigationPolicyHandler::OnNavigationPolicyProviderDisconnected));
}

NavigationPolicyHandler::~NavigationPolicyHandler() {
  for (auto* throttle : navigation_throttles_) {
    throttle->OnNavigationPolicyProviderDisconnected(
        content::NavigationThrottle::CANCEL);
  }
  navigation_throttles_.clear();
}

void NavigationPolicyHandler::RegisterNavigationThrottle(
    NavigationPolicyThrottle* navigation_throttle) {
  navigation_throttles_.insert(navigation_throttle);
}

void NavigationPolicyHandler::RemoveNavigationThrottle(
    NavigationPolicyThrottle* navigation_throttle) {
  navigation_throttles_.erase(navigation_throttle);
}

void NavigationPolicyHandler::EvaluateRequestedNavigation(
    fuchsia::web::RequestedNavigation requested_navigation,
    fuchsia::web::NavigationPolicyProvider::EvaluateRequestedNavigationCallback
        callback) {
  provider_->EvaluateRequestedNavigation(std::move(requested_navigation),
                                         std::move(callback));
}

bool NavigationPolicyHandler::ShouldEvaluateNavigation(
    content::NavigationHandle* handle,
    fuchsia::web::NavigationPhase phase) {
  if (handle->IsInMainFrame()) {
    return (phase & params_.main_frame_phases()) == phase;
  }

  return (phase & params_.subframe_phases()) == phase;
}

bool NavigationPolicyHandler::is_provider_connected() {
  return provider_.is_bound();
}

void NavigationPolicyHandler::OnNavigationPolicyProviderDisconnected(
    zx_status_t status) {
  ZX_LOG(ERROR, status) << "NavigationPolicyProvider disconnected";
  for (auto* throttle : navigation_throttles_) {
    throttle->OnNavigationPolicyProviderDisconnected(
        content::NavigationThrottle::CANCEL);
  }
  navigation_throttles_.clear();
}