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