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 "AuthenticationChallengeProxy.h"
28
29#include "AuthenticationDecisionListener.h"
30#include "AuthenticationManagerMessages.h"
31#include "ChildProcessProxy.h"
32#include "WebCertificateInfo.h"
33#include "WebCoreArgumentCoders.h"
34#include "WebCredential.h"
35#include "WebProcessProxy.h"
36#include "WebProtectionSpace.h"
37
38namespace WebKit {
39
40AuthenticationChallengeProxy::AuthenticationChallengeProxy(const WebCore::AuthenticationChallenge& authenticationChallenge, uint64_t challengeID, IPC::Connection* connection)
41    : m_coreAuthenticationChallenge(authenticationChallenge)
42    , m_challengeID(challengeID)
43    , m_connection(connection)
44{
45    ASSERT(m_challengeID);
46    m_listener = AuthenticationDecisionListener::create(this);
47}
48
49AuthenticationChallengeProxy::~AuthenticationChallengeProxy()
50{
51    // If an outstanding AuthenticationChallengeProxy is being destroyed even though it hasn't been responded to yet,
52    // we cancel it here so the process isn't waiting for an answer forever.
53    if (m_challengeID)
54        m_connection->send(Messages::AuthenticationManager::CancelChallenge(m_challengeID), 0);
55
56    if (m_listener)
57        m_listener->detachChallenge();
58}
59
60void AuthenticationChallengeProxy::useCredential(WebCredential* credential)
61{
62    if (!m_challengeID)
63        return;
64
65    uint64_t challengeID = m_challengeID;
66    m_challengeID = 0;
67
68    if (!credential) {
69        m_connection->send(Messages::AuthenticationManager::ContinueWithoutCredentialForChallenge(challengeID), 0);
70        return;
71    }
72
73    WebCore::CertificateInfo certificateInfo;
74    if (credential->certificateInfo())
75        certificateInfo = credential->certificateInfo()->certificateInfo();
76
77    m_connection->send(Messages::AuthenticationManager::UseCredentialForChallenge(challengeID, credential->credential(), certificateInfo), 0);
78}
79
80void AuthenticationChallengeProxy::cancel()
81{
82    if (!m_challengeID)
83        return;
84
85    m_connection->send(Messages::AuthenticationManager::CancelChallenge(m_challengeID), 0);
86
87    m_challengeID = 0;
88}
89
90void AuthenticationChallengeProxy::performDefaultHandling()
91{
92    if (!m_challengeID)
93        return;
94
95    m_connection->send(Messages::AuthenticationManager::PerformDefaultHandling(m_challengeID), 0);
96
97    m_challengeID = 0;
98}
99
100void AuthenticationChallengeProxy::rejectProtectionSpaceAndContinue()
101{
102    if (!m_challengeID)
103        return;
104
105    m_connection->send(Messages::AuthenticationManager::RejectProtectionSpaceAndContinue(m_challengeID), 0);
106
107    m_challengeID = 0;
108}
109
110WebCredential* AuthenticationChallengeProxy::proposedCredential() const
111{
112    if (!m_webCredential)
113        m_webCredential = WebCredential::create(m_coreAuthenticationChallenge.proposedCredential());
114
115    return m_webCredential.get();
116}
117
118WebProtectionSpace* AuthenticationChallengeProxy::protectionSpace() const
119{
120    if (!m_webProtectionSpace)
121        m_webProtectionSpace = WebProtectionSpace::create(m_coreAuthenticationChallenge.protectionSpace());
122
123    return m_webProtectionSpace.get();
124}
125
126} // namespace WebKit
127