OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_SUBRESOURCE_FILTER_CONTENT_COMMON_DOCUMENT_SUBRESOURCE_FILTER _H_ | 5 #ifndef COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_DOCUMENT_SUBRESOURCE_FILTER_H_ |
6 #define COMPONENTS_SUBRESOURCE_FILTER_CONTENT_COMMON_DOCUMENT_SUBRESOURCE_FILTER _H_ | 6 #define COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_DOCUMENT_SUBRESOURCE_FILTER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/callback.h" | |
14 #include "base/macros.h" | 13 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
16 #include "base/memory/weak_ptr.h" | |
17 #include "base/time/time.h" | |
18 #include "components/subresource_filter/content/common/document_load_statistics. h" | |
19 #include "components/subresource_filter/core/common/activation_level.h" | 15 #include "components/subresource_filter/core/common/activation_level.h" |
20 #include "components/subresource_filter/core/common/activation_state.h" | 16 #include "components/subresource_filter/core/common/activation_state.h" |
17 #include "components/subresource_filter/core/common/document_load_statistics.h" | |
21 #include "components/subresource_filter/core/common/indexed_ruleset.h" | 18 #include "components/subresource_filter/core/common/indexed_ruleset.h" |
22 #include "components/subresource_filter/core/common/proto/rules.pb.h" | 19 #include "components/subresource_filter/core/common/proto/rules.pb.h" |
23 #include "third_party/WebKit/public/platform/WebDocumentSubresourceFilter.h" | |
24 #include "url/gurl.h" | 20 #include "url/gurl.h" |
engedy
2017/02/17 16:44:04
nit: Same question, can we forward-declare GURL an
pkalinnikov
2017/02/17 17:16:48
Done.
| |
25 #include "url/origin.h" | 21 #include "url/origin.h" |
26 | 22 |
27 namespace subresource_filter { | 23 namespace subresource_filter { |
28 | 24 |
29 class FirstPartyOrigin; | 25 class FirstPartyOrigin; |
30 class MemoryMappedRuleset; | 26 class MemoryMappedRuleset; |
31 | 27 |
28 enum class LoadPolicy { | |
29 ALLOW, | |
30 DISALLOW, | |
31 WOULD_DISALLOW, | |
32 }; | |
33 | |
32 // Computes whether/how subresource filtering should be activated while loading | 34 // Computes whether/how subresource filtering should be activated while loading |
33 // |document_url| in a frame, based on the parent document's |activation_state|, | 35 // |document_url| in a frame, based on the parent document's |activation_state|, |
34 // the |parent_document_origin|, as well as any applicable deactivation rules in | 36 // the |parent_document_origin|, as well as any applicable deactivation rules in |
35 // non-null |ruleset|. | 37 // non-null |ruleset|. |
36 ActivationState ComputeActivationState( | 38 ActivationState ComputeActivationState( |
37 const GURL& document_url, | 39 const GURL& document_url, |
38 const url::Origin& parent_document_origin, | 40 const url::Origin& parent_document_origin, |
39 const ActivationState& parent_activation_state, | 41 const ActivationState& parent_activation_state, |
40 const MemoryMappedRuleset* ruleset); | 42 const MemoryMappedRuleset* ruleset); |
41 | 43 |
42 // Same as above, but instead of relying on the pre-computed activation state of | 44 // Same as above, but instead of relying on the pre-computed activation state of |
43 // the parent, computes the activation state for a frame from scratch, based on | 45 // the parent, computes the activation state for a frame from scratch, based on |
44 // the page-level activation options |activation_level| and | 46 // the page-level activation options |activation_level| and |
45 // |measure_performance|; as well as any deactivation rules in |ruleset| that | 47 // |measure_performance|; as well as any deactivation rules in |ruleset| that |
46 // apply to |ancestor_document_urls|. | 48 // apply to |ancestor_document_urls|. |
47 // | 49 // |
48 // TODO(pkalinnikov): Remove this when browser-side navigation is supported. | 50 // TODO(pkalinnikov): Remove this when browser-side navigation is supported. |
49 ActivationState ComputeActivationState( | 51 ActivationState ComputeActivationState( |
50 ActivationLevel activation_level, | 52 ActivationLevel activation_level, |
51 bool measure_performance, | 53 bool measure_performance, |
52 const std::vector<GURL>& ancestor_document_urls, | 54 const std::vector<GURL>& ancestor_document_urls, |
53 const MemoryMappedRuleset* ruleset); | 55 const MemoryMappedRuleset* ruleset); |
54 | 56 |
55 // Performs filtering of subresource loads in the scope of a given document. | 57 // Performs filtering of subresource loads in the scope of a given document. |
56 class DocumentSubresourceFilter | 58 class DocumentSubresourceFilter { |
57 : public blink::WebDocumentSubresourceFilter, | |
58 public base::SupportsWeakPtr<DocumentSubresourceFilter> { | |
59 public: | 59 public: |
60 // Constructs a new filter that will: | 60 // Constructs a new filter that will: |
61 // -- Operate in a manner prescribed in |activation_state|. | 61 // -- Operate in a manner prescribed in |activation_state|. |
62 // -- Filter subresource loads in the scope of a document loaded from | 62 // -- Filter subresource loads in the scope of a document loaded from |
63 // |document_origin|. | 63 // |document_origin|. |
64 // -- Hold a reference to and use |ruleset| for its entire lifetime. | 64 // -- Hold a reference to and use |ruleset| for its entire lifetime. |
65 // -- Invoke |first_disallowed_load_callback|, if it is non-null, on the | |
66 // first disallowed subresource load. | |
67 DocumentSubresourceFilter(url::Origin document_origin, | 65 DocumentSubresourceFilter(url::Origin document_origin, |
68 ActivationState activation_state, | 66 ActivationState activation_state, |
69 scoped_refptr<const MemoryMappedRuleset> ruleset, | 67 scoped_refptr<const MemoryMappedRuleset> ruleset); |
70 base::OnceClosure first_disallowed_load_callback); | |
71 | 68 |
72 ~DocumentSubresourceFilter() override; | 69 ~DocumentSubresourceFilter(); |
73 | 70 |
71 ActivationState activation_state() const { return activation_state_; } | |
74 const DocumentLoadStatistics& statistics() const { return statistics_; } | 72 const DocumentLoadStatistics& statistics() const { return statistics_; } |
75 bool is_performance_measuring_enabled() const { | |
76 return activation_state_.measure_performance; | |
77 } | |
78 | 73 |
79 // blink::WebDocumentSubresourceFilter: | 74 // WARNING: This is only to allow DocumentSubresourceFilter's wrappers to |
80 LoadPolicy getLoadPolicy(const blink::WebURL& resourceUrl, | 75 // modify the |statistics|. |
81 blink::WebURLRequest::RequestContext) override; | 76 // TODO(pkalinnikov): Find a better way to achieve this. |
82 void reportDisallowedLoad() override; | 77 DocumentLoadStatistics& statistics() { return statistics_; } |
83 | 78 |
84 LoadPolicy GetLoadPolicyForSubdocument(const GURL& subdocument_url); | 79 LoadPolicy GetLoadPolicy(const GURL& subresource_url, |
80 proto::ElementType subresource_type); | |
85 | 81 |
86 private: | 82 private: |
87 LoadPolicy EvaluateLoadPolicy(const GURL& resource_url, | |
88 proto::ElementType element_type); | |
89 | |
90 const ActivationState activation_state_; | 83 const ActivationState activation_state_; |
91 const scoped_refptr<const MemoryMappedRuleset> ruleset_; | 84 const scoped_refptr<const MemoryMappedRuleset> ruleset_; |
92 const IndexedRulesetMatcher ruleset_matcher_; | 85 const IndexedRulesetMatcher ruleset_matcher_; |
93 | 86 |
94 // Equals nullptr iff |activation_state_.filtering_disabled_for_document|. | 87 // Equals nullptr iff |activation_state_.filtering_disabled_for_document|. |
95 std::unique_ptr<FirstPartyOrigin> document_origin_; | 88 std::unique_ptr<FirstPartyOrigin> document_origin_; |
96 | 89 |
97 base::OnceClosure first_disallowed_load_callback_; | |
98 DocumentLoadStatistics statistics_; | 90 DocumentLoadStatistics statistics_; |
99 | 91 |
100 DISALLOW_COPY_AND_ASSIGN(DocumentSubresourceFilter); | 92 DISALLOW_COPY_AND_ASSIGN(DocumentSubresourceFilter); |
101 }; | 93 }; |
102 | 94 |
103 } // namespace subresource_filter | 95 } // namespace subresource_filter |
104 | 96 |
105 #endif // COMPONENTS_SUBRESOURCE_FILTER_CONTENT_COMMON_DOCUMENT_SUBRESOURCE_FIL TER_H_ | 97 #endif // COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_DOCUMENT_SUBRESOURCE_FILTER _H_ |
OLD | NEW |