xref: /aoo41x/main/uui/source/iahndl-cookies.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 #include "com/sun/star/ucb/HandleCookiesRequest.hpp"
29 #include "com/sun/star/ucb/XInteractionCookieHandling.hpp"
30 #include "com/sun/star/task/XInteractionRequest.hpp"
31 
32 #include "vos/mutex.hxx"
33 #include "tools/list.hxx"
34 #include "svl/httpcook.hxx"
35 #include "vcl/svapp.hxx"
36 
37 #include "cookiedg.hxx"
38 
39 #include "iahndl.hxx"
40 
41 using namespace com::sun::star;
42 
43 namespace {
44 
45 class CookieList: public List
46 {
47 public:
48     ~CookieList() SAL_THROW(());
49 };
50 
51 CookieList::~CookieList() SAL_THROW(())
52 {
53     while (Count() != 0)
54         delete static_cast< CntHTTPCookie * >(Remove(Count() - 1));
55 }
56 
57 void
58 executeCookieDialog(Window * pParent, CntHTTPCookieRequest & rRequest)
59     SAL_THROW((uno::RuntimeException))
60 {
61     try
62     {
63         vos::OGuard aGuard(Application::GetSolarMutex());
64 
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();
70     }
71     catch (std::bad_alloc const &)
72     {
73         throw uno::RuntimeException(
74                   rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("out of memory")),
75                   uno::Reference< uno::XInterface>());
76     }
77 }
78 
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))
86 {
87     CookieList aCookies;
88     for (sal_Int32 i = 0; i < rRequest.Cookies.getLength(); ++i)
89     {
90         try
91         {
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)
108             {
109             case ucb::CookiePolicy_CONFIRM:
110                 xCookie->m_nPolicy = CNTHTTP_COOKIE_POLICY_INTERACTIVE;
111                 break;
112 
113             case ucb::CookiePolicy_ACCEPT:
114                 xCookie->m_nPolicy = CNTHTTP_COOKIE_POLICY_ACCEPTED;
115                 break;
116 
117             case ucb::CookiePolicy_IGNORE:
118                 xCookie->m_nPolicy = CNTHTTP_COOKIE_POLICY_BANNED;
119                 break;
120 
121             default:
122                 OSL_ASSERT(false);
123                 break;
124             }
125             aCookies.Insert(xCookie.get(), LIST_APPEND);
126             xCookie.release();
127         }
128         catch (std::bad_alloc const &)
129         {
130             throw uno::RuntimeException(
131                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
132                                   "out of memory")),
133                 uno::Reference< uno::XInterface >());
134         }
135     }
136 
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)
145     {
146         uno::Reference< ucb::XInteractionCookieHandling >
147             xCookieHandling(rContinuations[i], uno::UNO_QUERY);
148         if (xCookieHandling.is())
149         {
150             switch (aRequest.m_nRet)
151             {
152             case CNTHTTP_COOKIE_POLICY_INTERACTIVE:
153                 xCookieHandling->
154                     setGeneralPolicy(ucb::CookiePolicy_CONFIRM);
155                 break;
156 
157             case CNTHTTP_COOKIE_POLICY_ACCEPTED:
158                 xCookieHandling->
159                     setGeneralPolicy(ucb::CookiePolicy_ACCEPT);
160                 break;
161 
162             case CNTHTTP_COOKIE_POLICY_BANNED:
163                 xCookieHandling->
164                     setGeneralPolicy(ucb::CookiePolicy_IGNORE);
165                 break;
166             }
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)
173                     {
174                     case CNTHTTP_COOKIE_POLICY_ACCEPTED:
175                         xCookieHandling->
176                             setSpecificPolicy(rRequest.Cookies[j], true);
177                         break;
178 
179                     case CNTHTTP_COOKIE_POLICY_BANNED:
180                         xCookieHandling->
181                             setSpecificPolicy(rRequest.Cookies[j], false);
182                         break;
183                     }
184             xCookieHandling->select();
185             break;
186         }
187     }
188 }
189 
190 } // namespace
191 
192 bool
193 UUIInteractionHelper::handleCookiesRequest(
194     uno::Reference< task::XInteractionRequest > const & rRequest)
195     SAL_THROW((uno::RuntimeException))
196 {
197     uno::Any aAnyRequest(rRequest->getRequest());
198 
199     ucb::HandleCookiesRequest aCookiesRequest;
200     if (aAnyRequest >>= aCookiesRequest)
201     {
202         handleCookiesRequest_(getParentProperty(),
203                               aCookiesRequest,
204                               rRequest->getContinuations());
205         return true;
206     }
207     return false;
208 }
209 
210