1 // Copyright 2019 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_TEST_RESOURCE_LOAD_OBSERVER_H_ 6 #define CONTENT_TEST_RESOURCE_LOAD_OBSERVER_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/callback_forward.h" 12 #include "base/files/file_util.h" 13 #include "base/time/time.h" 14 #include "content/public/browser/web_contents_observer.h" 15 #include "content/shell/browser/shell.h" 16 #include "third_party/blink/public/mojom/loader/resource_load_info.mojom.h" 17 #include "url/gurl.h" 18 19 namespace content { 20 21 // Observer class to track resource loads. 22 class ResourceLoadObserver : public WebContentsObserver { 23 public: 24 explicit ResourceLoadObserver(Shell* shell); 25 26 ~ResourceLoadObserver() override; 27 resource_load_infos()28 const std::vector<blink::mojom::ResourceLoadInfoPtr>& resource_load_infos() 29 const { 30 return resource_load_infos_; 31 } 32 resource_is_associated_with_main_frame()33 const std::vector<bool>& resource_is_associated_with_main_frame() const { 34 return resource_is_associated_with_main_frame_; 35 } 36 memory_cached_loaded_urls()37 const std::vector<GURL>& memory_cached_loaded_urls() const { 38 return memory_cached_loaded_urls_; 39 } 40 41 // Use this method with the SCOPED_TRACE macro, so it shows the caller context 42 // if it fails. 43 void CheckResourceLoaded( 44 const GURL& original_url, 45 const GURL& referrer, 46 const std::string& load_method, 47 network::mojom::RequestDestination request_destination, 48 const base::FilePath::StringPieceType& served_file_name, 49 const std::string& mime_type, 50 const std::string& ip_address, 51 bool was_cached, 52 bool first_network_request, 53 const base::TimeTicks& before_request, 54 const base::TimeTicks& after_request); 55 56 // Returns the resource with the given url if found, otherwise nullptr. 57 blink::mojom::ResourceLoadInfoPtr* FindResource(const GURL& original_url); 58 59 void Reset(); 60 61 void WaitForResourceCompletion(const GURL& original_url); 62 63 private: 64 // WebContentsObserver implementation: 65 void ResourceLoadComplete( 66 content::RenderFrameHost* render_frame_host, 67 const GlobalRequestID& request_id, 68 const blink::mojom::ResourceLoadInfo& resource_load_info) override; 69 void DidLoadResourceFromMemoryCache( 70 const GURL& url, 71 const std::string& mime_type, 72 network::mojom::RequestDestination request_destination) override; 73 74 std::vector<GURL> memory_cached_loaded_urls_; 75 std::vector<blink::mojom::ResourceLoadInfoPtr> resource_load_infos_; 76 std::vector<bool> resource_is_associated_with_main_frame_; 77 GURL waiting_original_url_; 78 base::OnceClosure waiting_callback_; 79 80 DISALLOW_COPY_AND_ASSIGN(ResourceLoadObserver); 81 }; 82 83 } // namespace content 84 85 #endif // CONTENT_TEST_RESOURCE_LOAD_OBSERVER_H_ 86