1 // Copyright 2018 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 THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_PREVIEWS_RESOURCE_LOADING_HINTS_H_
6 #define THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_PREVIEWS_RESOURCE_LOADING_HINTS_H_
7 
8 #include "base/feature_list.h"
9 #include "third_party/blink/public/platform/web_loading_hints_provider.h"
10 #include "third_party/blink/renderer/core/core_export.h"
11 #include "third_party/blink/renderer/platform/heap/garbage_collected.h"
12 #include "third_party/blink/renderer/platform/heap/handle.h"
13 #include "third_party/blink/renderer/platform/loader/fetch/resource.h"
14 #include "third_party/blink/renderer/platform/loader/fetch/resource_load_priority.h"
15 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
16 #include "third_party/blink/renderer/platform/wtf/vector.h"
17 
18 namespace ukm {
19 class UkmRecorder;
20 }
21 
22 namespace blink {
23 
24 class ExecutionContext;
25 class KURL;
26 
27 // PreviewsResourceLoadingHints stores the resource loading hints that apply to
28 // a single document.
29 class CORE_EXPORT PreviewsResourceLoadingHints final
30     : public GarbageCollected<PreviewsResourceLoadingHints> {
31  public:
32   static PreviewsResourceLoadingHints* Create(
33       ExecutionContext& execution_context,
34       int64_t ukm_source_id,
35       const WebVector<WebString>& subresource_patterns_to_block);
36 
37   static PreviewsResourceLoadingHints* CreateFromLoadingHintsProvider(
38       ExecutionContext& execution_context,
39       std::unique_ptr<WebLoadingHintsProvider> loading_hints_provider);
40 
41   PreviewsResourceLoadingHints(
42       ExecutionContext* execution_context,
43       int64_t ukm_source_id,
44       const WebVector<WebString>& subresource_patterns_to_block);
45   ~PreviewsResourceLoadingHints();
46 
47   // Returns true if load of resource with type |type|, URL |resource_url|
48   // and priority |resource_load_priority| is allowed as per resource loading
49   // hints.
50   bool AllowLoad(ResourceType type,
51                  const KURL& resource_url,
52                  ResourceLoadPriority resource_load_priority) const;
53 
54   virtual void Trace(Visitor*) const;
55 
56   // Records UKM on the utilization of patterns to block during the document
57   // load. This is expected to be called once after the document finishes
58   // loading.
59   void RecordUKM(ukm::UkmRecorder* ukm_recorder) const;
60 
61  private:
62   // Reports to console when loading of |resource_url| is blocked.
63   void ReportBlockedLoading(const KURL& resource_url) const;
64 
65   Member<ExecutionContext> execution_context_;
66   const int64_t ukm_source_id_;
67 
68   // |subresource_patterns_to_block_| is a collection of subresource patterns
69   // for resources whose loading should be blocked. Each pattern is a
70   // WebString. If a subresource URL contains any of the strings specified in
71   // |subresource_patterns_to_block_|, then that subresource's loading could
72   // be blocked.
73   const WebVector<WebString> subresource_patterns_to_block_;
74 
75   // True if resource blocking hints should apply to resource of a given type.
76   bool block_resource_type_[static_cast<int>(ResourceType::kMaxValue) + 1] = {
77       false};
78 
79   // |subresource_patterns_to_block_usage_| records whether the pattern located
80   // at the same index in |subresource_patterns_to_block_| was ever blocked.
81   mutable Vector<bool> subresource_patterns_to_block_usage_;
82 
83   // |blocked_resource_load_priority_counts_| records the total number of
84   // resources blocked at each ResourceLoadPriority.
85   mutable Vector<int> blocked_resource_load_priority_counts_;
86 };
87 
88 }  // namespace blink
89 
90 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_PREVIEWS_RESOURCE_LOADING_HINTS_H_
91