gfx/skia/trunk/src/views/SkEvent.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

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 /*
michael@0 3 * Copyright 2006 The Android Open Source Project
michael@0 4 *
michael@0 5 * Use of this source code is governed by a BSD-style license that can be
michael@0 6 * found in the LICENSE file.
michael@0 7 */
michael@0 8
michael@0 9
michael@0 10 #include "SkEvent.h"
michael@0 11
michael@0 12 void SkEvent::initialize(const char* type, size_t typeLen,
michael@0 13 SkEventSinkID targetID) {
michael@0 14 fType = NULL;
michael@0 15 setType(type, typeLen);
michael@0 16 f32 = 0;
michael@0 17 fTargetID = targetID;
michael@0 18 fTargetProc = NULL;
michael@0 19 #ifdef SK_DEBUG
michael@0 20 fTime = 0;
michael@0 21 fNextEvent = NULL;
michael@0 22 #endif
michael@0 23 }
michael@0 24
michael@0 25 SkEvent::SkEvent()
michael@0 26 {
michael@0 27 initialize("", 0, 0);
michael@0 28 }
michael@0 29
michael@0 30 SkEvent::SkEvent(const SkEvent& src)
michael@0 31 {
michael@0 32 *this = src;
michael@0 33 if (((size_t) fType & 1) == 0)
michael@0 34 setType(src.fType);
michael@0 35 }
michael@0 36
michael@0 37 SkEvent::SkEvent(const SkString& type, SkEventSinkID targetID)
michael@0 38 {
michael@0 39 initialize(type.c_str(), type.size(), targetID);
michael@0 40 }
michael@0 41
michael@0 42 SkEvent::SkEvent(const char type[], SkEventSinkID targetID)
michael@0 43 {
michael@0 44 SkASSERT(type);
michael@0 45 initialize(type, strlen(type), targetID);
michael@0 46 }
michael@0 47
michael@0 48 SkEvent::~SkEvent()
michael@0 49 {
michael@0 50 if (((size_t) fType & 1) == 0)
michael@0 51 sk_free((void*) fType);
michael@0 52 }
michael@0 53
michael@0 54 static size_t makeCharArray(char* buffer, size_t compact)
michael@0 55 {
michael@0 56 size_t bits = (size_t) compact >> 1;
michael@0 57 memcpy(buffer, &bits, sizeof(compact));
michael@0 58 buffer[sizeof(compact)] = 0;
michael@0 59 return strlen(buffer);
michael@0 60 }
michael@0 61
michael@0 62 void SkEvent::getType(SkString* str) const
michael@0 63 {
michael@0 64 if (str)
michael@0 65 {
michael@0 66 if ((size_t) fType & 1) // not a pointer
michael@0 67 {
michael@0 68 char chars[sizeof(size_t) + 1];
michael@0 69 size_t len = makeCharArray(chars, (size_t) fType);
michael@0 70 str->set(chars, len);
michael@0 71 }
michael@0 72 else
michael@0 73 str->set(fType);
michael@0 74 }
michael@0 75 }
michael@0 76
michael@0 77 bool SkEvent::isType(const SkString& str) const
michael@0 78 {
michael@0 79 return this->isType(str.c_str(), str.size());
michael@0 80 }
michael@0 81
michael@0 82 bool SkEvent::isType(const char type[], size_t typeLen) const
michael@0 83 {
michael@0 84 if (typeLen == 0)
michael@0 85 typeLen = strlen(type);
michael@0 86 if ((size_t) fType & 1) { // not a pointer
michael@0 87 char chars[sizeof(size_t) + 1];
michael@0 88 size_t len = makeCharArray(chars, (size_t) fType);
michael@0 89 return len == typeLen && strncmp(chars, type, typeLen) == 0;
michael@0 90 }
michael@0 91 return strncmp(fType, type, typeLen) == 0 && fType[typeLen] == 0;
michael@0 92 }
michael@0 93
michael@0 94 void SkEvent::setType(const char type[], size_t typeLen)
michael@0 95 {
michael@0 96 if (typeLen == 0)
michael@0 97 typeLen = strlen(type);
michael@0 98 if (typeLen <= sizeof(fType)) {
michael@0 99 size_t slot = 0;
michael@0 100 memcpy(&slot, type, typeLen);
michael@0 101 if (slot << 1 >> 1 != slot)
michael@0 102 goto useCharStar;
michael@0 103 slot <<= 1;
michael@0 104 slot |= 1;
michael@0 105 fType = (char*) slot;
michael@0 106 } else {
michael@0 107 useCharStar:
michael@0 108 fType = (char*) sk_malloc_throw(typeLen + 1);
michael@0 109 SkASSERT(((size_t) fType & 1) == 0);
michael@0 110 memcpy(fType, type, typeLen);
michael@0 111 fType[typeLen] = 0;
michael@0 112 }
michael@0 113 }
michael@0 114
michael@0 115 void SkEvent::setType(const SkString& type)
michael@0 116 {
michael@0 117 setType(type.c_str());
michael@0 118 }
michael@0 119
michael@0 120 ////////////////////////////////////////////////////////////////////////////
michael@0 121
michael@0 122 #include "SkParse.h"
michael@0 123
michael@0 124 void SkEvent::inflate(const SkDOM& dom, const SkDOM::Node* node)
michael@0 125 {
michael@0 126 const char* name = dom.findAttr(node, "type");
michael@0 127 if (name)
michael@0 128 this->setType(name);
michael@0 129
michael@0 130 const char* value;
michael@0 131 if ((value = dom.findAttr(node, "fast32")) != NULL)
michael@0 132 {
michael@0 133 int32_t n;
michael@0 134 if (SkParse::FindS32(value, &n))
michael@0 135 this->setFast32(n);
michael@0 136 }
michael@0 137
michael@0 138 for (node = dom.getFirstChild(node); node; node = dom.getNextSibling(node))
michael@0 139 {
michael@0 140 if (strcmp(dom.getName(node), "data"))
michael@0 141 {
michael@0 142 SkDEBUGCODE(SkDebugf("SkEvent::inflate unrecognized subelement <%s>\n", dom.getName(node));)
michael@0 143 continue;
michael@0 144 }
michael@0 145
michael@0 146 name = dom.findAttr(node, "name");
michael@0 147 if (name == NULL)
michael@0 148 {
michael@0 149 SkDEBUGCODE(SkDebugf("SkEvent::inflate missing required \"name\" attribute in <data> subelement\n");)
michael@0 150 continue;
michael@0 151 }
michael@0 152
michael@0 153 if ((value = dom.findAttr(node, "s32")) != NULL)
michael@0 154 {
michael@0 155 int32_t n;
michael@0 156 if (SkParse::FindS32(value, &n))
michael@0 157 this->setS32(name, n);
michael@0 158 }
michael@0 159 else if ((value = dom.findAttr(node, "scalar")) != NULL)
michael@0 160 {
michael@0 161 SkScalar x;
michael@0 162 if (SkParse::FindScalar(value, &x))
michael@0 163 this->setScalar(name, x);
michael@0 164 }
michael@0 165 else if ((value = dom.findAttr(node, "string")) != NULL)
michael@0 166 this->setString(name, value);
michael@0 167 #ifdef SK_DEBUG
michael@0 168 else
michael@0 169 {
michael@0 170 SkDebugf("SkEvent::inflate <data name=\"%s\"> subelement missing required type attribute [S32 | scalar | string]\n", name);
michael@0 171 }
michael@0 172 #endif
michael@0 173 }
michael@0 174 }
michael@0 175
michael@0 176 #ifdef SK_DEBUG
michael@0 177
michael@0 178 #ifndef SkScalarToFloat
michael@0 179 #define SkScalarToFloat(x) ((x) / 65536.f)
michael@0 180 #endif
michael@0 181
michael@0 182 void SkEvent::dump(const char title[])
michael@0 183 {
michael@0 184 if (title)
michael@0 185 SkDebugf("%s ", title);
michael@0 186
michael@0 187 SkString etype;
michael@0 188 this->getType(&etype);
michael@0 189 SkDebugf("event<%s> fast32=%d", etype.c_str(), this->getFast32());
michael@0 190
michael@0 191 const SkMetaData& md = this->getMetaData();
michael@0 192 SkMetaData::Iter iter(md);
michael@0 193 SkMetaData::Type mtype;
michael@0 194 int count;
michael@0 195 const char* name;
michael@0 196
michael@0 197 while ((name = iter.next(&mtype, &count)) != NULL)
michael@0 198 {
michael@0 199 SkASSERT(count > 0);
michael@0 200
michael@0 201 SkDebugf(" <%s>=", name);
michael@0 202 switch (mtype) {
michael@0 203 case SkMetaData::kS32_Type: // vector version???
michael@0 204 {
michael@0 205 int32_t value;
michael@0 206 md.findS32(name, &value);
michael@0 207 SkDebugf("%d ", value);
michael@0 208 }
michael@0 209 break;
michael@0 210 case SkMetaData::kScalar_Type:
michael@0 211 {
michael@0 212 const SkScalar* values = md.findScalars(name, &count, NULL);
michael@0 213 SkDebugf("%f", SkScalarToFloat(values[0]));
michael@0 214 for (int i = 1; i < count; i++)
michael@0 215 SkDebugf(", %f", SkScalarToFloat(values[i]));
michael@0 216 SkDebugf(" ");
michael@0 217 }
michael@0 218 break;
michael@0 219 case SkMetaData::kString_Type:
michael@0 220 {
michael@0 221 const char* value = md.findString(name);
michael@0 222 SkASSERT(value);
michael@0 223 SkDebugf("<%s> ", value);
michael@0 224 }
michael@0 225 break;
michael@0 226 case SkMetaData::kPtr_Type: // vector version???
michael@0 227 {
michael@0 228 void* value;
michael@0 229 md.findPtr(name, &value);
michael@0 230 SkDebugf("%p ", value);
michael@0 231 }
michael@0 232 break;
michael@0 233 case SkMetaData::kBool_Type: // vector version???
michael@0 234 {
michael@0 235 bool value;
michael@0 236 md.findBool(name, &value);
michael@0 237 SkDebugf("%s ", value ? "true" : "false");
michael@0 238 }
michael@0 239 break;
michael@0 240 default:
michael@0 241 SkDEBUGFAIL("unknown metadata type returned from iterator");
michael@0 242 break;
michael@0 243 }
michael@0 244 }
michael@0 245 SkDebugf("\n");
michael@0 246 }
michael@0 247 #endif
michael@0 248
michael@0 249 ///////////////////////////////////////////////////////////////////////////////////////
michael@0 250
michael@0 251 #ifdef SK_DEBUG
michael@0 252 // #define SK_TRACE_EVENTSx
michael@0 253 #endif
michael@0 254
michael@0 255 #ifdef SK_TRACE_EVENTS
michael@0 256 static void event_log(const char s[])
michael@0 257 {
michael@0 258 SkDEBUGF(("%s\n", s));
michael@0 259 }
michael@0 260
michael@0 261 #define EVENT_LOG(s) event_log(s)
michael@0 262 #define EVENT_LOGN(s, n) do { SkString str(s); str.append(" "); str.appendS32(n); event_log(str.c_str()); } while (0)
michael@0 263 #else
michael@0 264 #define EVENT_LOG(s)
michael@0 265 #define EVENT_LOGN(s, n)
michael@0 266 #endif
michael@0 267
michael@0 268 #include "SkThread.h"
michael@0 269 #include "SkTime.h"
michael@0 270
michael@0 271 class SkEvent_Globals {
michael@0 272 public:
michael@0 273 SkEvent_Globals() {
michael@0 274 fEventQHead = NULL;
michael@0 275 fEventQTail = NULL;
michael@0 276 fDelayQHead = NULL;
michael@0 277 SkDEBUGCODE(fEventCounter = 0;)
michael@0 278 }
michael@0 279
michael@0 280 SkMutex fEventMutex;
michael@0 281 SkEvent* fEventQHead, *fEventQTail;
michael@0 282 SkEvent* fDelayQHead;
michael@0 283 SkDEBUGCODE(int fEventCounter;)
michael@0 284 };
michael@0 285
michael@0 286 static SkEvent_Globals& getGlobals() {
michael@0 287 // leak this, so we don't incure any shutdown perf hit
michael@0 288 static SkEvent_Globals* gGlobals = new SkEvent_Globals;
michael@0 289 return *gGlobals;
michael@0 290 }
michael@0 291
michael@0 292 ///////////////////////////////////////////////////////////////////////////////
michael@0 293
michael@0 294 void SkEvent::postDelay(SkMSec delay) {
michael@0 295 if (!fTargetID && !fTargetProc) {
michael@0 296 delete this;
michael@0 297 return;
michael@0 298 }
michael@0 299
michael@0 300 if (delay) {
michael@0 301 this->postTime(SkTime::GetMSecs() + delay);
michael@0 302 return;
michael@0 303 }
michael@0 304
michael@0 305 SkEvent_Globals& globals = getGlobals();
michael@0 306
michael@0 307 globals.fEventMutex.acquire();
michael@0 308 bool wasEmpty = SkEvent::Enqueue(this);
michael@0 309 globals.fEventMutex.release();
michael@0 310
michael@0 311 // call outside of us holding the mutex
michael@0 312 if (wasEmpty) {
michael@0 313 SkEvent::SignalNonEmptyQueue();
michael@0 314 }
michael@0 315 }
michael@0 316
michael@0 317 void SkEvent::postTime(SkMSec time) {
michael@0 318 if (!fTargetID && !fTargetProc) {
michael@0 319 delete this;
michael@0 320 return;
michael@0 321 }
michael@0 322
michael@0 323 SkEvent_Globals& globals = getGlobals();
michael@0 324
michael@0 325 globals.fEventMutex.acquire();
michael@0 326 SkMSec queueDelay = SkEvent::EnqueueTime(this, time);
michael@0 327 globals.fEventMutex.release();
michael@0 328
michael@0 329 // call outside of us holding the mutex
michael@0 330 if ((int32_t)queueDelay != ~0) {
michael@0 331 SkEvent::SignalQueueTimer(queueDelay);
michael@0 332 }
michael@0 333 }
michael@0 334
michael@0 335 bool SkEvent::Enqueue(SkEvent* evt) {
michael@0 336 SkEvent_Globals& globals = getGlobals();
michael@0 337 // gEventMutex acquired by caller
michael@0 338
michael@0 339 SkASSERT(evt);
michael@0 340
michael@0 341 bool wasEmpty = globals.fEventQHead == NULL;
michael@0 342
michael@0 343 if (globals.fEventQTail)
michael@0 344 globals.fEventQTail->fNextEvent = evt;
michael@0 345 globals.fEventQTail = evt;
michael@0 346 if (globals.fEventQHead == NULL)
michael@0 347 globals.fEventQHead = evt;
michael@0 348 evt->fNextEvent = NULL;
michael@0 349
michael@0 350 SkDEBUGCODE(++globals.fEventCounter);
michael@0 351
michael@0 352 return wasEmpty;
michael@0 353 }
michael@0 354
michael@0 355 SkEvent* SkEvent::Dequeue() {
michael@0 356 SkEvent_Globals& globals = getGlobals();
michael@0 357 globals.fEventMutex.acquire();
michael@0 358
michael@0 359 SkEvent* evt = globals.fEventQHead;
michael@0 360 if (evt) {
michael@0 361 SkDEBUGCODE(--globals.fEventCounter);
michael@0 362
michael@0 363 globals.fEventQHead = evt->fNextEvent;
michael@0 364 if (globals.fEventQHead == NULL) {
michael@0 365 globals.fEventQTail = NULL;
michael@0 366 }
michael@0 367 }
michael@0 368 globals.fEventMutex.release();
michael@0 369
michael@0 370 return evt;
michael@0 371 }
michael@0 372
michael@0 373 bool SkEvent::QHasEvents() {
michael@0 374 SkEvent_Globals& globals = getGlobals();
michael@0 375
michael@0 376 // this is not thread accurate, need a semaphore for that
michael@0 377 return globals.fEventQHead != NULL;
michael@0 378 }
michael@0 379
michael@0 380 #ifdef SK_TRACE_EVENTS
michael@0 381 static int gDelayDepth;
michael@0 382 #endif
michael@0 383
michael@0 384 SkMSec SkEvent::EnqueueTime(SkEvent* evt, SkMSec time) {
michael@0 385 SkEvent_Globals& globals = getGlobals();
michael@0 386 // gEventMutex acquired by caller
michael@0 387
michael@0 388 SkEvent* curr = globals.fDelayQHead;
michael@0 389 SkEvent* prev = NULL;
michael@0 390
michael@0 391 while (curr) {
michael@0 392 if (SkMSec_LT(time, curr->fTime)) {
michael@0 393 break;
michael@0 394 }
michael@0 395 prev = curr;
michael@0 396 curr = curr->fNextEvent;
michael@0 397 }
michael@0 398
michael@0 399 evt->fTime = time;
michael@0 400 evt->fNextEvent = curr;
michael@0 401 if (prev == NULL) {
michael@0 402 globals.fDelayQHead = evt;
michael@0 403 } else {
michael@0 404 prev->fNextEvent = evt;
michael@0 405 }
michael@0 406
michael@0 407 SkMSec delay = globals.fDelayQHead->fTime - SkTime::GetMSecs();
michael@0 408 if ((int32_t)delay <= 0) {
michael@0 409 delay = 1;
michael@0 410 }
michael@0 411 return delay;
michael@0 412 }
michael@0 413
michael@0 414 ///////////////////////////////////////////////////////////////////////////////
michael@0 415
michael@0 416 #include "SkEventSink.h"
michael@0 417
michael@0 418 bool SkEvent::ProcessEvent() {
michael@0 419 SkEvent* evt = SkEvent::Dequeue();
michael@0 420 SkAutoTDelete<SkEvent> autoDelete(evt);
michael@0 421 bool again = false;
michael@0 422
michael@0 423 EVENT_LOGN("ProcessEvent", (int32_t)evt);
michael@0 424
michael@0 425 if (evt) {
michael@0 426 (void)SkEventSink::DoEvent(*evt);
michael@0 427 again = SkEvent::QHasEvents();
michael@0 428 }
michael@0 429 return again;
michael@0 430 }
michael@0 431
michael@0 432 void SkEvent::ServiceQueueTimer()
michael@0 433 {
michael@0 434 SkEvent_Globals& globals = getGlobals();
michael@0 435
michael@0 436 globals.fEventMutex.acquire();
michael@0 437
michael@0 438 bool wasEmpty = false;
michael@0 439 SkMSec now = SkTime::GetMSecs();
michael@0 440 SkEvent* evt = globals.fDelayQHead;
michael@0 441
michael@0 442 while (evt)
michael@0 443 {
michael@0 444 if (SkMSec_LT(now, evt->fTime))
michael@0 445 break;
michael@0 446
michael@0 447 #ifdef SK_TRACE_EVENTS
michael@0 448 --gDelayDepth;
michael@0 449 SkDebugf("dequeue-delay %s (%d)", evt->getType(), gDelayDepth);
michael@0 450 const char* idStr = evt->findString("id");
michael@0 451 if (idStr)
michael@0 452 SkDebugf(" (%s)", idStr);
michael@0 453 SkDebugf("\n");
michael@0 454 #endif
michael@0 455
michael@0 456 SkEvent* next = evt->fNextEvent;
michael@0 457 if (SkEvent::Enqueue(evt))
michael@0 458 wasEmpty = true;
michael@0 459 evt = next;
michael@0 460 }
michael@0 461 globals.fDelayQHead = evt;
michael@0 462
michael@0 463 SkMSec time = evt ? evt->fTime - now : 0;
michael@0 464
michael@0 465 globals.fEventMutex.release();
michael@0 466
michael@0 467 if (wasEmpty)
michael@0 468 SkEvent::SignalNonEmptyQueue();
michael@0 469
michael@0 470 SkEvent::SignalQueueTimer(time);
michael@0 471 }
michael@0 472
michael@0 473 int SkEvent::CountEventsOnQueue() {
michael@0 474 SkEvent_Globals& globals = getGlobals();
michael@0 475 globals.fEventMutex.acquire();
michael@0 476
michael@0 477 int count = 0;
michael@0 478 const SkEvent* evt = globals.fEventQHead;
michael@0 479 while (evt) {
michael@0 480 count += 1;
michael@0 481 evt = evt->fNextEvent;
michael@0 482 }
michael@0 483 globals.fEventMutex.release();
michael@0 484
michael@0 485 return count;
michael@0 486 }
michael@0 487
michael@0 488 ///////////////////////////////////////////////////////////////////////////////
michael@0 489
michael@0 490 void SkEvent::Init() {}
michael@0 491
michael@0 492 void SkEvent::Term() {
michael@0 493 SkEvent_Globals& globals = getGlobals();
michael@0 494
michael@0 495 SkEvent* evt = globals.fEventQHead;
michael@0 496 while (evt) {
michael@0 497 SkEvent* next = evt->fNextEvent;
michael@0 498 delete evt;
michael@0 499 evt = next;
michael@0 500 }
michael@0 501
michael@0 502 evt = globals.fDelayQHead;
michael@0 503 while (evt) {
michael@0 504 SkEvent* next = evt->fNextEvent;
michael@0 505 delete evt;
michael@0 506 evt = next;
michael@0 507 }
michael@0 508 }

mercurial