778336854cbf577bcbc9d5443b2dcf2f2aadede8
[WebKit-https.git] / Source / WebCore / loader / ResourceLoadInfo.cpp
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::MainResource:
41         return ResourceType::Document;
42     case CachedResource::Type::SVGDocumentResource:
43         return ResourceType::SVGDocument;
44     case CachedResource::Type::ImageResource:
45         return ResourceType::Image;
46     case CachedResource::Type::CSSStyleSheet:
47 #if ENABLE(XSLT)
48     case CachedResource::Type::XSLStyleSheet:
49 #endif
50         return ResourceType::StyleSheet;
51
52     case CachedResource::Type::Script:
53         return ResourceType::Script;
54
55     case CachedResource::Type::FontResource:
56 #if ENABLE(SVG_FONTS)
57     case CachedResource::Type::SVGFontResource:
58 #endif
59         return ResourceType::Font;
60
61     case CachedResource::Type::MediaResource:
62         return ResourceType::Media;
63
64     case CachedResource::Type::Beacon:
65     case CachedResource::Type::Icon:
66     case CachedResource::Type::RawResource:
67         return ResourceType::Raw;
68
69 #if ENABLE(VIDEO_TRACK)
70     case CachedResource::Type::TextTrackResource:
71         return ResourceType::Media;
72 #endif
73     case CachedResource::Type::LinkPrefetch:
74         ASSERT_NOT_REACHED();
75         break;
76 #if ENABLE(APPLICATION_MANIFEST)
77     case CachedResource::Type::ApplicationManifest:
78         return ResourceType::Raw;
79 #endif
80     };
81     return ResourceType::Raw;
82 }
83
84 uint16_t readResourceType(const String& name)
85 {
86     if (name == "document")
87         return static_cast<uint16_t>(ResourceType::Document);
88     if (name == "image")
89         return static_cast<uint16_t>(ResourceType::Image);
90     if (name == "style-sheet")
91         return static_cast<uint16_t>(ResourceType::StyleSheet);
92     if (name == "script")
93         return static_cast<uint16_t>(ResourceType::Script);
94     if (name == "font")
95         return static_cast<uint16_t>(ResourceType::Font);
96     if (name == "raw")
97         return static_cast<uint16_t>(ResourceType::Raw);
98     if (name == "svg-document")
99         return static_cast<uint16_t>(ResourceType::SVGDocument);
100     if (name == "media")
101         return static_cast<uint16_t>(ResourceType::Media);
102     if (name == "popup")
103         return static_cast<uint16_t>(ResourceType::Popup);
104     return static_cast<uint16_t>(ResourceType::Invalid);
105 }
106
107 uint16_t readLoadType(const String& name)
108 {
109     if (name == "first-party")
110         return static_cast<uint16_t>(LoadType::FirstParty);
111     if (name == "third-party")
112         return static_cast<uint16_t>(LoadType::ThirdParty);
113     return static_cast<uint16_t>(LoadType::Invalid);
114 }
115
116 bool ResourceLoadInfo::isThirdParty() const
117 {
118     Ref<SecurityOrigin> mainDocumentSecurityOrigin = SecurityOrigin::create(mainDocumentURL);
119     Ref<SecurityOrigin> resourceSecurityOrigin = SecurityOrigin::create(resourceURL);
120
121     return !mainDocumentSecurityOrigin->canAccess(resourceSecurityOrigin.get());
122 }
123     
124 ResourceFlags ResourceLoadInfo::getResourceFlags() const
125 {
126     ResourceFlags flags = 0;
127     ASSERT(type != ResourceType::Invalid);
128     flags |= static_cast<ResourceFlags>(type);
129     flags |= isThirdParty() ? static_cast<ResourceFlags>(LoadType::ThirdParty) : static_cast<ResourceFlags>(LoadType::FirstParty);
130     return flags;
131 }
132
133 } // namespace ContentExtensions
134 } // namespace WebCore
135
136 #endif // ENABLE(CONTENT_EXTENSIONS)