gfx/layers/ipc/FenceUtilsGonk.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

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 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * vim: sw=2 ts=8 et :
michael@0 3 */
michael@0 4 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 5 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 7
michael@0 8 #include "GLContext.h"
michael@0 9 #include "mozilla/unused.h"
michael@0 10 #include "nsXULAppAPI.h"
michael@0 11
michael@0 12 #include "FenceUtilsGonk.h"
michael@0 13
michael@0 14 using namespace android;
michael@0 15 using namespace base;
michael@0 16 using namespace mozilla::layers;
michael@0 17
michael@0 18 namespace IPC {
michael@0 19
michael@0 20 void
michael@0 21 ParamTraits<FenceHandle>::Write(Message* aMsg,
michael@0 22 const paramType& aParam)
michael@0 23 {
michael@0 24 #if ANDROID_VERSION >= 19
michael@0 25 sp<Fence> flattenable = aParam.mFence;
michael@0 26 #else
michael@0 27 Flattenable *flattenable = aParam.mFence.get();
michael@0 28 #endif
michael@0 29 size_t nbytes = flattenable->getFlattenedSize();
michael@0 30 size_t nfds = flattenable->getFdCount();
michael@0 31
michael@0 32 char data[nbytes];
michael@0 33 int fds[nfds];
michael@0 34
michael@0 35 #if ANDROID_VERSION >= 19
michael@0 36 // Make a copy of "data" and "fds" for flatten() to avoid casting problem
michael@0 37 void *pdata = (void *)data;
michael@0 38 int *pfds = fds;
michael@0 39
michael@0 40 flattenable->flatten(pdata, nbytes, pfds, nfds);
michael@0 41
michael@0 42 // In Kitkat, flatten() will change the value of nbytes and nfds, which dues
michael@0 43 // to multiple parcelable object consumption. The actual size and fd count
michael@0 44 // which returned by getFlattenedSize() and getFdCount() are not changed.
michael@0 45 // So we change nbytes and nfds back by call corresponding calls.
michael@0 46 nbytes = flattenable->getFlattenedSize();
michael@0 47 nfds = flattenable->getFdCount();
michael@0 48 #else
michael@0 49 flattenable->flatten(data, nbytes, fds, nfds);
michael@0 50 #endif
michael@0 51 aMsg->WriteSize(nbytes);
michael@0 52 aMsg->WriteSize(nfds);
michael@0 53
michael@0 54 aMsg->WriteBytes(data, nbytes);
michael@0 55 for (size_t n = 0; n < nfds; ++n) {
michael@0 56 // These buffers can't die in transit because they're created
michael@0 57 // synchonously and the parent-side buffer can only be dropped if
michael@0 58 // there's a crash.
michael@0 59 aMsg->WriteFileDescriptor(FileDescriptor(fds[n], false));
michael@0 60 }
michael@0 61 }
michael@0 62
michael@0 63 bool
michael@0 64 ParamTraits<FenceHandle>::Read(const Message* aMsg,
michael@0 65 void** aIter, paramType* aResult)
michael@0 66 {
michael@0 67 size_t nbytes;
michael@0 68 size_t nfds;
michael@0 69 const char* data;
michael@0 70
michael@0 71 if (!aMsg->ReadSize(aIter, &nbytes) ||
michael@0 72 !aMsg->ReadSize(aIter, &nfds) ||
michael@0 73 !aMsg->ReadBytes(aIter, &data, nbytes)) {
michael@0 74 return false;
michael@0 75 }
michael@0 76
michael@0 77 int fds[nfds];
michael@0 78
michael@0 79 for (size_t n = 0; n < nfds; ++n) {
michael@0 80 FileDescriptor fd;
michael@0 81 if (!aMsg->ReadFileDescriptor(aIter, &fd)) {
michael@0 82 return false;
michael@0 83 }
michael@0 84 // If the GraphicBuffer was shared cross-process, SCM_RIGHTS does
michael@0 85 // the right thing and dup's the fd. If it's shared cross-thread,
michael@0 86 // SCM_RIGHTS doesn't dup the fd. That's surprising, but we just
michael@0 87 // deal with it here. NB: only the "default" (master) process can
michael@0 88 // alloc gralloc buffers.
michael@0 89 bool sameProcess = (XRE_GetProcessType() == GeckoProcessType_Default);
michael@0 90 int dupFd = sameProcess ? dup(fd.fd) : fd.fd;
michael@0 91 fds[n] = dupFd;
michael@0 92 }
michael@0 93
michael@0 94 sp<Fence> buffer(new Fence());
michael@0 95 #if ANDROID_VERSION >= 19
michael@0 96 // Make a copy of "data" and "fds" for unflatten() to avoid casting problem
michael@0 97 void const *pdata = (void const *)data;
michael@0 98 int const *pfds = fds;
michael@0 99
michael@0 100 if (NO_ERROR == buffer->unflatten(pdata, nbytes, pfds, nfds)) {
michael@0 101 #else
michael@0 102 Flattenable *flattenable = buffer.get();
michael@0 103
michael@0 104 if (NO_ERROR == flattenable->unflatten(data, nbytes, fds, nfds)) {
michael@0 105 #endif
michael@0 106 aResult->mFence = buffer;
michael@0 107 return true;
michael@0 108 }
michael@0 109 return false;
michael@0 110 }
michael@0 111
michael@0 112 } // namespace IPC
michael@0 113
michael@0 114 namespace mozilla {
michael@0 115 namespace layers {
michael@0 116
michael@0 117 FenceHandle::FenceHandle(const sp<Fence>& aFence)
michael@0 118 : mFence(aFence)
michael@0 119 {
michael@0 120 }
michael@0 121
michael@0 122 } // namespace layers
michael@0 123 } // namespace mozilla

mercurial