merged tag ooo/DEV300_m102
[LibreOffice.git] / uui / source / iahndl-cookies.cxx
blobdb8da498c407454b6a96817477bb02bf0133c64c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #include "com/sun/star/ucb/HandleCookiesRequest.hpp"
29 #include "com/sun/star/ucb/XInteractionCookieHandling.hpp"
30 #include "com/sun/star/task/XInteractionRequest.hpp"
32 #include "vos/mutex.hxx"
33 #include "tools/list.hxx"
34 #include "svl/httpcook.hxx"
35 #include "vcl/svapp.hxx"
37 #include "cookiedg.hxx"
39 #include "iahndl.hxx"
41 using namespace com::sun::star;
43 namespace {
45 class CookieList: public List
47 public:
48 ~CookieList() SAL_THROW(());
51 CookieList::~CookieList() SAL_THROW(())
53 while (Count() != 0)
54 delete static_cast< CntHTTPCookie * >(Remove(Count() - 1));
57 void
58 executeCookieDialog(Window * pParent, CntHTTPCookieRequest & rRequest)
59 SAL_THROW((uno::RuntimeException))
61 try
63 vos::OGuard aGuard(Application::GetSolarMutex());
65 std::auto_ptr< ResMgr > xManager(
66 ResMgr::CreateResMgr(CREATEVERSIONRESMGR_NAME(uui)));
67 std::auto_ptr< CookiesDialog > xDialog(
68 new CookiesDialog(pParent, &rRequest, xManager.get()));
69 xDialog->Execute();
71 catch (std::bad_alloc const &)
73 throw uno::RuntimeException(
74 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("out of memory")),
75 uno::Reference< uno::XInterface>());
79 void
80 handleCookiesRequest_(
81 Window * pParent,
82 ucb::HandleCookiesRequest const & rRequest,
83 uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
84 rContinuations)
85 SAL_THROW((uno::RuntimeException))
87 CookieList aCookies;
88 for (sal_Int32 i = 0; i < rRequest.Cookies.getLength(); ++i)
90 try
92 std::auto_ptr< CntHTTPCookie > xCookie(new CntHTTPCookie);
93 xCookie->m_aName = UniString(rRequest.Cookies[i].Name);
94 xCookie->m_aValue = UniString(rRequest.Cookies[i].Value);
95 xCookie->m_aDomain = UniString(rRequest.Cookies[i].Domain);
96 xCookie->m_aPath = UniString(rRequest.Cookies[i].Path);
97 xCookie->m_aExpires
98 = DateTime(Date(rRequest.Cookies[i].Expires.Day,
99 rRequest.Cookies[i].Expires.Month,
100 rRequest.Cookies[i].Expires.Year),
101 Time(rRequest.Cookies[i].Expires.Hours,
102 rRequest.Cookies[i].Expires.Minutes,
103 rRequest.Cookies[i].Expires.Seconds,
104 rRequest.Cookies[i].Expires.HundredthSeconds));
105 xCookie->m_nFlags
106 = rRequest.Cookies[i].Secure ? CNTHTTP_COOKIE_FLAG_SECURE : 0;
107 switch (rRequest.Cookies[i].Policy)
109 case ucb::CookiePolicy_CONFIRM:
110 xCookie->m_nPolicy = CNTHTTP_COOKIE_POLICY_INTERACTIVE;
111 break;
113 case ucb::CookiePolicy_ACCEPT:
114 xCookie->m_nPolicy = CNTHTTP_COOKIE_POLICY_ACCEPTED;
115 break;
117 case ucb::CookiePolicy_IGNORE:
118 xCookie->m_nPolicy = CNTHTTP_COOKIE_POLICY_BANNED;
119 break;
121 default:
122 OSL_ASSERT(false);
123 break;
125 aCookies.Insert(xCookie.get(), LIST_APPEND);
126 xCookie.release();
128 catch (std::bad_alloc const &)
130 throw uno::RuntimeException(
131 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
132 "out of memory")),
133 uno::Reference< uno::XInterface >());
137 CntHTTPCookieRequest
138 aRequest(rRequest.URL,
139 aCookies,
140 rRequest.Request == ucb::CookieRequest_RECEIVE
141 ? CNTHTTP_COOKIE_REQUEST_RECV
142 : CNTHTTP_COOKIE_REQUEST_SEND);
143 executeCookieDialog(pParent, aRequest);
144 for (sal_Int32 i = 0; i < rContinuations.getLength(); ++i)
146 uno::Reference< ucb::XInteractionCookieHandling >
147 xCookieHandling(rContinuations[i], uno::UNO_QUERY);
148 if (xCookieHandling.is())
150 switch (aRequest.m_nRet)
152 case CNTHTTP_COOKIE_POLICY_INTERACTIVE:
153 xCookieHandling->
154 setGeneralPolicy(ucb::CookiePolicy_CONFIRM);
155 break;
157 case CNTHTTP_COOKIE_POLICY_ACCEPTED:
158 xCookieHandling->
159 setGeneralPolicy(ucb::CookiePolicy_ACCEPT);
160 break;
162 case CNTHTTP_COOKIE_POLICY_BANNED:
163 xCookieHandling->
164 setGeneralPolicy(ucb::CookiePolicy_IGNORE);
165 break;
167 for (sal_Int32 j = 0; j < rRequest.Cookies.getLength(); ++j)
168 if (rRequest.Cookies[j].Policy
169 == ucb::CookiePolicy_CONFIRM)
170 switch (static_cast< CntHTTPCookie * >(aCookies.
171 GetObject(j))->
172 m_nPolicy)
174 case CNTHTTP_COOKIE_POLICY_ACCEPTED:
175 xCookieHandling->
176 setSpecificPolicy(rRequest.Cookies[j], true);
177 break;
179 case CNTHTTP_COOKIE_POLICY_BANNED:
180 xCookieHandling->
181 setSpecificPolicy(rRequest.Cookies[j], false);
182 break;
184 xCookieHandling->select();
185 break;
190 } // namespace
192 bool
193 UUIInteractionHelper::handleCookiesRequest(
194 uno::Reference< task::XInteractionRequest > const & rRequest)
195 SAL_THROW((uno::RuntimeException))
197 uno::Any aAnyRequest(rRequest->getRequest());
199 ucb::HandleCookiesRequest aCookiesRequest;
200 if (aAnyRequest >>= aCookiesRequest)
202 handleCookiesRequest_(getParentProperty(),
203 aCookiesRequest,
204 rRequest->getContinuations());
205 return true;
207 return false;