| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 4 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 5 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 10 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 11 |
* contributor license agreements. See the NOTICE file distributed |
| 12 |
* with this work for additional information regarding copyright |
| 13 |
* ownership. The ASF licenses this file to you under the Apache |
| 14 |
* License, Version 2.0 (the "License"); you may not use this file |
| 15 |
* except in compliance with the License. You may obtain a copy of |
| 16 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 17 |
*/ |
| 18 |
package complex.dispatches; |
| 19 |
|
| 20 |
// __________ Imports __________ |
| 21 |
|
| 22 |
// structs, const, ... |
| 23 |
import com.sun.star.beans.PropertyValue; |
| 24 |
|
| 25 |
// exceptions |
| 26 |
import com.sun.star.frame.DispatchDescriptor; |
| 27 |
import com.sun.star.frame.XDispatch; |
| 28 |
import com.sun.star.frame.XDispatchProvider; |
| 29 |
import com.sun.star.frame.XDispatchProviderInterceptor; |
| 30 |
import com.sun.star.frame.XInterceptorInfo; |
| 31 |
import com.sun.star.frame.XStatusListener; |
| 32 |
|
| 33 |
import com.sun.star.lang.XMultiServiceFactory; |
| 34 |
import com.sun.star.awt.XDataTransferProviderAccess; |
| 35 |
|
| 36 |
// helper |
| 37 |
import com.sun.star.util.URL; |
| 38 |
import com.sun.star.uno.UnoRuntime; |
| 39 |
|
| 40 |
// __________ Implementation __________ |
| 41 |
|
| 42 |
/** |
| 43 |
* implements a configurable interceptor for dispatch events. |
| 44 |
*/ |
| 45 |
public class Interceptor implements XDispatch, |
| 46 |
XDispatchProviderInterceptor, |
| 47 |
XInterceptorInfo |
| 48 |
{ |
| 49 |
/** contains the list of interception URL schema's (wildcards are allowed there!) |
| 50 |
supported by this interceptor. It can be set from outside. |
| 51 |
If no external URLs are set, the default "*" is used instead. |
| 52 |
That would have the same effect as if this implementation would not support the |
| 53 |
interface XInterceptorInfo ! |
| 54 |
*/ |
| 55 |
private String[] m_lURLs4InterceptionInfo = null; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
/** These URL's will be blocked by this interceptor. |
| 60 |
Can be set from outside. Every queryDispatch() for these |
| 61 |
set of URL's will be answered with an empty dispatch object! |
| 62 |
If no external URLs are set the default "*" is used instead. |
| 63 |
So every incoming URL will be blocked .-) |
| 64 |
*/ |
| 65 |
private String[] m_lURLs4Blocking = null; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
/** Every dispatch interceptor knows it's master and slave interceptor |
| 70 |
of the dispatch chain. These values must be stupid handled .-) |
| 71 |
They have to be set and reset in case the right interface methods are called. |
| 72 |
Nothing more. It's not allowed to dispose() it. |
| 73 |
The slave can be used inside queryDispatch() to forward requests, |
| 74 |
which are not handled by this interceptor instance. |
| 75 |
*/ |
| 76 |
private XDispatchProvider m_xSlave = null; |
| 77 |
private XDispatchProvider m_xMaster = null; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
/** counts calls of setSlave...(). |
| 82 |
So the outside API test can use this value to know if this interceptor |
| 83 |
was really added to the interceptor chain of OOo. |
| 84 |
*/ |
| 85 |
private int m_nRegistrationCount = 0; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
/** indicates if this interceptor object is currently part of the interceptor |
| 90 |
chain of OOo. Only true if a valid slave or master dispatch is set on this |
| 91 |
instance. |
| 92 |
*/ |
| 93 |
private boolean m_bIsRegistered = false; |
| 94 |
|
| 95 |
/** points to the global uno service manager. */ |
| 96 |
private XMultiServiceFactory m_xMSF = null; |
| 97 |
|
| 98 |
public Interceptor(XMultiServiceFactory xMSF) |
| 99 |
{ |
| 100 |
m_xMSF = xMSF; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
/** XInterceptorInfo */ |
| 105 |
public synchronized String[] getInterceptedURLs() |
| 106 |
{ |
| 107 |
return impl_getURLs4InterceptionInfo(); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
/** XDispatchProviderInterceptor */ |
| 113 |
public synchronized XDispatchProvider getSlaveDispatchProvider() |
| 114 |
{ |
| 115 |
System.out.println("Interceptor.getSlaveDispatchProvider() called"); |
| 116 |
return m_xSlave; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
/** XDispatchProviderInterceptor */ |
| 122 |
public synchronized XDispatchProvider getMasterDispatchProvider() |
| 123 |
{ |
| 124 |
System.out.println("Interceptor.getMasterDispatchProvider() called"); |
| 125 |
return m_xMaster; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
/** XDispatchProviderInterceptor */ |
| 131 |
public synchronized void setSlaveDispatchProvider(XDispatchProvider xSlave) |
| 132 |
{ |
| 133 |
System.out.println("Interceptor.setSlaveDispatchProvider("+xSlave+") called"); |
| 134 |
|
| 135 |
if (xSlave != null) |
| 136 |
{ |
| 137 |
++m_nRegistrationCount; |
| 138 |
m_bIsRegistered = true; |
| 139 |
} |
| 140 |
else |
| 141 |
{ |
| 142 |
m_bIsRegistered = false; |
| 143 |
} |
| 144 |
|
| 145 |
m_xSlave = xSlave; |
| 146 |
} |
| 147 |
|
| 148 |
/** XDispatchProviderInterceptor */ |
| 149 |
public synchronized void setMasterDispatchProvider(XDispatchProvider xMaster) |
| 150 |
{ |
| 151 |
System.out.println("Interceptor.setMasterDispatchProvider("+xMaster+") called"); |
| 152 |
m_xMaster = xMaster; |
| 153 |
} |
| 154 |
|
| 155 |
private XDataTransferProviderAccess m_xToolkit = null; |
| 156 |
|
| 157 |
/** A beautiful method whose only purpose is to take and release a |
| 158 |
* solar mutex. If this hangs - you can see a beautiful deadlock |
| 159 |
* when you attach your debugger to the main process. |
| 160 |
*/ |
| 161 |
private void checkNoSolarMutexHeld() |
| 162 |
{ |
| 163 |
try |
| 164 |
{ |
| 165 |
if (m_xToolkit == null) |
| 166 |
m_xToolkit = UnoRuntime.queryInterface( |
| 167 |
XDataTransferProviderAccess.class, |
| 168 |
m_xMSF.createInstance("com.sun.star.awt.Toolkit")); |
| 169 |
|
| 170 |
// A Method notable only for taking the solar mutex. |
| 171 |
System.out.println("Check solarmutex not held - if so deadlock"); |
| 172 |
m_xToolkit.getDragSource( null ); |
| 173 |
System.out.println("Solarmutex not held."); |
| 174 |
} catch (java.lang.Throwable ex) { |
| 175 |
System.out.println("Failed to create and invoke toolkit method " + ex.toString()); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
/** XDispatchProvider |
| 180 |
*/ |
| 181 |
public synchronized XDispatch queryDispatch(URL aURL, |
| 182 |
String sTargetFrameName, |
| 183 |
int nSearchFlags) |
| 184 |
{ |
| 185 |
System.out.println("Interceptor.queryDispatch('"+aURL.Complete+"', '"+sTargetFrameName+"', "+nSearchFlags+") called"); |
| 186 |
|
| 187 |
checkNoSolarMutexHeld(); |
| 188 |
|
| 189 |
if (impl_isBlockedURL(aURL.Complete)) |
| 190 |
{ |
| 191 |
System.out.println("Interceptor.queryDispatch(): URL blocked => returns NULL"); |
| 192 |
return null; |
| 193 |
} |
| 194 |
|
| 195 |
if (m_xSlave != null) |
| 196 |
{ |
| 197 |
System.out.println("Interceptor.queryDispatch(): ask slave ..."); |
| 198 |
return m_xSlave.queryDispatch(aURL, sTargetFrameName, nSearchFlags); |
| 199 |
} |
| 200 |
|
| 201 |
System.out.println("Interceptor.queryDispatch(): no idea => returns this"); |
| 202 |
return this; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
/** XDispatchProvider |
| 208 |
*/ |
| 209 |
public XDispatch[] queryDispatches(DispatchDescriptor[] lRequests) |
| 210 |
{ |
| 211 |
int i = 0; |
| 212 |
int c = lRequests.length; |
| 213 |
|
| 214 |
XDispatch[] lResults = new XDispatch[c]; |
| 215 |
for (i=0; i<c; ++i) |
| 216 |
{ |
| 217 |
lResults[i] = queryDispatch(lRequests[i].FeatureURL , |
| 218 |
lRequests[i].FrameName , |
| 219 |
lRequests[i].SearchFlags); |
| 220 |
} |
| 221 |
|
| 222 |
return lResults; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
/** XDispatch |
| 228 |
*/ |
| 229 |
public synchronized void dispatch(URL aURL , |
| 230 |
PropertyValue[] lArguments) |
| 231 |
{ |
| 232 |
System.out.println("Interceptor.dispatch('"+aURL.Complete+"') called"); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
/** XDispatch |
| 238 |
*/ |
| 239 |
public synchronized void addStatusListener(XStatusListener xListener, |
| 240 |
com.sun.star.util.URL aURL ) |
| 241 |
{ |
| 242 |
System.out.println("Interceptor.addStatusListener(..., '"+aURL.Complete+"') called"); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
/** XDispatch |
| 248 |
*/ |
| 249 |
public synchronized void removeStatusListener(XStatusListener xListener, |
| 250 |
com.sun.star.util.URL aURL ) |
| 251 |
{ |
| 252 |
System.out.println("Interceptor.removeStatusListener(..., '"+aURL.Complete+"') called"); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
public synchronized int getRegistrationCount() |
| 258 |
{ |
| 259 |
return m_nRegistrationCount; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
public synchronized boolean isRegistered() |
| 265 |
{ |
| 266 |
return m_bIsRegistered; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
/** set a new list of URL's, which should be blocked by this interceptor. |
| 271 |
(that's why it's necessary to call this impl-method before the interceptor |
| 272 |
is used at the OOo API!) |
| 273 |
*/ |
| 274 |
public synchronized void setURLs4URLs4Blocking(String[] lURLs) |
| 275 |
{ |
| 276 |
m_lURLs4Blocking = lURLs; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
/** must be used internal to access the member m_lURLs4InterceptionInfo |
| 282 |
- threadsafe |
| 283 |
- and to make sure it's initialized on demand |
| 284 |
*/ |
| 285 |
private synchronized String[] impl_getURLs4InterceptionInfo() |
| 286 |
{ |
| 287 |
if (m_lURLs4InterceptionInfo == null) |
| 288 |
{ |
| 289 |
m_lURLs4InterceptionInfo = new String[] { "*" }; |
| 290 |
} |
| 291 |
|
| 292 |
return m_lURLs4InterceptionInfo; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
/** must be used internal to access the member m_lURLs4Blocking |
| 298 |
- threadsafe |
| 299 |
- and to make sure it's initialized on demand |
| 300 |
*/ |
| 301 |
private synchronized String[] impl_getURLs4Blocking() |
| 302 |
{ |
| 303 |
if (m_lURLs4Blocking == null) |
| 304 |
{ |
| 305 |
m_lURLs4Blocking = new String[] { "*" }; |
| 306 |
} |
| 307 |
|
| 308 |
return m_lURLs4Blocking; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
private boolean impl_isBlockedURL(String sURL) |
| 313 |
{ |
| 314 |
String[] lBlockedURLs = impl_getURLs4Blocking(); |
| 315 |
int i = 0; |
| 316 |
int c = lBlockedURLs.length; |
| 317 |
|
| 318 |
for (i=0; i<c; ++i) |
| 319 |
{ |
| 320 |
if (impl_match(sURL, lBlockedURLs[i])) |
| 321 |
{ |
| 322 |
return true; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
return false; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
private boolean impl_match(String sVal1, String sVal2) |
| 332 |
{ |
| 333 |
// TODO implement wildcard match |
| 334 |
return sVal1.equals(sVal2); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |