1/*
2 * Copyright (C) 2008 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 */
26
27#include "config.h"
28#include "NavigatorBase.h"
29
30#include "Document.h"
31#include "RuntimeEnabledFeatures.h"
32#include "ServiceWorkerContainer.h"
33#include <mutex>
34#include <wtf/Language.h>
35#include <wtf/NeverDestroyed.h>
36#include <wtf/NumberOfCores.h>
37#include <wtf/text/WTFString.h>
38
39#if OS(LINUX)
40#include "sys/utsname.h"
41#include <wtf/StdLibExtras.h>
42#endif
43
44#if PLATFORM(IOS_FAMILY)
45#include "Device.h"
46#endif
47
48#ifndef WEBCORE_NAVIGATOR_PRODUCT
49#define WEBCORE_NAVIGATOR_PRODUCT "Gecko"_s
50#endif // ifndef WEBCORE_NAVIGATOR_PRODUCT
51
52#ifndef WEBCORE_NAVIGATOR_PRODUCT_SUB
53#define WEBCORE_NAVIGATOR_PRODUCT_SUB "20030107"_s
54#endif // ifndef WEBCORE_NAVIGATOR_PRODUCT_SUB
55
56#ifndef WEBCORE_NAVIGATOR_VENDOR
57#define WEBCORE_NAVIGATOR_VENDOR "Apple Computer, Inc."_s
58#endif // ifndef WEBCORE_NAVIGATOR_VENDOR
59
60#ifndef WEBCORE_NAVIGATOR_VENDOR_SUB
61#define WEBCORE_NAVIGATOR_VENDOR_SUB emptyString()
62#endif // ifndef WEBCORE_NAVIGATOR_VENDOR_SUB
63
64namespace WebCore {
65
66NavigatorBase::NavigatorBase(ScriptExecutionContext* context)
67 : ContextDestructionObserver(context)
68{
69}
70
71NavigatorBase::~NavigatorBase() = default;
72
73String NavigatorBase::appName()
74{
75 return "Netscape"_s;
76}
77
78String NavigatorBase::appVersion() const
79{
80 // Version is everything in the user agent string past the "Mozilla/" prefix.
81 const String& agent = userAgent();
82 return agent.substring(agent.find('/') + 1);
83}
84
85String NavigatorBase::platform() const
86{
87#if OS(LINUX)
88 static LazyNeverDestroyed<String> platformName;
89 static std::once_flag onceKey;
90 std::call_once(onceKey, [] {
91 struct utsname osname;
92 platformName.construct(uname(&osname) >= 0 ? String(osname.sysname) + " "_str + String(osname.machine) : String(""_s));
93 });
94 return platformName->isolatedCopy();
95#elif PLATFORM(IOS_FAMILY)
96 return deviceName();
97#elif OS(MAC_OS_X)
98 return "MacIntel"_s;
99#elif OS(WINDOWS)
100 return "Win32"_s;
101#else
102 return ""_s;
103#endif
104}
105
106String NavigatorBase::appCodeName()
107{
108 return "Mozilla"_s;
109}
110
111String NavigatorBase::product()
112{
113 return WEBCORE_NAVIGATOR_PRODUCT;
114}
115
116String NavigatorBase::productSub()
117{
118 return WEBCORE_NAVIGATOR_PRODUCT_SUB;
119}
120
121String NavigatorBase::vendor()
122{
123 return WEBCORE_NAVIGATOR_VENDOR;
124}
125
126String NavigatorBase::vendorSub()
127{
128 return WEBCORE_NAVIGATOR_VENDOR_SUB;
129}
130
131String NavigatorBase::language()
132{
133 return defaultLanguage();
134}
135
136Vector<String> NavigatorBase::languages()
137{
138 // We intentionally expose only the primary language for privacy reasons.
139 return { defaultLanguage() };
140}
141
142#if ENABLE(SERVICE_WORKER)
143ServiceWorkerContainer* NavigatorBase::serviceWorkerIfExists()
144{
145 return m_serviceWorkerContainer.get();
146}
147
148ServiceWorkerContainer& NavigatorBase::serviceWorker()
149{
150 ASSERT(RuntimeEnabledFeatures::sharedFeatures().serviceWorkerEnabled());
151 if (!m_serviceWorkerContainer)
152 m_serviceWorkerContainer = std::make_unique<ServiceWorkerContainer>(scriptExecutionContext(), *this);
153 return *m_serviceWorkerContainer;
154}
155
156ExceptionOr<ServiceWorkerContainer&> NavigatorBase::serviceWorker(ScriptExecutionContext& context)
157{
158 if (is<Document>(context) && downcast<Document>(context).isSandboxed(SandboxOrigin))
159 return Exception { SecurityError, "Service Worker is disabled because the context is sandboxed and lacks the 'allow-same-origin' flag" };
160 return serviceWorker();
161}
162#endif
163
164} // namespace WebCore
165