1 | /* |
2 | * Copyright (C) 2015 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "ResourceLoadInfo.h" |
28 | |
29 | #include "ContentExtensionActions.h" |
30 | #include "SecurityOrigin.h" |
31 | |
32 | #if ENABLE(CONTENT_EXTENSIONS) |
33 | |
34 | namespace WebCore { |
35 | namespace ContentExtensions { |
36 | |
37 | ResourceType toResourceType(CachedResource::Type type) |
38 | { |
39 | switch (type) { |
40 | case CachedResource::Type::LinkPrefetch: |
41 | case CachedResource::Type::MainResource: |
42 | return ResourceType::Document; |
43 | case CachedResource::Type::SVGDocumentResource: |
44 | return ResourceType::SVGDocument; |
45 | case CachedResource::Type::ImageResource: |
46 | return ResourceType::Image; |
47 | case CachedResource::Type::CSSStyleSheet: |
48 | #if ENABLE(XSLT) |
49 | case CachedResource::Type::XSLStyleSheet: |
50 | #endif |
51 | return ResourceType::StyleSheet; |
52 | |
53 | case CachedResource::Type::Script: |
54 | return ResourceType::Script; |
55 | |
56 | case CachedResource::Type::FontResource: |
57 | #if ENABLE(SVG_FONTS) |
58 | case CachedResource::Type::SVGFontResource: |
59 | #endif |
60 | return ResourceType::Font; |
61 | |
62 | case CachedResource::Type::MediaResource: |
63 | return ResourceType::Media; |
64 | |
65 | case CachedResource::Type::Beacon: |
66 | case CachedResource::Type::Ping: |
67 | case CachedResource::Type::Icon: |
68 | case CachedResource::Type::RawResource: |
69 | return ResourceType::Raw; |
70 | |
71 | #if ENABLE(VIDEO_TRACK) |
72 | case CachedResource::Type::TextTrackResource: |
73 | return ResourceType::Media; |
74 | #endif |
75 | #if ENABLE(APPLICATION_MANIFEST) |
76 | case CachedResource::Type::ApplicationManifest: |
77 | return ResourceType::Raw; |
78 | #endif |
79 | }; |
80 | return ResourceType::Raw; |
81 | } |
82 | |
83 | uint16_t readResourceType(const String& name) |
84 | { |
85 | if (name == "document" ) |
86 | return static_cast<uint16_t>(ResourceType::Document); |
87 | if (name == "image" ) |
88 | return static_cast<uint16_t>(ResourceType::Image); |
89 | if (name == "style-sheet" ) |
90 | return static_cast<uint16_t>(ResourceType::StyleSheet); |
91 | if (name == "script" ) |
92 | return static_cast<uint16_t>(ResourceType::Script); |
93 | if (name == "font" ) |
94 | return static_cast<uint16_t>(ResourceType::Font); |
95 | if (name == "raw" ) |
96 | return static_cast<uint16_t>(ResourceType::Raw); |
97 | if (name == "svg-document" ) |
98 | return static_cast<uint16_t>(ResourceType::SVGDocument); |
99 | if (name == "media" ) |
100 | return static_cast<uint16_t>(ResourceType::Media); |
101 | if (name == "popup" ) |
102 | return static_cast<uint16_t>(ResourceType::Popup); |
103 | if (name == "ping" ) |
104 | return static_cast<uint16_t>(ResourceType::Ping); |
105 | return static_cast<uint16_t>(ResourceType::Invalid); |
106 | } |
107 | |
108 | uint16_t readLoadType(const String& name) |
109 | { |
110 | if (name == "first-party" ) |
111 | return static_cast<uint16_t>(LoadType::FirstParty); |
112 | if (name == "third-party" ) |
113 | return static_cast<uint16_t>(LoadType::ThirdParty); |
114 | return static_cast<uint16_t>(LoadType::Invalid); |
115 | } |
116 | |
117 | bool ResourceLoadInfo::isThirdParty() const |
118 | { |
119 | Ref<SecurityOrigin> mainDocumentSecurityOrigin = SecurityOrigin::create(mainDocumentURL); |
120 | Ref<SecurityOrigin> resourceSecurityOrigin = SecurityOrigin::create(resourceURL); |
121 | |
122 | return !mainDocumentSecurityOrigin->canAccess(resourceSecurityOrigin.get()); |
123 | } |
124 | |
125 | ResourceFlags ResourceLoadInfo::getResourceFlags() const |
126 | { |
127 | ResourceFlags flags = 0; |
128 | ASSERT(type != ResourceType::Invalid); |
129 | flags |= type.toRaw(); |
130 | flags |= isThirdParty() ? static_cast<ResourceFlags>(LoadType::ThirdParty) : static_cast<ResourceFlags>(LoadType::FirstParty); |
131 | return flags; |
132 | } |
133 | |
134 | } // namespace ContentExtensions |
135 | } // namespace WebCore |
136 | |
137 | #endif // ENABLE(CONTENT_EXTENSIONS) |
138 | |