Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2013 Google Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #include "SkPurgeableMemoryBlock.h" |
michael@0 | 9 | |
michael@0 | 10 | #include "android/ashmem.h" |
michael@0 | 11 | #include <sys/mman.h> |
michael@0 | 12 | #include <unistd.h> |
michael@0 | 13 | |
michael@0 | 14 | bool SkPurgeableMemoryBlock::IsSupported() { |
michael@0 | 15 | return true; |
michael@0 | 16 | } |
michael@0 | 17 | |
michael@0 | 18 | #ifdef SK_DEBUG |
michael@0 | 19 | bool SkPurgeableMemoryBlock::PlatformSupportsPurgingAllUnpinnedBlocks() { |
michael@0 | 20 | return false; |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | bool SkPurgeableMemoryBlock::PurgeAllUnpinnedBlocks() { |
michael@0 | 24 | return false; |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | bool SkPurgeableMemoryBlock::purge() { |
michael@0 | 28 | SkASSERT(!fPinned); |
michael@0 | 29 | if (-1 != fFD) { |
michael@0 | 30 | ashmem_purge_all_caches(fFD); |
michael@0 | 31 | return true; |
michael@0 | 32 | } else { |
michael@0 | 33 | return false; |
michael@0 | 34 | } |
michael@0 | 35 | } |
michael@0 | 36 | #endif |
michael@0 | 37 | |
michael@0 | 38 | // ashmem likes lengths on page boundaries. |
michael@0 | 39 | static size_t round_to_page_size(size_t size) { |
michael@0 | 40 | const size_t mask = getpagesize() - 1; |
michael@0 | 41 | size_t newSize = (size + mask) & ~mask; |
michael@0 | 42 | return newSize; |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | SkPurgeableMemoryBlock::SkPurgeableMemoryBlock(size_t size) |
michael@0 | 46 | : fAddr(NULL) |
michael@0 | 47 | , fSize(round_to_page_size(size)) |
michael@0 | 48 | , fPinned(false) |
michael@0 | 49 | , fFD(-1) { |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | SkPurgeableMemoryBlock::~SkPurgeableMemoryBlock() { |
michael@0 | 53 | if (-1 != fFD) { |
michael@0 | 54 | munmap(fAddr, fSize); |
michael@0 | 55 | close(fFD); |
michael@0 | 56 | } |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | void* SkPurgeableMemoryBlock::pin(SkPurgeableMemoryBlock::PinResult* pinResult) { |
michael@0 | 60 | SkASSERT(!fPinned); |
michael@0 | 61 | if (-1 == fFD) { |
michael@0 | 62 | int fd = ashmem_create_region(NULL, fSize); |
michael@0 | 63 | if (-1 == fd) { |
michael@0 | 64 | SkDebugf("ashmem_create_region failed\n"); |
michael@0 | 65 | return NULL; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | int err = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE); |
michael@0 | 69 | if (err != 0) { |
michael@0 | 70 | SkDebugf("ashmem_set_prot_region failed\n"); |
michael@0 | 71 | close(fd); |
michael@0 | 72 | return NULL; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | void* addr = mmap(NULL, fSize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
michael@0 | 76 | if (-1 == (long) addr) { |
michael@0 | 77 | SkDebugf("mmap failed\n"); |
michael@0 | 78 | close(fd); |
michael@0 | 79 | return NULL; |
michael@0 | 80 | } |
michael@0 | 81 | fAddr = addr; |
michael@0 | 82 | fFD = fd; |
michael@0 | 83 | (void) ashmem_pin_region(fd, 0, 0); |
michael@0 | 84 | *pinResult = kUninitialized_PinResult; |
michael@0 | 85 | fPinned = true; |
michael@0 | 86 | } else { |
michael@0 | 87 | int pin = ashmem_pin_region(fFD, 0, 0); |
michael@0 | 88 | if (ASHMEM_NOT_PURGED == pin) { |
michael@0 | 89 | fPinned = true; |
michael@0 | 90 | *pinResult = kRetained_PinResult; |
michael@0 | 91 | } else if (ASHMEM_WAS_PURGED == pin) { |
michael@0 | 92 | fPinned = true; |
michael@0 | 93 | *pinResult = kUninitialized_PinResult; |
michael@0 | 94 | } else { |
michael@0 | 95 | // Failed. |
michael@0 | 96 | munmap(fAddr, fSize); |
michael@0 | 97 | close(fFD); |
michael@0 | 98 | fFD = -1; |
michael@0 | 99 | fAddr = NULL; |
michael@0 | 100 | } |
michael@0 | 101 | } |
michael@0 | 102 | return fAddr; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | void SkPurgeableMemoryBlock::unpin() { |
michael@0 | 106 | if (-1 != fFD) { |
michael@0 | 107 | ashmem_unpin_region(fFD, 0, 0); |
michael@0 | 108 | fPinned = false; |
michael@0 | 109 | } |
michael@0 | 110 | } |