Replace uses of ArgumentEncoder/ArgumentDecoder with MessageEncoder/MessageDecoder
[WebKit-https.git] / Source / WebKit2 / WebProcess / Authentication / AuthenticationManager.cpp
1 /*
2  * Copyright (C) 2010 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 "Download.h"
30 #include "DownloadProxyMessages.h"
31 #include "MessageID.h"
32 #include "WebCoreArgumentCoders.h"
33 #include "WebFrame.h"
34 #include "WebPage.h"
35 #include "WebPageProxyMessages.h"
36 #include "WebProcess.h"
37 #include <WebCore/AuthenticationChallenge.h>
38 #include <WebCore/AuthenticationClient.h>
39
40 using namespace WebCore;
41
42 namespace WebKit {
43
44 static uint64_t generateAuthenticationChallengeID()
45 {
46     static uint64_t uniqueAuthenticationChallengeID = 1;
47     return uniqueAuthenticationChallengeID++;
48 }
49
50 AuthenticationManager& AuthenticationManager::shared()
51 {
52     static AuthenticationManager& manager = *new AuthenticationManager;
53     return manager;
54 }
55
56 AuthenticationManager::AuthenticationManager()
57 {
58     WebProcess::shared().connection()->addMessageReceiver(CoreIPC::MessageClassAuthenticationManager, this);
59 }
60
61 void AuthenticationManager::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::MessageDecoder& decoder)
62 {
63     didReceiveAuthenticationManagerMessage(connection, messageID, decoder);
64 }
65
66 void AuthenticationManager::didReceiveAuthenticationChallenge(WebFrame* frame, const AuthenticationChallenge& authenticationChallenge)
67 {
68     ASSERT(frame);
69     ASSERT(frame->page());
70
71     uint64_t challengeID = generateAuthenticationChallengeID();
72     m_challenges.set(challengeID, authenticationChallenge);    
73     
74     WebProcess::shared().connection()->send(Messages::WebPageProxy::DidReceiveAuthenticationChallenge(frame->frameID(), authenticationChallenge, challengeID), frame->page()->pageID());
75 }
76
77 void AuthenticationManager::didReceiveAuthenticationChallenge(Download* download, const AuthenticationChallenge& authenticationChallenge)
78 {
79     uint64_t challengeID = generateAuthenticationChallengeID();
80     m_challenges.set(challengeID, authenticationChallenge);
81
82     download->send(Messages::DownloadProxy::DidReceiveAuthenticationChallenge(authenticationChallenge, challengeID));
83 }
84
85 // Currently, only Mac knows how to respond to authentication challenges with certificate info.
86 #if !USE(SECURITY_FRAMEWORK)
87 bool AuthenticationManager::tryUsePlatformCertificateInfoForChallenge(const WebCore::AuthenticationChallenge&, const PlatformCertificateInfo&)
88 {
89     return false;
90 }
91 #endif
92
93 void AuthenticationManager::useCredentialForChallenge(uint64_t challengeID, const Credential& credential, const PlatformCertificateInfo& certificateInfo)
94 {
95     AuthenticationChallenge challenge = m_challenges.take(challengeID);
96     ASSERT(!challenge.isNull());
97     
98     if (tryUsePlatformCertificateInfoForChallenge(challenge, certificateInfo))
99         return;
100     
101     AuthenticationClient* coreClient = challenge.authenticationClient();
102     if (!coreClient) {
103         // This authentication challenge comes from a download.
104         Download::receivedCredential(challenge, credential);
105         return;
106         
107     }
108
109     coreClient->receivedCredential(challenge, credential);
110 }
111
112 void AuthenticationManager::continueWithoutCredentialForChallenge(uint64_t challengeID)
113 {
114     AuthenticationChallenge challenge = m_challenges.take(challengeID);
115     ASSERT(!challenge.isNull());
116     AuthenticationClient* coreClient = challenge.authenticationClient();
117     if (!coreClient) {
118         // This authentication challenge comes from a download.
119         Download::receivedRequestToContinueWithoutCredential(challenge);
120         return;
121     }
122
123     coreClient->receivedRequestToContinueWithoutCredential(challenge);
124 }
125
126 void AuthenticationManager::cancelChallenge(uint64_t challengeID)
127 {
128     AuthenticationChallenge challenge = m_challenges.take(challengeID);
129     ASSERT(!challenge.isNull());
130     AuthenticationClient* coreClient = challenge.authenticationClient();
131     if (!coreClient) {
132         // This authentication challenge comes from a download.
133         Download::receivedCancellation(challenge);
134         return;
135     }
136
137     coreClient->receivedCancellation(challenge);
138 }
139
140 } // namespace WebKit