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
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170

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

// 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.

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

#include <string>
#include <vector>

#include "base/path_service.h"
#include "base/run_loop.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/common/content_paths.h"
#include "content/shell/browser/shell.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace content {

ResourceLoadObserver::ResourceLoadEntry::ResourceLoadEntry(
    blink::mojom::ResourceLoadInfoPtr resource_load_info,
    bool resource_is_associated_with_main_frame)
    : resource_load_info(std::move(resource_load_info)),
      resource_is_associated_with_main_frame(
          resource_is_associated_with_main_frame) {}

ResourceLoadObserver::ResourceLoadEntry::~ResourceLoadEntry() = default;

ResourceLoadObserver::ResourceLoadEntry::ResourceLoadEntry(
    ResourceLoadObserver::ResourceLoadEntry&&) = default;

ResourceLoadObserver::ResourceLoadEntry&
ResourceLoadObserver::ResourceLoadEntry::operator=(
    ResourceLoadObserver::ResourceLoadEntry&&) = default;

ResourceLoadObserver::ResourceLoadObserver(Shell* shell)
    : WebContentsObserver(shell->web_contents()) {}

ResourceLoadObserver::ResourceLoadObserver(WebContents* web_contents)
    : WebContentsObserver(web_contents) {}

ResourceLoadObserver::~ResourceLoadObserver() = default;

// Use this method with the SCOPED_TRACE macro, so it shows the caller context
// if it fails.
void ResourceLoadObserver::CheckResourceLoaded(
    const GURL& original_url,
    const GURL& referrer,
    const std::string& load_method,
    network::mojom::RequestDestination request_destination,
    const base::FilePath::StringPieceType& served_file_name,
    const std::string& mime_type,
    const std::string& ip_address,
    bool was_cached,
    bool first_network_request,
    const base::TimeTicks& before_request,
    const base::TimeTicks& after_request) {
  bool resource_load_info_found = false;
  for (const auto& resource_load_entry : resource_load_entries_) {
    const auto& resource_load_info = resource_load_entry.resource_load_info;
    if (resource_load_info->original_url != original_url) {
      continue;
    }

    resource_load_info_found = true;
    int64_t file_size = -1;
    if (!served_file_name.empty()) {
      base::ScopedAllowBlockingForTesting allow_blocking;
      base::FilePath test_dir;
      ASSERT_TRUE(base::PathService::Get(content::DIR_TEST_DATA, &test_dir));
      base::FilePath served_file = test_dir.Append(served_file_name);
      ASSERT_TRUE(GetFileSize(served_file, &file_size));
    }
    EXPECT_EQ(referrer, resource_load_info->referrer);
    EXPECT_EQ(load_method, resource_load_info->method);
    EXPECT_EQ(request_destination, resource_load_info->request_destination);
    if (!first_network_request) {
      EXPECT_GT(resource_load_info->request_id, 0);
    }
    EXPECT_EQ(mime_type, resource_load_info->mime_type);
    ASSERT_TRUE(resource_load_info->network_info->remote_endpoint);
    EXPECT_EQ(ip_address, resource_load_info->network_info->remote_endpoint
                              ->ToStringWithoutPort());
    EXPECT_EQ(was_cached, resource_load_info->was_cached);
    // Simple sanity check of the load timing info.
    auto CheckTime = [before_request, after_request](auto actual) {
      EXPECT_LE(before_request, actual);
      EXPECT_GT(after_request, actual);
    };
    const net::LoadTimingInfo& timing = resource_load_info->load_timing_info;
    CheckTime(timing.request_start);
    CheckTime(timing.receive_headers_end);
    CheckTime(timing.send_start);
    CheckTime(timing.send_end);
    if (!was_cached) {
      CheckTime(timing.connect_timing.domain_lookup_start);
      CheckTime(timing.connect_timing.domain_lookup_end);
      CheckTime(timing.connect_timing.connect_start);
      CheckTime(timing.connect_timing.connect_end);
    }
    if (file_size != -1) {
      EXPECT_EQ(file_size, resource_load_info->raw_body_bytes);
      EXPECT_LT(file_size, resource_load_info->total_received_bytes);
    }
  }
  EXPECT_TRUE(resource_load_info_found);
}

// Returns the resource with the given url if found, otherwise nullptr.
blink::mojom::ResourceLoadInfoPtr* ResourceLoadObserver::GetResource(
    const GURL& original_url) {
  for (auto& entry : resource_load_entries_) {
    if (entry.resource_load_info->original_url == original_url) {
      return &entry.resource_load_info;
    }
  }
  return nullptr;
}

void ResourceLoadObserver::Reset() {
  resource_load_entries_.clear();
  memory_cached_loaded_urls_.clear();
}

void ResourceLoadObserver::WaitForResourceCompletion(const GURL& original_url) {
  // If we've already seen the resource, return immediately.
  for (const auto& entry : resource_load_entries_) {
    if (entry.resource_load_info->original_url == original_url) {
      return;
    }
  }

  // Otherwise wait for it.
  base::RunLoop loop;
  waiting_original_url_ = original_url;
  waiting_callback_ = loop.QuitClosure();
  loop.Run();
}

// WebContentsObserver implementation:
void ResourceLoadObserver::ResourceLoadComplete(
    content::RenderFrameHost* render_frame_host,
    const GlobalRequestID& request_id,
    const blink::mojom::ResourceLoadInfo& resource_load_info) {
  EXPECT_NE(nullptr, render_frame_host);
  resource_load_entries_.emplace_back(ResourceLoadEntry(
      resource_load_info.Clone(), render_frame_host->IsInPrimaryMainFrame()));
  // Sorts entries with request start time since the resource loading time is
  // not deterministic.
  std::sort(resource_load_entries_.begin(), resource_load_entries_.end(),
            [](auto& a, auto& b) {
              return a.resource_load_info->load_timing_info.request_start <
                     b.resource_load_info->load_timing_info.request_start;
            });
  // Have we been waiting for this resource? If so, run the callback.
  if (waiting_original_url_.is_valid() &&
      resource_load_info.original_url == waiting_original_url_) {
    waiting_original_url_ = GURL();
    std::move(waiting_callback_).Run();
  }
}

void ResourceLoadObserver::DidLoadResourceFromMemoryCache(
    content::RenderFrameHost* render_frame_host,
    const GURL& url,
    const std::string& mime_type,
    network::mojom::RequestDestination request_destination) {
  memory_cached_loaded_urls_.push_back(url);
}

}  // namespace content