xref: /aoo4110/main/uui/source/iahndl-cookies.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "com/sun/star/ucb/HandleCookiesRequest.hpp"
25 #include "com/sun/star/ucb/XInteractionCookieHandling.hpp"
26 #include "com/sun/star/task/XInteractionRequest.hpp"
27 
28 #include "vos/mutex.hxx"
29 #include "tools/list.hxx"
30 #include "svl/httpcook.hxx"
31 #include "vcl/svapp.hxx"
32 
33 #include "cookiedg.hxx"
34 
35 #include "iahndl.hxx"
36 
37 using namespace com::sun::star;
38 
39 namespace {
40 
41 class CookieList: public List
42 {
43 public:
44     ~CookieList() SAL_THROW(());
45 };
46 
~CookieList()47 CookieList::~CookieList() SAL_THROW(())
48 {
49     while (Count() != 0)
50         delete static_cast< CntHTTPCookie * >(Remove(Count() - 1));
51 }
52 
53 void
executeCookieDialog(Window * pParent,CntHTTPCookieRequest & rRequest)54 executeCookieDialog(Window * pParent, CntHTTPCookieRequest & rRequest)
55     SAL_THROW((uno::RuntimeException))
56 {
57     try
58     {
59         vos::OGuard aGuard(Application::GetSolarMutex());
60 
61         std::auto_ptr< ResMgr > xManager(
62             ResMgr::CreateResMgr(CREATEVERSIONRESMGR_NAME(uui)));
63         std::auto_ptr< CookiesDialog > xDialog(
64             new CookiesDialog(pParent, &rRequest, xManager.get()));
65         xDialog->Execute();
66     }
67     catch (std::bad_alloc const &)
68     {
69         throw uno::RuntimeException(
70                   rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("out of memory")),
71                   uno::Reference< uno::XInterface>());
72     }
73 }
74 
75 void
handleCookiesRequest_(Window * pParent,ucb::HandleCookiesRequest const & rRequest,uno::Sequence<uno::Reference<task::XInteractionContinuation>> const & rContinuations)76 handleCookiesRequest_(
77     Window * pParent,
78     ucb::HandleCookiesRequest const & rRequest,
79     uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
80         rContinuations)
81     SAL_THROW((uno::RuntimeException))
82 {
83     CookieList aCookies;
84     for (sal_Int32 i = 0; i < rRequest.Cookies.getLength(); ++i)
85     {
86         try
87         {
88             std::auto_ptr< CntHTTPCookie > xCookie(new CntHTTPCookie);
89             xCookie->m_aName = UniString(rRequest.Cookies[i].Name);
90             xCookie->m_aValue = UniString(rRequest.Cookies[i].Value);
91             xCookie->m_aDomain = UniString(rRequest.Cookies[i].Domain);
92             xCookie->m_aPath = UniString(rRequest.Cookies[i].Path);
93             xCookie->m_aExpires
94                 = DateTime(Date(rRequest.Cookies[i].Expires.Day,
95                                 rRequest.Cookies[i].Expires.Month,
96                                 rRequest.Cookies[i].Expires.Year),
97                            Time(rRequest.Cookies[i].Expires.Hours,
98                                 rRequest.Cookies[i].Expires.Minutes,
99                                 rRequest.Cookies[i].Expires.Seconds,
100                                 rRequest.Cookies[i].Expires.HundredthSeconds));
101             xCookie->m_nFlags
102                 = rRequest.Cookies[i].Secure ? CNTHTTP_COOKIE_FLAG_SECURE : 0;
103             switch (rRequest.Cookies[i].Policy)
104             {
105             case ucb::CookiePolicy_CONFIRM:
106                 xCookie->m_nPolicy = CNTHTTP_COOKIE_POLICY_INTERACTIVE;
107                 break;
108 
109             case ucb::CookiePolicy_ACCEPT:
110                 xCookie->m_nPolicy = CNTHTTP_COOKIE_POLICY_ACCEPTED;
111                 break;
112 
113             case ucb::CookiePolicy_IGNORE:
114                 xCookie->m_nPolicy = CNTHTTP_COOKIE_POLICY_BANNED;
115                 break;
116 
117             default:
118                 OSL_ASSERT(false);
119                 break;
120             }
121             aCookies.Insert(xCookie.get(), LIST_APPEND);
122             xCookie.release();
123         }
124         catch (std::bad_alloc const &)
125         {
126             throw uno::RuntimeException(
127                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
128                                   "out of memory")),
129                 uno::Reference< uno::XInterface >());
130         }
131     }
132 
133     CntHTTPCookieRequest
134     aRequest(rRequest.URL,
135                  aCookies,
136                  rRequest.Request == ucb::CookieRequest_RECEIVE
137                      ? CNTHTTP_COOKIE_REQUEST_RECV
138                      : CNTHTTP_COOKIE_REQUEST_SEND);
139     executeCookieDialog(pParent, aRequest);
140     for (sal_Int32 i = 0; i < rContinuations.getLength(); ++i)
141     {
142         uno::Reference< ucb::XInteractionCookieHandling >
143             xCookieHandling(rContinuations[i], uno::UNO_QUERY);
144         if (xCookieHandling.is())
145         {
146             switch (aRequest.m_nRet)
147             {
148             case CNTHTTP_COOKIE_POLICY_INTERACTIVE:
149                 xCookieHandling->
150                     setGeneralPolicy(ucb::CookiePolicy_CONFIRM);
151                 break;
152 
153             case CNTHTTP_COOKIE_POLICY_ACCEPTED:
154                 xCookieHandling->
155                     setGeneralPolicy(ucb::CookiePolicy_ACCEPT);
156                 break;
157 
158             case CNTHTTP_COOKIE_POLICY_BANNED:
159                 xCookieHandling->
160                     setGeneralPolicy(ucb::CookiePolicy_IGNORE);
161                 break;
162             }
163             for (sal_Int32 j = 0; j < rRequest.Cookies.getLength(); ++j)
164                 if (rRequest.Cookies[j].Policy
165                     == ucb::CookiePolicy_CONFIRM)
166                     switch (static_cast< CntHTTPCookie * >(aCookies.
167                                                            GetObject(j))->
168                             m_nPolicy)
169                     {
170                     case CNTHTTP_COOKIE_POLICY_ACCEPTED:
171                         xCookieHandling->
172                             setSpecificPolicy(rRequest.Cookies[j], true);
173                         break;
174 
175                     case CNTHTTP_COOKIE_POLICY_BANNED:
176                         xCookieHandling->
177                             setSpecificPolicy(rRequest.Cookies[j], false);
178                         break;
179                     }
180             xCookieHandling->select();
181             break;
182         }
183     }
184 }
185 
186 } // namespace
187 
188 bool
handleCookiesRequest(uno::Reference<task::XInteractionRequest> const & rRequest)189 UUIInteractionHelper::handleCookiesRequest(
190     uno::Reference< task::XInteractionRequest > const & rRequest)
191     SAL_THROW((uno::RuntimeException))
192 {
193     uno::Any aAnyRequest(rRequest->getRequest());
194 
195     ucb::HandleCookiesRequest aCookiesRequest;
196     if (aAnyRequest >>= aCookiesRequest)
197     {
198         handleCookiesRequest_(getParentProperty(),
199                               aCookiesRequest,
200                               rRequest->getContinuations());
201         return true;
202     }
203     return false;
204 }
205 
206