Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.print; |
| 18 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.ServiceConnection; |
| 23 | import android.os.Binder; |
| 24 | import android.os.Handler; |
| 25 | import android.os.IBinder; |
| 26 | import android.os.Looper; |
| 27 | import android.os.Message; |
| 28 | import android.os.ParcelFileDescriptor; |
| 29 | import android.os.RemoteException; |
| 30 | import android.os.UserHandle; |
| 31 | import android.print.IPrinterDiscoveryObserver; |
| 32 | import android.print.PrintJobInfo; |
| 33 | import android.print.PrintManager; |
| 34 | import android.print.PrinterId; |
| 35 | import android.print.PrinterInfo; |
| 36 | import android.printservice.IPrintService; |
| 37 | import android.printservice.IPrintServiceClient; |
| 38 | import android.util.Slog; |
| 39 | |
| 40 | import java.lang.ref.WeakReference; |
| 41 | import java.util.ArrayList; |
| 42 | import java.util.List; |
| 43 | |
| 44 | /** |
| 45 | * This class represents a remote print service. It abstracts away the binding |
| 46 | * and unbinding from the remote implementation. Clients can call methods of |
| 47 | * this class without worrying about when and how to bind/unbind. |
| 48 | */ |
| 49 | final class RemotePrintService { |
| 50 | |
| 51 | private static final String LOG_TAG = "RemotePrintService"; |
| 52 | |
| 53 | private static final boolean DEBUG = true; |
| 54 | |
| 55 | private final Context mContext; |
| 56 | |
| 57 | private final ComponentName mComponentName; |
| 58 | |
| 59 | private final Intent mIntent; |
| 60 | |
| 61 | private final RemotePrintSpooler mSpooler; |
| 62 | |
| 63 | private final int mUserId; |
| 64 | |
| 65 | private final List<Runnable> mPendingCommands = new ArrayList<Runnable>(); |
| 66 | |
| 67 | private final ServiceConnection mServiceConnection = new RemoteServiceConneciton(); |
| 68 | |
| 69 | private final RemotePrintServiceClient mPrintServiceClient; |
| 70 | |
| 71 | private final Handler mHandler; |
| 72 | |
| 73 | private IPrintService mPrintService; |
| 74 | |
| 75 | private boolean mBinding; |
| 76 | |
| 77 | private boolean mDestroyed; |
| 78 | |
| 79 | public RemotePrintService(Context context, ComponentName componentName, int userId, |
| 80 | RemotePrintSpooler spooler) { |
| 81 | mContext = context; |
| 82 | mComponentName = componentName; |
| 83 | mIntent = new Intent().setComponent(mComponentName); |
| 84 | mUserId = userId; |
| 85 | mSpooler = spooler; |
| 86 | mHandler = new MyHandler(context.getMainLooper()); |
| 87 | mPrintServiceClient = new RemotePrintServiceClient(this); |
| 88 | } |
| 89 | |
| 90 | public void destroy() { |
| 91 | mHandler.sendEmptyMessage(MyHandler.MSG_DESTROY); |
| 92 | } |
| 93 | |
| 94 | private void handleDestroy() { |
| 95 | throwIfDestroyed(); |
| 96 | ensureUnbound(); |
| 97 | mDestroyed = true; |
| 98 | } |
| 99 | |
| 100 | public void onAllPrintJobsHandled() { |
| 101 | mHandler.sendEmptyMessage(MyHandler.MSG_ALL_PRINT_JOBS_HANDLED); |
| 102 | } |
| 103 | |
| 104 | private void handleOnAllPrintJobsHandled() { |
| 105 | throwIfDestroyed(); |
| 106 | if (isBound()) { |
| 107 | if (DEBUG) { |
| 108 | Slog.i(LOG_TAG, "[user: " + mUserId + "] handleOnAllPrintJobsHandled"); |
| 109 | } |
| 110 | // If bound and all the work is completed, then unbind. |
| 111 | ensureUnbound(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | public void onRequestCancelPrintJob(PrintJobInfo printJob) { |
| 116 | mHandler.obtainMessage(MyHandler.MSG_REQUEST_CANCEL_PRINT_JOB, |
| 117 | printJob).sendToTarget(); |
| 118 | } |
| 119 | |
| 120 | private void handleOnRequestCancelPrintJob(final PrintJobInfo printJob) { |
| 121 | throwIfDestroyed(); |
| 122 | // If we are not bound, then we have no print jobs to handle |
| 123 | // which means that there are no print jobs to be cancelled. |
| 124 | if (isBound()) { |
| 125 | if (DEBUG) { |
| 126 | Slog.i(LOG_TAG, "[user: " + mUserId + "] handleOnRequestCancelPrintJob()"); |
| 127 | } |
| 128 | try { |
| 129 | mPrintService.requestCancelPrintJob(printJob); |
| 130 | } catch (RemoteException re) { |
| 131 | Slog.e(LOG_TAG, "Error canceling pring job.", re); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | public void onPrintJobQueued(PrintJobInfo printJob) { |
| 137 | mHandler.obtainMessage(MyHandler.MSG_PRINT_JOB_QUEUED, |
| 138 | printJob).sendToTarget(); |
| 139 | } |
| 140 | |
| 141 | private void handleOnPrintJobQueued(final PrintJobInfo printJob) { |
| 142 | throwIfDestroyed(); |
| 143 | if (!isBound()) { |
| 144 | ensureBound(); |
| 145 | mPendingCommands.add(new Runnable() { |
| 146 | @Override |
| 147 | public void run() { |
| 148 | handleOnPrintJobQueued(printJob); |
| 149 | } |
| 150 | }); |
| 151 | } else { |
| 152 | if (DEBUG) { |
| 153 | Slog.i(LOG_TAG, "[user: " + mUserId + "] handleOnPrintJobQueued()"); |
| 154 | } |
| 155 | try { |
| 156 | mPrintService.onPrintJobQueued(printJob); |
| 157 | } catch (RemoteException re) { |
| 158 | Slog.e(LOG_TAG, "Error announcing queued pring job.", re); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | public void onStartPrinterDiscovery(IPrinterDiscoveryObserver observer) { |
| 164 | mHandler.obtainMessage(MyHandler.MSG_START_PRINTER_DISCOVERY, observer).sendToTarget(); |
| 165 | } |
| 166 | |
| 167 | private void handleOnStartPrinterDiscovery(final IPrinterDiscoveryObserver observer) { |
| 168 | throwIfDestroyed(); |
| 169 | if (!isBound()) { |
| 170 | ensureBound(); |
| 171 | mPendingCommands.add(new Runnable() { |
| 172 | @Override |
| 173 | public void run() { |
| 174 | handleOnStartPrinterDiscovery(observer); |
| 175 | } |
| 176 | }); |
| 177 | } else { |
| 178 | if (DEBUG) { |
| 179 | Slog.i(LOG_TAG, "[user: " + mUserId + "] onStartPrinterDiscovery()"); |
| 180 | } |
| 181 | try { |
| 182 | mPrintService.startPrinterDiscovery(observer); |
| 183 | } catch (RemoteException re) { |
| 184 | Slog.e(LOG_TAG, "Error announcing start printer dicovery.", re); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | public void onStopPrinterDiscovery() { |
| 190 | mHandler.sendEmptyMessage(MyHandler.MSG_STOP_PRINTER_DISCOVERY); |
| 191 | } |
| 192 | |
| 193 | private void handleStopPrinterDiscovery() { |
| 194 | throwIfDestroyed(); |
| 195 | if (!isBound()) { |
| 196 | ensureBound(); |
| 197 | mPendingCommands.add(new Runnable() { |
| 198 | @Override |
| 199 | public void run() { |
| 200 | handleStopPrinterDiscovery(); |
| 201 | } |
| 202 | }); |
| 203 | } else { |
| 204 | if (DEBUG) { |
| 205 | Slog.i(LOG_TAG, "[user: " + mUserId + "] onStopPrinterDiscovery()"); |
| 206 | } |
| 207 | try { |
| 208 | mPrintService.stopPrinterDiscovery(); |
| 209 | } catch (RemoteException re) { |
| 210 | Slog.e(LOG_TAG, "Error announcing stop printer dicovery.", re); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | private boolean isBound() { |
| 216 | return mPrintService != null; |
| 217 | } |
| 218 | |
| 219 | private void ensureBound() { |
| 220 | if (isBound() || mBinding) { |
| 221 | return; |
| 222 | } |
| 223 | if (DEBUG) { |
| 224 | Slog.i(LOG_TAG, "[user: " + mUserId + "] ensureBound()"); |
| 225 | } |
| 226 | mBinding = true; |
| 227 | mContext.bindServiceAsUser(mIntent, mServiceConnection, |
| 228 | Context.BIND_AUTO_CREATE, new UserHandle(mUserId)); |
| 229 | } |
| 230 | |
| 231 | private void ensureUnbound() { |
| 232 | if (!isBound() && !mBinding) { |
| 233 | return; |
| 234 | } |
| 235 | if (DEBUG) { |
| 236 | Slog.i(LOG_TAG, "[user: " + mUserId + "] ensureUnbound()"); |
| 237 | } |
| 238 | mBinding = false; |
| 239 | mPendingCommands.clear(); |
| 240 | if (isBound()) { |
| 241 | try { |
| 242 | mPrintService.setClient(null); |
| 243 | } catch (RemoteException re) { |
| 244 | /* ignore */ |
| 245 | } |
| 246 | mPrintService = null; |
| 247 | mContext.unbindService(mServiceConnection); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | private void throwIfDestroyed() { |
| 252 | if (mDestroyed) { |
| 253 | throw new IllegalStateException("Cannot interact with a destroyed service"); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | private class RemoteServiceConneciton implements ServiceConnection { |
| 258 | @Override |
| 259 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 260 | if (mDestroyed || !mBinding) { |
| 261 | return; |
| 262 | } |
| 263 | mBinding = false; |
| 264 | mPrintService = IPrintService.Stub.asInterface(service); |
| 265 | try { |
| 266 | mPrintService.setClient(mPrintServiceClient); |
| 267 | } catch (RemoteException re) { |
| 268 | Slog.e(LOG_TAG, "Error setting client for: " + service, re); |
| 269 | handleDestroy(); |
| 270 | return; |
| 271 | } |
| 272 | final int pendingCommandCount = mPendingCommands.size(); |
| 273 | for (int i = 0; i < pendingCommandCount; i++) { |
| 274 | Runnable pendingCommand = mPendingCommands.get(i); |
| 275 | pendingCommand.run(); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public void onServiceDisconnected(ComponentName name) { |
| 281 | mBinding = true; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | private final class MyHandler extends Handler { |
| 286 | public static final int MSG_ALL_PRINT_JOBS_HANDLED = 1; |
| 287 | public static final int MSG_REQUEST_CANCEL_PRINT_JOB = 2; |
| 288 | public static final int MSG_PRINT_JOB_QUEUED = 3; |
| 289 | public static final int MSG_START_PRINTER_DISCOVERY = 4; |
| 290 | public static final int MSG_STOP_PRINTER_DISCOVERY = 5; |
| 291 | public static final int MSG_DESTROY = 6; |
| 292 | |
| 293 | public MyHandler(Looper looper) { |
| 294 | super(looper, null, false); |
| 295 | } |
| 296 | |
| 297 | @Override |
| 298 | public void handleMessage(Message message) { |
| 299 | switch (message.what) { |
| 300 | case MSG_ALL_PRINT_JOBS_HANDLED: { |
| 301 | handleOnAllPrintJobsHandled(); |
| 302 | } break; |
| 303 | |
| 304 | case MSG_REQUEST_CANCEL_PRINT_JOB: { |
| 305 | PrintJobInfo printJob = (PrintJobInfo) message.obj; |
| 306 | handleOnRequestCancelPrintJob(printJob); |
| 307 | } break; |
| 308 | |
| 309 | case MSG_PRINT_JOB_QUEUED: { |
| 310 | PrintJobInfo printJob = (PrintJobInfo) message.obj; |
| 311 | handleOnPrintJobQueued(printJob); |
| 312 | } break; |
| 313 | |
| 314 | case MSG_START_PRINTER_DISCOVERY: { |
| 315 | IPrinterDiscoveryObserver observer = (IPrinterDiscoveryObserver) message.obj; |
| 316 | handleOnStartPrinterDiscovery(new SecurePrinterDiscoveryObserver( |
| 317 | mComponentName, observer)); |
| 318 | } break; |
| 319 | |
| 320 | case MSG_STOP_PRINTER_DISCOVERY: { |
| 321 | handleStopPrinterDiscovery(); |
| 322 | } break; |
| 323 | |
| 324 | case MSG_DESTROY: { |
| 325 | handleDestroy(); |
| 326 | } break; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | private static final class RemotePrintServiceClient extends IPrintServiceClient.Stub { |
| 332 | private final WeakReference<RemotePrintService> mWeakService; |
| 333 | |
| 334 | public RemotePrintServiceClient(RemotePrintService service) { |
| 335 | mWeakService = new WeakReference<RemotePrintService>(service); |
| 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public List<PrintJobInfo> getPrintJobInfos() { |
| 340 | RemotePrintService service = mWeakService.get(); |
| 341 | if (service != null) { |
| 342 | final long identity = Binder.clearCallingIdentity(); |
| 343 | try { |
| 344 | return service.mSpooler.getPrintJobInfos(service.mComponentName, |
| 345 | PrintJobInfo.STATE_ANY, PrintManager.APP_ID_ANY); |
| 346 | } finally { |
| 347 | Binder.restoreCallingIdentity(identity); |
| 348 | } |
| 349 | } |
| 350 | return null; |
| 351 | } |
| 352 | |
| 353 | @Override |
| 354 | public PrintJobInfo getPrintJobInfo(int printJobId) { |
| 355 | RemotePrintService service = mWeakService.get(); |
| 356 | if (service != null) { |
| 357 | final long identity = Binder.clearCallingIdentity(); |
| 358 | try { |
| 359 | return service.mSpooler.getPrintJobInfo(printJobId, |
| 360 | PrintManager.APP_ID_ANY); |
| 361 | } finally { |
| 362 | Binder.restoreCallingIdentity(identity); |
| 363 | } |
| 364 | } |
| 365 | return null; |
| 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public boolean setPrintJobState(int printJobId, int state) { |
| 370 | RemotePrintService service = mWeakService.get(); |
| 371 | if (service != null) { |
| 372 | final long identity = Binder.clearCallingIdentity(); |
| 373 | try { |
| 374 | return service.mSpooler.setPrintJobState(printJobId, state); |
| 375 | } finally { |
| 376 | Binder.restoreCallingIdentity(identity); |
| 377 | } |
| 378 | } |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | @Override |
| 383 | public boolean setPrintJobTag(int printJobId, String tag) { |
| 384 | RemotePrintService service = mWeakService.get(); |
| 385 | if (service != null) { |
| 386 | final long identity = Binder.clearCallingIdentity(); |
| 387 | try { |
| 388 | return service.mSpooler.setPrintJobTag(printJobId, tag); |
| 389 | } finally { |
| 390 | Binder.restoreCallingIdentity(identity); |
| 391 | } |
| 392 | } |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | @Override |
| 397 | public void writePrintJobData(ParcelFileDescriptor fd, int printJobId) { |
| 398 | RemotePrintService service = mWeakService.get(); |
| 399 | if (service != null) { |
| 400 | final long identity = Binder.clearCallingIdentity(); |
| 401 | try { |
| 402 | service.mSpooler.writePrintJobData(fd, printJobId); |
| 403 | } finally { |
| 404 | Binder.restoreCallingIdentity(identity); |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | private static final class SecurePrinterDiscoveryObserver |
| 411 | extends IPrinterDiscoveryObserver.Stub { |
| 412 | private final ComponentName mComponentName; |
| 413 | |
| 414 | private final IPrinterDiscoveryObserver mDecoratedObsever; |
| 415 | |
| 416 | public SecurePrinterDiscoveryObserver(ComponentName componentName, |
| 417 | IPrinterDiscoveryObserver observer) { |
| 418 | mComponentName = componentName; |
| 419 | mDecoratedObsever = observer; |
| 420 | } |
| 421 | |
| 422 | @Override |
| 423 | public void addDiscoveredPrinters(List<PrinterInfo> printers) { |
| 424 | throwIfPrinterIdsForPrinterInfoTampered(printers); |
| 425 | try { |
| 426 | mDecoratedObsever.addDiscoveredPrinters(printers); |
| 427 | } catch (RemoteException re) { |
| 428 | Slog.e(LOG_TAG, "Error delegating to addDiscoveredPrinters", re); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | @Override |
| 433 | public void removeDiscoveredPrinters(List<PrinterId> printerIds) { |
| 434 | throwIfPrinterIdsTampered(printerIds); |
| 435 | try { |
| 436 | mDecoratedObsever.removeDiscoveredPrinters(printerIds); |
| 437 | } catch (RemoteException re) { |
| 438 | Slog.e(LOG_TAG, "Error delegating to removeDiscoveredPrinters", re); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | private void throwIfPrinterIdsForPrinterInfoTampered( |
| 443 | List<PrinterInfo> printerInfos) { |
| 444 | final int printerInfoCount = printerInfos.size(); |
| 445 | for (int i = 0; i < printerInfoCount; i++) { |
| 446 | PrinterId printerId = printerInfos.get(i).getId(); |
| 447 | throwIfPrinterIdTampered(printerId); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | private void throwIfPrinterIdsTampered(List<PrinterId> printerIds) { |
| 452 | final int printerIdCount = printerIds.size(); |
| 453 | for (int i = 0; i < printerIdCount; i++) { |
| 454 | PrinterId printerId = printerIds.get(i); |
| 455 | throwIfPrinterIdTampered(printerId); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | private void throwIfPrinterIdTampered(PrinterId printerId) { |
| 460 | if (printerId == null || printerId.getService() == null |
| 461 | || !printerId.getService().equals(mComponentName)) { |
| 462 | throw new IllegalArgumentException("Invalid printer id: " + printerId); |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | } |