Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* |
michael@0 | 2 | ** Copyright 2010, The Android Open Source Project |
michael@0 | 3 | ** Copyright 2013, Mozilla Foundation |
michael@0 | 4 | ** |
michael@0 | 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
michael@0 | 6 | ** you may not use this file except in compliance with the License. |
michael@0 | 7 | ** You may obtain a copy of the License at |
michael@0 | 8 | ** |
michael@0 | 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 10 | ** |
michael@0 | 11 | ** Unless required by applicable law or agreed to in writing, software |
michael@0 | 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 14 | ** See the License for the specific language governing permissions and |
michael@0 | 15 | ** limitations under the License. |
michael@0 | 16 | */ |
michael@0 | 17 | |
michael@0 | 18 | //#define LOG_NDEBUG 0 |
michael@0 | 19 | #define LOG_TAG "IMediaResourceManagerDeathNotifier" |
michael@0 | 20 | #include <utils/Log.h> |
michael@0 | 21 | |
michael@0 | 22 | #include <binder/IServiceManager.h> |
michael@0 | 23 | #include <binder/IPCThreadState.h> |
michael@0 | 24 | |
michael@0 | 25 | #include "IMediaResourceManagerDeathNotifier.h" |
michael@0 | 26 | |
michael@0 | 27 | #define DN_LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__) |
michael@0 | 28 | #define DN_LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__) |
michael@0 | 29 | #define DN_LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) |
michael@0 | 30 | #define DN_LOGE_IF(cond, ...) \ |
michael@0 | 31 | ( (CONDITION(cond)) \ |
michael@0 | 32 | ? ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \ |
michael@0 | 33 | : (void)0 ) |
michael@0 | 34 | |
michael@0 | 35 | namespace android { |
michael@0 | 36 | |
michael@0 | 37 | // client singleton for binder interface to services |
michael@0 | 38 | Mutex IMediaResourceManagerDeathNotifier::sServiceLock; |
michael@0 | 39 | sp<IMediaResourceManagerService> IMediaResourceManagerDeathNotifier::sMediaResourceManagerService; |
michael@0 | 40 | sp<IMediaResourceManagerDeathNotifier::DeathNotifier> IMediaResourceManagerDeathNotifier::sDeathNotifier; |
michael@0 | 41 | SortedVector< wp<IMediaResourceManagerDeathNotifier> > IMediaResourceManagerDeathNotifier::sObitRecipients; |
michael@0 | 42 | |
michael@0 | 43 | // establish binder interface to MediaResourceManagerService |
michael@0 | 44 | /*static*/const sp<IMediaResourceManagerService>& |
michael@0 | 45 | IMediaResourceManagerDeathNotifier::getMediaResourceManagerService() |
michael@0 | 46 | { |
michael@0 | 47 | DN_LOGV("getMediaResourceManagerService"); |
michael@0 | 48 | Mutex::Autolock _l(sServiceLock); |
michael@0 | 49 | if (sMediaResourceManagerService.get() == 0) { |
michael@0 | 50 | sp<IServiceManager> sm = defaultServiceManager(); |
michael@0 | 51 | sp<IBinder> binder; |
michael@0 | 52 | do { |
michael@0 | 53 | binder = sm->getService(String16("media.resource_manager")); |
michael@0 | 54 | if (binder != 0) { |
michael@0 | 55 | break; |
michael@0 | 56 | } |
michael@0 | 57 | DN_LOGW("Media resource manager service not published, waiting..."); |
michael@0 | 58 | usleep(500000); // 0.5 s |
michael@0 | 59 | } while(true); |
michael@0 | 60 | |
michael@0 | 61 | if (sDeathNotifier == NULL) { |
michael@0 | 62 | sDeathNotifier = new DeathNotifier(); |
michael@0 | 63 | } |
michael@0 | 64 | binder->linkToDeath(sDeathNotifier); |
michael@0 | 65 | sMediaResourceManagerService = interface_cast<IMediaResourceManagerService>(binder); |
michael@0 | 66 | } |
michael@0 | 67 | DN_LOGE_IF(sMediaResourceManagerService == 0, "no media player service!?"); |
michael@0 | 68 | return sMediaResourceManagerService; |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | /*static*/ void |
michael@0 | 72 | IMediaResourceManagerDeathNotifier::addObitRecipient(const wp<IMediaResourceManagerDeathNotifier>& recipient) |
michael@0 | 73 | { |
michael@0 | 74 | Mutex::Autolock _l(sServiceLock); |
michael@0 | 75 | sObitRecipients.add(recipient); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | /*static*/ void |
michael@0 | 79 | IMediaResourceManagerDeathNotifier::removeObitRecipient(const wp<IMediaResourceManagerDeathNotifier>& recipient) |
michael@0 | 80 | { |
michael@0 | 81 | Mutex::Autolock _l(sServiceLock); |
michael@0 | 82 | sObitRecipients.remove(recipient); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | void |
michael@0 | 86 | IMediaResourceManagerDeathNotifier::DeathNotifier::binderDied(const wp<IBinder>& who) |
michael@0 | 87 | { |
michael@0 | 88 | DN_LOGW("media resource manager service died"); |
michael@0 | 89 | // Need to do this with the lock held |
michael@0 | 90 | SortedVector< wp<IMediaResourceManagerDeathNotifier> > list; |
michael@0 | 91 | { |
michael@0 | 92 | Mutex::Autolock _l(sServiceLock); |
michael@0 | 93 | sMediaResourceManagerService.clear(); |
michael@0 | 94 | list = sObitRecipients; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | // Notify application when media server dies. |
michael@0 | 98 | // Don't hold the static lock during callback in case app |
michael@0 | 99 | // makes a call that needs the lock. |
michael@0 | 100 | size_t count = list.size(); |
michael@0 | 101 | for (size_t iter = 0; iter < count; ++iter) { |
michael@0 | 102 | sp<IMediaResourceManagerDeathNotifier> notifier = list[iter].promote(); |
michael@0 | 103 | if (notifier != 0) { |
michael@0 | 104 | notifier->died(); |
michael@0 | 105 | } |
michael@0 | 106 | } |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | IMediaResourceManagerDeathNotifier::DeathNotifier::~DeathNotifier() |
michael@0 | 110 | { |
michael@0 | 111 | Mutex::Autolock _l(sServiceLock); |
michael@0 | 112 | sObitRecipients.clear(); |
michael@0 | 113 | if (sMediaResourceManagerService != 0) { |
michael@0 | 114 | sMediaResourceManagerService->asBinder()->unlinkToDeath(this); |
michael@0 | 115 | } |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | }; // namespace android |