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