<rdar://problem/13194263> Crashes in NetworkProcess due to threading issues
[WebKit-https.git] / Source / WebKit2 / Shared / Authentication / AuthenticationManager.cpp
1 /*
2  * Copyright (C) 2010, 2013 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 "AuthenticationManager.h"
28
29 #include "AuthenticationManagerMessages.h"
30 #include "ChildProcess.h"
31 #include "Download.h"
32 #include "DownloadProxyMessages.h"
33 #include "WebCoreArgumentCoders.h"
34 #include "WebFrame.h"
35 #include "WebPage.h"
36 #include "WebPageProxyMessages.h"
37 #include <WebCore/AuthenticationChallenge.h>
38 #include <WebCore/AuthenticationClient.h>
39
40 #if ENABLE(NETWORK_PROCESS)
41 #include "NetworkProcessProxyMessages.h"
42 #endif
43
44 using namespace WebCore;
45
46 namespace WebKit {
47
48 static uint64_t generateAuthenticationChallengeID()
49 {
50     ASSERT(isMainThread());
51
52     static int64_t uniqueAuthenticationChallengeID;
53     return ++uniqueAuthenticationChallengeID;
54 }
55
56 const char* AuthenticationManager::supplementName()
57 {
58     return "AuthenticationManager";
59 }
60
61 AuthenticationManager::AuthenticationManager(ChildProcess* process)
62     : m_process(process)
63 {
64     m_process->addMessageReceiver(Messages::AuthenticationManager::messageReceiverName(), this);
65 }
66
67 uint64_t AuthenticationManager::establishIdentifierForChallenge(const WebCore::AuthenticationChallenge& authenticationChallenge)
68 {
69     ASSERT(isMainThread());
70
71     uint64_t challengeID = generateAuthenticationChallengeID();
72     m_challenges.set(challengeID, authenticationChallenge);
73     return challengeID;
74 }
75
76 void AuthenticationManager::didReceiveAuthenticationChallenge(WebFrame* frame, const AuthenticationChallenge& authenticationChallenge)
77 {
78     ASSERT(frame);
79     ASSERT(frame->page());
80     
81     m_process->send(Messages::WebPageProxy::DidReceiveAuthenticationChallenge(frame->frameID(), authenticationChallenge, establishIdentifierForChallenge(authenticationChallenge)), frame->page()->pageID());
82 }
83
84 #if ENABLE(NETWORK_PROCESS)
85 void AuthenticationManager::didReceiveAuthenticationChallenge(uint64_t pageID, uint64_t frameID, const AuthenticationChallenge& authenticationChallenge)
86 {
87     ASSERT(pageID);
88     ASSERT(frameID);
89     
90     m_process->send(Messages::NetworkProcessProxy::DidReceiveAuthenticationChallenge(pageID, frameID, authenticationChallenge, establishIdentifierForChallenge(authenticationChallenge)));
91 }
92 #endif
93
94 void AuthenticationManager::didReceiveAuthenticationChallenge(Download* download, const AuthenticationChallenge& authenticationChallenge)
95 {
96     download->send(Messages::DownloadProxy::DidReceiveAuthenticationChallenge(authenticationChallenge, establishIdentifierForChallenge(authenticationChallenge)));
97 }
98
99 // Currently, only Mac knows how to respond to authentication challenges with certificate info.
100 #if !USE(SECURITY_FRAMEWORK)
101 bool AuthenticationManager::tryUsePlatformCertificateInfoForChallenge(const WebCore::AuthenticationChallenge&, const PlatformCertificateInfo&)
102 {
103     return false;
104 }
105 #endif
106
107 void AuthenticationManager::useCredentialForChallenge(uint64_t challengeID, const Credential& credential, const PlatformCertificateInfo& certificateInfo)
108 {
109     ASSERT(isMainThread());
110
111     AuthenticationChallenge challenge = m_challenges.take(challengeID);
112     ASSERT(!challenge.isNull());
113     
114     if (tryUsePlatformCertificateInfoForChallenge(challenge, certificateInfo))
115         return;
116     
117     AuthenticationClient* coreClient = challenge.authenticationClient();
118     if (!coreClient) {
119         // This authentication challenge comes from a download.
120         Download::receivedCredential(challenge, credential);
121         return;
122     }
123
124     coreClient->receivedCredential(challenge, credential);
125 }
126
127 void AuthenticationManager::continueWithoutCredentialForChallenge(uint64_t challengeID)
128 {
129     ASSERT(isMainThread());
130
131     AuthenticationChallenge challenge = m_challenges.take(challengeID);
132     ASSERT(!challenge.isNull());
133     AuthenticationClient* coreClient = challenge.authenticationClient();
134     if (!coreClient) {
135         // This authentication challenge comes from a download.
136         Download::receivedRequestToContinueWithoutCredential(challenge);
137         return;
138     }
139
140     coreClient->receivedRequestToContinueWithoutCredential(challenge);
141 }
142
143 void AuthenticationManager::cancelChallenge(uint64_t challengeID)
144 {
145     ASSERT(isMainThread());
146
147     AuthenticationChallenge challenge = m_challenges.take(challengeID);
148     ASSERT(!challenge.isNull());
149     AuthenticationClient* coreClient = challenge.authenticationClient();
150     if (!coreClient) {
151         // This authentication challenge comes from a download.
152         Download::receivedCancellation(challenge);
153         return;
154     }
155
156     coreClient->receivedCancellation(challenge);
157 }
158
159 } // namespace WebKit