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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_desktop.hxx"
26 
27 #include <cppuhelper/implbase3.hxx>
28 
29 #include "comphelper/servicedecl.hxx"
30 
31 #include "com/sun/star/deployment/UpdateInformationProvider.hpp"
32 #include "com/sun/star/deployment/XPackage.hpp"
33 #include "com/sun/star/deployment/XPackageInformationProvider.hpp"
34 #include "com/sun/star/deployment/ExtensionManager.hpp"
35 #include "com/sun/star/deployment/XUpdateInformationProvider.hpp"
36 #include "com/sun/star/lang/XServiceInfo.hpp"
37 #include "com/sun/star/registry/XRegistryKey.hpp"
38 #include "com/sun/star/task/XAbortChannel.hpp"
39 #include "com/sun/star/uno/XComponentContext.hpp"
40 #include "com/sun/star/ucb/XCommandEnvironment.hpp"
41 #include "com/sun/star/xml/dom/XElement.hpp"
42 #include "com/sun/star/xml/dom/XNode.hpp"
43 
44 #include "com/sun/star/uno/Reference.hxx"
45 #include "rtl/ustring.hxx"
46 #include "ucbhelper/content.hxx"
47 
48 #include "dp_dependencies.hxx"
49 #include "dp_descriptioninfoset.hxx"
50 #include "dp_identifier.hxx"
51 #include "dp_version.hxx"
52 #include "dp_misc.h"
53 #include "dp_update.hxx"
54 
55 namespace beans      = com::sun::star::beans ;
56 namespace deployment = com::sun::star::deployment ;
57 namespace lang       = com::sun::star::lang ;
58 namespace registry   = com::sun::star::registry ;
59 namespace task       = com::sun::star::task ;
60 namespace css_ucb    = com::sun::star::ucb ;
61 namespace uno        = com::sun::star::uno ;
62 namespace xml = com::sun::star::xml ;
63 
64 #define UNISTRING(s) rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(s))
65 
66 namespace dp_info {
67 
68 class PackageInformationProvider :
69         public ::cppu::WeakImplHelper1< deployment::XPackageInformationProvider >
70 
71 {
72     public:
73                  PackageInformationProvider( uno::Reference< uno::XComponentContext >const& xContext);
74     virtual     ~PackageInformationProvider();
75 
76     static uno::Sequence< rtl::OUString > getServiceNames();
77     static rtl::OUString getImplName();
78 
79     // XPackageInformationProvider
80     virtual rtl::OUString SAL_CALL getPackageLocation( const rtl::OUString& extensionId )
81         throw ( uno::RuntimeException );
82     virtual uno::Sequence< uno::Sequence< rtl::OUString > > SAL_CALL isUpdateAvailable( const rtl::OUString& extensionId )
83         throw ( uno::RuntimeException );
84     virtual uno::Sequence< uno::Sequence< rtl::OUString > > SAL_CALL getExtensionList()
85         throw ( uno::RuntimeException );
86 //---------
87 private:
88 
89     uno::Reference< uno::XComponentContext> mxContext;
90 
91     rtl::OUString getPackageLocation( const rtl::OUString& repository,
92                                       const rtl::OUString& _sExtensionId );
93 
94     uno::Reference< deployment::XUpdateInformationProvider > mxUpdateInformation;
95 };
96 
97 //------------------------------------------------------------------------------
98 
PackageInformationProvider(uno::Reference<uno::XComponentContext> const & xContext)99 PackageInformationProvider::PackageInformationProvider( uno::Reference< uno::XComponentContext > const& xContext) :
100     mxContext( xContext ),
101     mxUpdateInformation( deployment::UpdateInformationProvider::create( xContext ) )
102 {
103 }
104 
105 //------------------------------------------------------------------------------
106 
~PackageInformationProvider()107 PackageInformationProvider::~PackageInformationProvider()
108 {
109 }
110 
111 //------------------------------------------------------------------------------
getPackageLocation(const rtl::OUString & repository,const rtl::OUString & _rExtensionId)112 rtl::OUString PackageInformationProvider::getPackageLocation(
113     const rtl::OUString & repository,
114     const rtl::OUString& _rExtensionId )
115 {
116     rtl::OUString aLocationURL;
117     uno::Reference<deployment::XExtensionManager> xManager =
118         deployment::ExtensionManager::get(mxContext);
119 
120     if ( xManager.is() )
121     {
122         const uno::Sequence< uno::Reference< deployment::XPackage > > packages(
123                 xManager->getDeployedExtensions(
124                     repository,
125                     uno::Reference< task::XAbortChannel >(),
126                     uno::Reference< css_ucb::XCommandEnvironment > () ) );
127 
128         for ( int pos = packages.getLength(); pos--; )
129         {
130             try
131             {
132                 const rtl::OUString aName = packages[ pos ]->getName();
133                 const beans::Optional< rtl::OUString > aID = packages[ pos ]->getIdentifier();
134                 if ( aID.IsPresent && aID.Value.compareTo( _rExtensionId ) == 0 )
135                 {
136                     aLocationURL = packages[ pos ]->getURL();
137                     break;
138                 }
139             }
140             catch ( uno::RuntimeException & ) {}
141         }
142     }
143 
144     return aLocationURL;
145 }
146 
147 //------------------------------------------------------------------------------
148 //------------------------------------------------------------------------------
149 //------------------------------------------------------------------------------
150 
151 rtl::OUString SAL_CALL
getPackageLocation(const rtl::OUString & _sExtensionId)152 PackageInformationProvider::getPackageLocation( const rtl::OUString& _sExtensionId )
153     throw ( uno::RuntimeException )
154 {
155     rtl::OUString aLocationURL = getPackageLocation( UNISTRING("user"), _sExtensionId );
156 
157     if ( aLocationURL.getLength() == 0 )
158     {
159         aLocationURL = getPackageLocation( UNISTRING("shared"), _sExtensionId );
160     }
161     if ( aLocationURL.getLength() == 0 )
162     {
163         aLocationURL = getPackageLocation( UNISTRING("bundled"), _sExtensionId );
164     }
165     if ( aLocationURL.getLength() == 0 )
166     {
167         aLocationURL = getPackageLocation( UNISTRING("bundled_prereg"), _sExtensionId );
168     }
169     if ( aLocationURL.getLength() )
170     {
171         ::ucbhelper::Content aContent( aLocationURL, NULL );
172         aLocationURL = aContent.getURL();
173     }
174     return aLocationURL;
175 }
176 
177 //------------------------------------------------------------------------------
178 
179 uno::Sequence< uno::Sequence< rtl::OUString > > SAL_CALL
isUpdateAvailable(const rtl::OUString & _sExtensionId)180 PackageInformationProvider::isUpdateAvailable( const rtl::OUString& _sExtensionId )
181     throw ( uno::RuntimeException )
182 {
183     uno::Sequence< uno::Sequence< rtl::OUString > > aList;
184 
185     uno::Reference<deployment::XExtensionManager> extMgr =
186         deployment::ExtensionManager::get(mxContext);
187 
188     if (!extMgr.is())
189     {
190         OSL_ASSERT(0);
191         return aList;
192     }
193     std::vector<std::pair<uno::Reference<deployment::XPackage>, uno::Any > > errors;
194     dp_misc::UpdateInfoMap updateInfoMap;
195     if (_sExtensionId.getLength())
196     {
197         std::vector<uno::Reference<deployment::XPackage> > vecExtensions;
198         uno::Reference<deployment::XPackage> extension;
199         try
200         {
201             extension = dp_misc::getExtensionWithHighestVersion(
202                 extMgr->getExtensionsWithSameIdentifier(
203                     _sExtensionId, _sExtensionId, uno::Reference<css_ucb::XCommandEnvironment>()));
204             vecExtensions.push_back(extension);
205         }
206         catch (lang::IllegalArgumentException &)
207         {
208             OSL_ASSERT(0);
209         }
210         updateInfoMap = dp_misc::getOnlineUpdateInfos(
211             mxContext, extMgr, mxUpdateInformation, &vecExtensions, errors);
212     }
213     else
214     {
215         updateInfoMap = dp_misc::getOnlineUpdateInfos(
216             mxContext, extMgr, mxUpdateInformation, NULL, errors);
217     }
218 
219     int nCount = 0;
220     for (dp_misc::UpdateInfoMap::iterator i(updateInfoMap.begin()); i != updateInfoMap.end(); i++)
221     {
222         dp_misc::UpdateInfo const & info = i->second;
223 
224         rtl::OUString sOnlineVersion;
225         if (info.info.is())
226         {
227             // check, if there are unsatisfied dependencies and ignore this online update
228             dp_misc::DescriptionInfoset infoset(mxContext, info.info);
229             uno::Sequence< uno::Reference< xml::dom::XElement > >
230                 ds( dp_misc::Dependencies::check( infoset ) );
231             if ( ! ds.getLength() )
232                 sOnlineVersion = info.version;
233         }
234 
235         rtl::OUString sVersionUser;
236         rtl::OUString sVersionShared;
237         rtl::OUString sVersionBundled;
238         uno::Sequence< uno::Reference< deployment::XPackage> > extensions;
239         try {
240             extensions = extMgr->getExtensionsWithSameIdentifier(
241                 dp_misc::getIdentifier(info.extension), info.extension->getName(),
242                 uno::Reference<css_ucb::XCommandEnvironment>());
243         } catch (lang::IllegalArgumentException& ) {
244             OSL_ASSERT(0);
245         }
246         OSL_ASSERT(extensions.getLength() == 3);
247         if (extensions[0].is() )
248             sVersionUser = extensions[0]->getVersion();
249         if (extensions[1].is() )
250             sVersionShared = extensions[1]->getVersion();
251         if (extensions[2].is() )
252             sVersionBundled = extensions[2]->getVersion();
253 
254         bool bSharedReadOnly = extMgr->isReadOnlyRepository(OUSTR("shared"));
255 
256         dp_misc::UPDATE_SOURCE sourceUser = dp_misc::isUpdateUserExtension(
257             bSharedReadOnly, sVersionUser, sVersionShared, sVersionBundled, sOnlineVersion);
258         dp_misc::UPDATE_SOURCE sourceShared = dp_misc::isUpdateSharedExtension(
259             bSharedReadOnly, sVersionShared, sVersionBundled, sOnlineVersion);
260 
261         rtl::OUString updateVersionUser;
262         rtl::OUString updateVersionShared;
263         if (sourceUser != dp_misc::UPDATE_SOURCE_NONE)
264             updateVersionUser = dp_misc::getHighestVersion(
265                 rtl::OUString(), sVersionShared, sVersionBundled, sOnlineVersion);
266         if (sourceShared  != dp_misc::UPDATE_SOURCE_NONE)
267             updateVersionShared = dp_misc::getHighestVersion(
268                 rtl::OUString(), rtl::OUString(), sVersionBundled, sOnlineVersion);
269         rtl::OUString updateVersion;
270         if (dp_misc::compareVersions(updateVersionUser, updateVersionShared) == dp_misc::GREATER)
271             updateVersion = updateVersionUser;
272         else
273             updateVersion = updateVersionShared;
274         if (updateVersion.getLength())
275         {
276 
277             rtl::OUString aNewEntry[2];
278             aNewEntry[0] = i->first;
279             aNewEntry[1] = updateVersion;
280             aList.realloc( ++nCount );
281             aList[ nCount-1 ] = ::uno::Sequence< rtl::OUString >( aNewEntry, 2 );
282         }
283     }
284     return aList;
285 }
286 
287 //------------------------------------------------------------------------------
getExtensionList()288 uno::Sequence< uno::Sequence< rtl::OUString > > SAL_CALL PackageInformationProvider::getExtensionList()
289     throw ( uno::RuntimeException )
290 {
291     const uno::Reference<deployment::XExtensionManager> mgr =
292         deployment::ExtensionManager::get(mxContext);
293 
294     if (!mgr.is())
295         return uno::Sequence< uno::Sequence< rtl::OUString > >();
296 
297     const uno::Sequence< uno::Sequence< uno::Reference<deployment::XPackage > > >
298         allExt =  mgr->getAllExtensions(
299             uno::Reference< task::XAbortChannel >(),
300             uno::Reference< css_ucb::XCommandEnvironment > () );
301 
302     uno::Sequence< uno::Sequence< rtl::OUString > > retList;
303 
304     sal_Int32 cAllIds = allExt.getLength();
305     retList.realloc(cAllIds);
306 
307     for (sal_Int32 i = 0; i < cAllIds; i++)
308     {
309         //The inner sequence contains extensions with the same identifier from
310         //all the different repositories, that is user, share, bundled.
311         const uno::Sequence< uno::Reference< deployment::XPackage > > &
312             seqExtension = allExt[i];
313         sal_Int32 cExt = seqExtension.getLength();
314         OSL_ASSERT(cExt == 3);
315         for (sal_Int32 j = 0; j < cExt; j++)
316         {
317             //ToDo according to the old code the first found extenions is used
318             //even if another one with the same id has a better version.
319             uno::Reference< deployment::XPackage > const & xExtension( seqExtension[j] );
320             if (xExtension.is())
321             {
322                 rtl::OUString aNewEntry[2];
323                 aNewEntry[0] = dp_misc::getIdentifier(xExtension);
324                 aNewEntry[1] = xExtension->getVersion();
325                 retList[i] = ::uno::Sequence< rtl::OUString >( aNewEntry, 2 );
326                 break;
327             }
328         }
329     }
330     return retList;
331 }
332 
333 
334 //------------------------------------------------------------------------------
335 //------------------------------------------------------------------------------
336 //------------------------------------------------------------------------------
337 
338 namespace sdecl = comphelper::service_decl;
339 sdecl::class_<PackageInformationProvider> servicePIP;
340 extern sdecl::ServiceDecl const serviceDecl(
341     servicePIP,
342     // a private one:
343     "com.sun.star.comp.deployment.PackageInformationProvider",
344     "com.sun.star.comp.deployment.PackageInformationProvider" );
345 
346 //------------------------------------------------------------------------------
singleton_entries(uno::Reference<registry::XRegistryKey> const & xRegistryKey)347 bool singleton_entries(
348     uno::Reference< registry::XRegistryKey > const & xRegistryKey )
349 {
350     try {
351         uno::Reference< registry::XRegistryKey > xKey(
352             xRegistryKey->createKey(
353                 serviceDecl.getImplementationName() +
354                 // xxx todo: use future generated function to get singleton name
355                 UNISTRING("/UNO/SINGLETONS/"
356                       "com.sun.star.deployment.PackageInformationProvider") ) );
357         xKey->setStringValue( serviceDecl.getSupportedServiceNames()[0] );
358         return true;
359     }
360     catch (registry::InvalidRegistryException & exc) {
361         (void) exc; // avoid warnings
362         OSL_ENSURE( 0, ::rtl::OUStringToOString(
363                         exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
364         return false;
365     }
366 }
367 
368 } // namespace dp_info
369 
370 
371