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, CoreIPC::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    if (!credential)
66        m_connection->send(Messages::AuthenticationManager::ContinueWithoutCredentialForChallenge(m_challengeID), 0);
67    else {
68        WebCertificateInfo* certificateInfo = credential->certificateInfo();
69        PlatformCertificateInfo platformInfo = certificateInfo ? certificateInfo->platformCertificateInfo() : PlatformCertificateInfo();
70        m_connection->send(Messages::AuthenticationManager::UseCredentialForChallenge(m_challengeID, credential->core(), platformInfo), 0);
71    }
72
73    m_challengeID = 0;
74}
75
76void AuthenticationChallengeProxy::cancel()
77{
78    if (!m_challengeID)
79        return;
80
81    m_connection->send(Messages::AuthenticationManager::CancelChallenge(m_challengeID), 0);
82
83    m_challengeID = 0;
84}
85
86WebCredential* AuthenticationChallengeProxy::proposedCredential() const
87{
88    if (!m_webCredential)
89        m_webCredential = WebCredential::create(m_coreAuthenticationChallenge.proposedCredential());
90
91    return m_webCredential.get();
92}
93
94WebProtectionSpace* AuthenticationChallengeProxy::protectionSpace() const
95{
96    if (!m_webProtectionSpace)
97        m_webProtectionSpace = WebProtectionSpace::create(m_coreAuthenticationChallenge.protectionSpace());
98
99    return m_webProtectionSpace.get();
100}
101
102} // namespace WebKit
103