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

content / public / test / navigation_handle_observer.cc [blame]

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

#include "content/public/test/navigation_handle_observer.h"

#include "content/public/browser/navigation_handle.h"

namespace content {

NavigationHandleObserver::NavigationHandleObserver(
    WebContents* web_contents,
    const GURL& expected_start_url)
    : WebContentsObserver(web_contents),
      page_transition_(ui::PAGE_TRANSITION_LINK),
      expected_start_url_(expected_start_url) {}

NavigationHandleObserver::~NavigationHandleObserver() {}

void NavigationHandleObserver::DidStartNavigation(
    NavigationHandle* navigation_handle) {
  if (handle_ || navigation_handle->GetURL() != expected_start_url_)
    return;

  handle_ = navigation_handle;
  has_committed_ = false;
  is_error_ = false;
  page_transition_ = ui::PAGE_TRANSITION_LINK;
  last_committed_url_ = GURL();
  response_headers_.reset();

  is_main_frame_ = navigation_handle->IsInMainFrame();
  is_renderer_initiated_ = navigation_handle->IsRendererInitiated();
  is_same_document_ = navigation_handle->IsSameDocument();
  was_redirected_ = navigation_handle->WasServerRedirect();
  frame_tree_node_id_ = navigation_handle->GetFrameTreeNodeId();
  navigation_id_ = navigation_handle->GetNavigationId();
  navigation_start_ = navigation_handle->NavigationStart();
  reload_type_ = navigation_handle->GetReloadType();
  next_page_ukm_source_id_ = navigation_handle->GetNextPageUkmSourceId();
}

void NavigationHandleObserver::DidFinishNavigation(
    NavigationHandle* navigation_handle) {
  if (navigation_handle != handle_)
    return;

  DCHECK_EQ(is_main_frame_, navigation_handle->IsInMainFrame());
  DCHECK_EQ(is_same_document_, navigation_handle->IsSameDocument());
  DCHECK_EQ(is_renderer_initiated_, navigation_handle->IsRendererInitiated());
  DCHECK_EQ(frame_tree_node_id_, navigation_handle->GetFrameTreeNodeId());

  was_redirected_ = navigation_handle->WasServerRedirect();
  net_error_code_ = navigation_handle->GetNetErrorCode();
  is_download_ = navigation_handle->IsDownload();
  auth_challenge_info_ = navigation_handle->GetAuthChallengeInfo();
  resolve_error_info_ = navigation_handle->GetResolveErrorInfo();

  if (navigation_handle->HasCommitted()) {
    has_committed_ = true;
    if (!navigation_handle->IsErrorPage()) {
      page_transition_ = navigation_handle->GetPageTransition();
      last_committed_url_ = navigation_handle->GetURL();
      response_headers_ = navigation_handle->GetResponseHeaders();
    } else {
      is_error_ = true;
    }
  } else {
    has_committed_ = false;
    is_error_ = true;
  }

  navigation_handle_timing_ = navigation_handle->GetNavigationHandleTiming();

  handle_ = nullptr;
  content_settings_ = navigation_handle->GetContentSettingsForTesting();
}

std::string NavigationHandleObserver::GetNormalizedResponseHeader(
    const std::string& key) const {
  return response_headers_->GetNormalizedHeader(key).value_or(std::string());
}

}  // namespace content