OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 #include "core/loader/SubresourceFilter.h" | 5 #include "core/loader/SubresourceFilter.h" |
6 | 6 |
7 #include "core/dom/TaskRunnerHelper.h" | |
7 #include "core/loader/DocumentLoader.h" | 8 #include "core/loader/DocumentLoader.h" |
8 #include "public/platform/WebDocumentSubresourceFilter.h" | 9 #include "platform/WebTaskRunner.h" |
10 #include "platform/weborigin/KURL.h" | |
11 #include "public/platform/WebTraceLocation.h" | |
9 | 12 |
10 namespace blink { | 13 namespace blink { |
11 | 14 |
12 // static | 15 // static |
13 SubresourceFilter* SubresourceFilter::create( | 16 SubresourceFilter* SubresourceFilter::create( |
14 DocumentLoader* loader, | 17 DocumentLoader* loader, |
15 std::unique_ptr<WebDocumentSubresourceFilter> filter) { | 18 std::unique_ptr<WebDocumentSubresourceFilter> filter) { |
16 return new SubresourceFilter(loader, std::move(filter)); | 19 return new SubresourceFilter(loader, std::move(filter)); |
17 } | 20 } |
18 | 21 |
19 SubresourceFilter::SubresourceFilter( | 22 SubresourceFilter::SubresourceFilter( |
20 DocumentLoader* documentLoader, | 23 DocumentLoader* documentLoader, |
21 std::unique_ptr<WebDocumentSubresourceFilter> subresourceFilter) | 24 std::unique_ptr<WebDocumentSubresourceFilter> subresourceFilter) |
22 : m_documentLoader(documentLoader), | 25 : m_documentLoader(documentLoader), |
23 m_subresourceFilter(std::move(subresourceFilter)) {} | 26 m_subresourceFilter(std::move(subresourceFilter)) {} |
24 | 27 |
25 SubresourceFilter::~SubresourceFilter() {} | 28 SubresourceFilter::~SubresourceFilter() {} |
26 | 29 |
27 bool SubresourceFilter::allowLoad( | 30 bool SubresourceFilter::allowLoad( |
28 const KURL& resourceUrl, | 31 const KURL& resourceUrl, |
29 WebURLRequest::RequestContext requestContext, | 32 WebURLRequest::RequestContext requestContext, |
30 SecurityViolationReportingPolicy reportingPolicy) { | 33 SecurityViolationReportingPolicy reportingPolicy) { |
31 // TODO(csharrison): Implement a caching layer here which is a HashMap of | 34 // TODO(csharrison): Implement a caching layer here which is a HashMap of |
32 // Pair<url string, context> -> LoadPolicy. | 35 // Pair<url string, context> -> LoadPolicy. |
33 WebDocumentSubresourceFilter::LoadPolicy loadPolicy = | 36 WebDocumentSubresourceFilter::LoadPolicy loadPolicy = |
34 m_subresourceFilter->getLoadPolicy(resourceUrl, requestContext); | 37 m_subresourceFilter->getLoadPolicy(resourceUrl, requestContext); |
35 if (reportingPolicy == SecurityViolationReportingPolicy::Report) { | 38 if (reportingPolicy == SecurityViolationReportingPolicy::Report) |
36 switch (loadPolicy) { | 39 notifyLoad(resourceUrl, loadPolicy); |
37 case WebDocumentSubresourceFilter::Allow: | |
38 break; | |
39 case WebDocumentSubresourceFilter::Disallow: | |
40 m_subresourceFilter->reportDisallowedLoad(); | |
41 // fall through | |
42 case WebDocumentSubresourceFilter::WouldDisallow: | |
43 m_documentLoader->didObserveLoadingBehavior( | |
44 WebLoadingBehaviorSubresourceFilterMatch); | |
45 break; | |
46 } | |
47 } | |
48 return loadPolicy != WebDocumentSubresourceFilter::Disallow; | 40 return loadPolicy != WebDocumentSubresourceFilter::Disallow; |
49 } | 41 } |
50 | 42 |
43 bool SubresourceFilter::allowWebSocketConnection(const KURL& resourceUrl) { | |
44 // TODO(csharrison): Should probably have a new API for this in | |
45 // WebDocumentSubresourceFilter rather than sending subresource context. | |
46 // Alternatively, could augment the filter API so that we send a | |
47 // WebDocumentSubresourceFilterResourceType that matches | |
48 // subresource_filter::proto::ElementType. | |
49 WebDocumentSubresourceFilter::LoadPolicy loadPolicy = | |
50 m_subresourceFilter->getLoadPolicy( | |
51 resourceUrl, WebURLRequest::RequestContextSubresource); | |
52 | |
53 // Post a task to notify this load to avoid unduly blocking the worker | |
54 // thread. Note that this unconditionally calls notifyLoad unlike allowLoad, | |
55 // because there aren't developer-invisible connections (like speculative | |
56 // preloads) happening here. | |
57 TaskRunnerHelper::get(TaskType::Networking, m_documentLoader->frame()) | |
58 ->postTask(BLINK_FROM_HERE, | |
59 WTF::bind(&SubresourceFilter::notifyLoad, wrapPersistent(this), | |
60 resourceUrl, loadPolicy)); | |
61 return loadPolicy != WebDocumentSubresourceFilter::Disallow; | |
62 } | |
63 | |
64 void SubresourceFilter::notifyLoad( | |
engedy
2017/03/06 14:12:46
nit: WDYT about calling this `reportLoad`? At leas
Charlie Harrison
2017/03/06 14:48:23
Sure, changed to report load.
| |
65 const KURL& resourceUrl, | |
engedy
2017/03/06 14:12:46
nit: |resourceUrl| argument is never used
Charlie Harrison
2017/03/06 14:48:23
Removed :)
| |
66 WebDocumentSubresourceFilter::LoadPolicy loadPolicy) { | |
67 switch (loadPolicy) { | |
68 case WebDocumentSubresourceFilter::Allow: | |
69 break; | |
70 case WebDocumentSubresourceFilter::Disallow: | |
71 m_subresourceFilter->reportDisallowedLoad(); | |
72 // fall through | |
73 case WebDocumentSubresourceFilter::WouldDisallow: | |
74 m_documentLoader->didObserveLoadingBehavior( | |
75 WebLoadingBehaviorSubresourceFilterMatch); | |
76 break; | |
77 } | |
78 } | |
79 | |
51 } // namespace blink | 80 } // namespace blink |
OLD | NEW |