| 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 | |
| 64 | namespace WebCore { |
| 65 | |
| 66 | NavigatorBase::NavigatorBase(ScriptExecutionContext* context) |
| 67 | : ContextDestructionObserver(context) |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | NavigatorBase::~NavigatorBase() = default; |
| 72 | |
| 73 | String NavigatorBase::appName() |
| 74 | { |
| 75 | return "Netscape"_s ; |
| 76 | } |
| 77 | |
| 78 | String 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 | |
| 85 | String 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) && (CPU(PPC) || CPU(PPC64)) |
| 98 | return "MacPPC"_s ; |
| 99 | #elif OS(MAC_OS_X) && (CPU(X86) || CPU(X86_64)) |
| 100 | return "MacIntel"_s ; |
| 101 | #elif OS(WINDOWS) |
| 102 | return "Win32"_s ; |
| 103 | #else |
| 104 | return ""_s ; |
| 105 | #endif |
| 106 | } |
| 107 | |
| 108 | String NavigatorBase::appCodeName() |
| 109 | { |
| 110 | return "Mozilla"_s ; |
| 111 | } |
| 112 | |
| 113 | String NavigatorBase::product() |
| 114 | { |
| 115 | return WEBCORE_NAVIGATOR_PRODUCT; |
| 116 | } |
| 117 | |
| 118 | String NavigatorBase::productSub() |
| 119 | { |
| 120 | return WEBCORE_NAVIGATOR_PRODUCT_SUB; |
| 121 | } |
| 122 | |
| 123 | String NavigatorBase::vendor() |
| 124 | { |
| 125 | return WEBCORE_NAVIGATOR_VENDOR; |
| 126 | } |
| 127 | |
| 128 | String NavigatorBase::vendorSub() |
| 129 | { |
| 130 | return WEBCORE_NAVIGATOR_VENDOR_SUB; |
| 131 | } |
| 132 | |
| 133 | String NavigatorBase::language() |
| 134 | { |
| 135 | return defaultLanguage(); |
| 136 | } |
| 137 | |
| 138 | Vector<String> NavigatorBase::languages() |
| 139 | { |
| 140 | // We intentionally expose only the primary language for privacy reasons. |
| 141 | return { defaultLanguage() }; |
| 142 | } |
| 143 | |
| 144 | #if ENABLE(SERVICE_WORKER) |
| 145 | ServiceWorkerContainer* NavigatorBase::serviceWorkerIfExists() |
| 146 | { |
| 147 | return m_serviceWorkerContainer.get(); |
| 148 | } |
| 149 | |
| 150 | ServiceWorkerContainer& NavigatorBase::serviceWorker() |
| 151 | { |
| 152 | ASSERT(RuntimeEnabledFeatures::sharedFeatures().serviceWorkerEnabled()); |
| 153 | if (!m_serviceWorkerContainer) |
| 154 | m_serviceWorkerContainer = std::make_unique<ServiceWorkerContainer>(scriptExecutionContext(), *this); |
| 155 | return *m_serviceWorkerContainer; |
| 156 | } |
| 157 | |
| 158 | ExceptionOr<ServiceWorkerContainer&> NavigatorBase::serviceWorker(ScriptExecutionContext& context) |
| 159 | { |
| 160 | if (is<Document>(context) && downcast<Document>(context).isSandboxed(SandboxOrigin)) |
| 161 | return Exception { SecurityError, "Service Worker is disabled because the context is sandboxed and lacks the 'allow-same-origin' flag" }; |
| 162 | return serviceWorker(); |
| 163 | } |
| 164 | #endif |
| 165 | |
| 166 | } // namespace WebCore |
| 167 | |