Clean up AuthenticationChallengeProxy
[WebKit-https.git] / Source / WebKit / UIProcess / ServiceWorkerProcessProxy.cpp
1 /*
2  * Copyright (C) 2017 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 "ServiceWorkerProcessProxy.h"
28
29 #if ENABLE(SERVICE_WORKER)
30
31 #include "AuthenticationChallengeProxy.h"
32 #include "AuthenticationDecisionListener.h"
33 #include "WebCredential.h"
34 #include "WebPageGroup.h"
35 #include "WebPreferencesStore.h"
36 #include "WebProcessMessages.h"
37 #include "WebProcessPool.h"
38 #include "WebSWContextManagerConnectionMessages.h"
39 #include <WebCore/NotImplemented.h>
40 #include <WebCore/RegistrationDatabase.h>
41
42 namespace WebKit {
43 using namespace WebCore;
44
45 Ref<ServiceWorkerProcessProxy> ServiceWorkerProcessProxy::create(WebProcessPool& pool, const SecurityOriginData& securityOrigin, WebsiteDataStore& store)
46 {
47     auto proxy = adoptRef(*new ServiceWorkerProcessProxy { pool, securityOrigin, store });
48     proxy->connect();
49     return proxy;
50 }
51
52 ServiceWorkerProcessProxy::ServiceWorkerProcessProxy(WebProcessPool& pool, const SecurityOriginData& securityOrigin, WebsiteDataStore& store)
53     : WebProcessProxy { pool, store, IsPrewarmed::No }
54     , m_securityOrigin(securityOrigin)
55     , m_serviceWorkerPageID(generatePageID())
56 {
57 }
58
59 ServiceWorkerProcessProxy::~ServiceWorkerProcessProxy()
60 {
61 }
62
63 bool ServiceWorkerProcessProxy::hasRegisteredServiceWorkers(const String& serviceWorkerDirectory)
64 {
65     String registrationFile = WebCore::serviceWorkerRegistrationDatabaseFilename(serviceWorkerDirectory);
66     return WebCore::FileSystem::fileExists(registrationFile);
67 }
68
69 void ServiceWorkerProcessProxy::getLaunchOptions(ProcessLauncher::LaunchOptions& launchOptions)
70 {
71     WebProcessProxy::getLaunchOptions(launchOptions);
72
73     launchOptions.extraInitializationData.add("service-worker-process"_s, "1"_s);
74     launchOptions.extraInitializationData.add("security-origin"_s, securityOrigin().toString());
75 }
76
77 void ServiceWorkerProcessProxy::start(const WebPreferencesStore& store, std::optional<PAL::SessionID> initialSessionID)
78 {
79     send(Messages::WebProcess::EstablishWorkerContextConnectionToStorageProcess { processPool().defaultPageGroup().pageGroupID(), m_serviceWorkerPageID, store, initialSessionID.value_or(PAL::SessionID::defaultSessionID()) }, 0);
80 }
81
82 void ServiceWorkerProcessProxy::setUserAgent(const String& userAgent)
83 {
84     send(Messages::WebSWContextManagerConnection::SetUserAgent { userAgent }, 0);
85 }
86
87 void ServiceWorkerProcessProxy::updatePreferencesStore(const WebPreferencesStore& store)
88 {
89     send(Messages::WebSWContextManagerConnection::UpdatePreferencesStore { store }, 0);
90 }
91
92 void ServiceWorkerProcessProxy::didReceiveAuthenticationChallenge(uint64_t pageID, uint64_t frameID, Ref<AuthenticationChallengeProxy>&& challenge)
93 {
94     UNUSED_PARAM(pageID);
95     UNUSED_PARAM(frameID);
96
97     // FIXME: Expose an API to delegate the actual decision to the application layer.
98     auto& protectionSpace = challenge->core().protectionSpace();
99     if (protectionSpace.authenticationScheme() == WebCore::ProtectionSpaceAuthenticationSchemeServerTrustEvaluationRequested && processPool().allowsAnySSLCertificateForServiceWorker()) {
100         auto credential = WebCore::Credential("accept server trust"_s, emptyString(), WebCore::CredentialPersistenceNone);
101         challenge->listener().useCredential(credential);
102         return;
103     }
104     notImplemented();
105     challenge->listener().performDefaultHandling();
106 }
107
108 void ServiceWorkerProcessProxy::didFinishLaunching(ProcessLauncher* launcher, IPC::Connection::Identifier connectionIdentifier)
109 {
110     WebProcessProxy::didFinishLaunching(launcher, connectionIdentifier);
111
112     // Prevent App Nap for Service Worker processes.
113     // FIXME: Ideally, the Service Worker process would app nap when all its clients app nap (http://webkit.org/b/185626).
114     setProcessSuppressionEnabled(false);
115 }
116
117 } // namespace WebKit
118
119 #endif // ENABLE(SERVICE_WORKER)