1/*
2 * Copyright (C) 2007 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 COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WebKitDLL.h"
28#include "WebURLCredential.h"
29
30#include "WebKit.h"
31#pragma warning(push, 0)
32#include <WebCore/BString.h>
33#pragma warning(pop)
34
35using namespace WebCore;
36
37// WebURLCredential ----------------------------------------------------------------
38
39WebURLCredential::WebURLCredential(const Credential& credential)
40    : m_refCount(0)
41    , m_credential(credential)
42{
43    gClassCount++;
44    gClassNameCount.add("WebURLCredential");
45}
46
47WebURLCredential::~WebURLCredential()
48{
49    gClassCount--;
50    gClassNameCount.remove("WebURLCredential");
51}
52
53WebURLCredential* WebURLCredential::createInstance()
54{
55    WebURLCredential* instance = new WebURLCredential(Credential());
56    instance->AddRef();
57    return instance;
58}
59
60WebURLCredential* WebURLCredential::createInstance(const Credential& credential)
61{
62    WebURLCredential* instance = new WebURLCredential(credential);
63    instance->AddRef();
64    return instance;
65}
66
67// IUnknown -------------------------------------------------------------------
68
69HRESULT STDMETHODCALLTYPE WebURLCredential::QueryInterface(REFIID riid, void** ppvObject)
70{
71    *ppvObject = 0;
72    if (IsEqualGUID(riid, IID_IUnknown))
73        *ppvObject = static_cast<IUnknown*>(this);
74    else if (IsEqualGUID(riid, __uuidof(WebURLCredential)))
75        *ppvObject = static_cast<WebURLCredential*>(this);
76    else if (IsEqualGUID(riid, IID_IWebURLCredential))
77        *ppvObject = static_cast<IWebURLCredential*>(this);
78    else
79        return E_NOINTERFACE;
80
81    AddRef();
82    return S_OK;
83}
84
85ULONG STDMETHODCALLTYPE WebURLCredential::AddRef(void)
86{
87    return ++m_refCount;
88}
89
90ULONG STDMETHODCALLTYPE WebURLCredential::Release(void)
91{
92    ULONG newRef = --m_refCount;
93    if (!newRef)
94        delete(this);
95
96    return newRef;
97}
98
99// IWebURLCredential -------------------------------------------------------------------
100HRESULT STDMETHODCALLTYPE WebURLCredential::hasPassword(
101        /* [out, retval] */ BOOL* result)
102{
103    *result = m_credential.hasPassword();
104    return S_OK;
105}
106
107HRESULT STDMETHODCALLTYPE WebURLCredential::initWithUser(
108        /* [in] */ BSTR user,
109        /* [in] */ BSTR password,
110        /* [in] */ WebURLCredentialPersistence persistence)
111{
112    CredentialPersistence corePersistence = CredentialPersistenceNone;
113    switch (persistence) {
114    case WebURLCredentialPersistenceNone:
115        break;
116    case WebURLCredentialPersistenceForSession:
117        corePersistence = CredentialPersistenceForSession;
118        break;
119    case WebURLCredentialPersistencePermanent:
120        corePersistence = CredentialPersistencePermanent;
121        break;
122    default:
123        ASSERT_NOT_REACHED();
124        return E_FAIL;
125    }
126
127    m_credential = Credential(String(user, SysStringLen(user)), String(password, SysStringLen(password)), corePersistence);
128    return S_OK;
129}
130
131HRESULT STDMETHODCALLTYPE WebURLCredential::password(
132        /* [out, retval] */ BSTR* password)
133{
134    BString str = m_credential.password();
135    *password = str.release();
136    return S_OK;
137}
138
139HRESULT STDMETHODCALLTYPE WebURLCredential::persistence(
140        /* [out, retval] */ WebURLCredentialPersistence* result)
141{
142    switch (m_credential.persistence()) {
143    case CredentialPersistenceNone:
144        *result = WebURLCredentialPersistenceNone;
145        break;
146    case CredentialPersistenceForSession:
147        *result = WebURLCredentialPersistenceForSession;
148        break;
149    case CredentialPersistencePermanent:
150        *result = WebURLCredentialPersistencePermanent;
151        break;
152    default:
153        ASSERT_NOT_REACHED();
154        return E_FAIL;
155    }
156    return S_OK;
157}
158
159HRESULT STDMETHODCALLTYPE WebURLCredential::user(
160        /* [out, retval] */ BSTR* result)
161{
162    BString str = m_credential.user();
163    *result = str.release();
164    return S_OK;
165}
166
167const WebCore::Credential& WebURLCredential::credential() const
168{
169    return m_credential;
170}
171
172