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 "WebCoreArgumentCoders.h"
32#include "WebCredential.h"
33#include "WebPageProxy.h"
34#include "WebProcessProxy.h"
35#include "WebProtectionSpace.h"
36
37namespace WebKit {
38
39AuthenticationChallengeProxy::AuthenticationChallengeProxy(const WebCore::AuthenticationChallenge& authenticationChallenge, uint64_t challengeID, WebProcessProxy* process)
40    : m_coreAuthenticationChallenge(authenticationChallenge)
41    , m_challengeID(challengeID)
42    , m_process(process)
43{
44    ASSERT(m_challengeID);
45    m_listener = AuthenticationDecisionListener::create(this);
46}
47
48AuthenticationChallengeProxy::~AuthenticationChallengeProxy()
49{
50    // If an outstanding AuthenticationChallengeProxy is being destroyed even though it hasn't been responded to yet,
51    // we cancel it here so the WebProcess isn't waiting for an answer forever.
52    if (m_challengeID)
53        m_process->send(Messages::AuthenticationManager::CancelChallenge(m_challengeID), 0);
54
55    if (m_listener)
56        m_listener->detachChallenge();
57}
58
59void AuthenticationChallengeProxy::useCredential(WebCredential* credential)
60{
61    if (!m_challengeID)
62        return;
63
64    if (!credential)
65        m_process->send(Messages::AuthenticationManager::ContinueWithoutCredentialForChallenge(m_challengeID), 0);
66    else
67        m_process->send(Messages::AuthenticationManager::UseCredentialForChallenge(m_challengeID, credential->core()), 0);
68
69    m_challengeID = 0;
70}
71
72void AuthenticationChallengeProxy::cancel()
73{
74    if (!m_challengeID)
75        return;
76
77    m_process->send(Messages::AuthenticationManager::CancelChallenge(m_challengeID), 0);
78
79    m_challengeID = 0;
80}
81
82WebCredential* AuthenticationChallengeProxy::proposedCredential() const
83{
84    if (!m_webCredential)
85        m_webCredential = WebCredential::create(m_coreAuthenticationChallenge.proposedCredential());
86
87    return m_webCredential.get();
88}
89
90WebProtectionSpace* AuthenticationChallengeProxy::protectionSpace() const
91{
92    if (!m_webProtectionSpace)
93        m_webProtectionSpace = WebProtectionSpace::create(m_coreAuthenticationChallenge.protectionSpace());
94
95    return m_webProtectionSpace.get();
96}
97
98} // namespace WebKit
99