toolkit/crashreporter/google-breakpad/src/common/mac/SimpleStringDictionary.h

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 // Copyright (c) 2007, Google Inc.
michael@0 2 // All rights reserved.
michael@0 3 //
michael@0 4 // Redistribution and use in source and binary forms, with or without
michael@0 5 // modification, are permitted provided that the following conditions are
michael@0 6 // met:
michael@0 7 //
michael@0 8 // * Redistributions of source code must retain the above copyright
michael@0 9 // notice, this list of conditions and the following disclaimer.
michael@0 10 // * Redistributions in binary form must reproduce the above
michael@0 11 // copyright notice, this list of conditions and the following disclaimer
michael@0 12 // in the documentation and/or other materials provided with the
michael@0 13 // distribution.
michael@0 14 // * Neither the name of Google Inc. nor the names of its
michael@0 15 // contributors may be used to endorse or promote products derived from
michael@0 16 // this software without specific prior written permission.
michael@0 17 //
michael@0 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 29 //
michael@0 30 // SimpleStringDictionary.h
michael@0 31 //
michael@0 32
michael@0 33 #ifndef SimpleStringDictionary_H__
michael@0 34 #define SimpleStringDictionary_H__
michael@0 35
michael@0 36 #import <string>
michael@0 37 #import <vector>
michael@0 38
michael@0 39 namespace google_breakpad {
michael@0 40
michael@0 41 //==============================================================================
michael@0 42 // SimpleStringDictionary (and associated class KeyValueEntry) implement a very
michael@0 43 // basic dictionary container class. It has the property of not making any
michael@0 44 // memory allocations when getting and setting values. But it is not very
michael@0 45 // efficient, with calls to get and set values operating in linear time.
michael@0 46 // It has the additional limitation of having a fairly small fixed capacity of
michael@0 47 // SimpleStringDictionary::MAX_NUM_ENTRIES entries. An assert() will fire if
michael@0 48 // the client attempts to set more than this number of key/value pairs.
michael@0 49 // Ordinarilly a C++ programmer would use something like the std::map template
michael@0 50 // class, or on the Macintosh would often choose CFDictionary or NSDictionary.
michael@0 51 // But these dictionary classes may call malloc() during get and set operations.
michael@0 52 // Google Breakpad requires that no memory allocations be made in code running
michael@0 53 // in its exception handling thread, so it uses SimpleStringDictionary as the
michael@0 54 // underlying implementation for the GoogleBreakpad.framework APIs:
michael@0 55 // GoogleBreakpadSetKeyValue(), GoogleBreakpadKeyValue(), and
michael@0 56 // GoogleBreakpadRemoveKeyValue()
michael@0 57 //
michael@0 58
michael@0 59 //==============================================================================
michael@0 60 // KeyValueEntry
michael@0 61 //
michael@0 62 // A helper class used by SimpleStringDictionary representing a single
michael@0 63 // storage cell for a key/value pair. Each key and value string are
michael@0 64 // limited to MAX_STRING_STORAGE_SIZE-1 bytes (not glyphs). This class
michael@0 65 // performs no memory allocations. It has methods for setting and getting
michael@0 66 // key and value strings.
michael@0 67 //
michael@0 68 class KeyValueEntry {
michael@0 69 public:
michael@0 70 KeyValueEntry() {
michael@0 71 Clear();
michael@0 72 }
michael@0 73
michael@0 74 KeyValueEntry(const char *key, const char *value) {
michael@0 75 SetKeyValue(key, value);
michael@0 76 }
michael@0 77
michael@0 78 void SetKeyValue(const char *key, const char *value) {
michael@0 79 if (!key) {
michael@0 80 key = "";
michael@0 81 }
michael@0 82 if (!value) {
michael@0 83 value = "";
michael@0 84 }
michael@0 85
michael@0 86 strlcpy(key_, key, sizeof(key_));
michael@0 87 strlcpy(value_, value, sizeof(value_));
michael@0 88 }
michael@0 89
michael@0 90 void SetValue(const char *value) {
michael@0 91 if (!value) {
michael@0 92 value = "";
michael@0 93 }
michael@0 94 strlcpy(value_, value, sizeof(value_));
michael@0 95 };
michael@0 96
michael@0 97 // Removes the key/value
michael@0 98 void Clear() {
michael@0 99 memset(key_, 0, sizeof(key_));
michael@0 100 memset(value_, 0, sizeof(value_));
michael@0 101 }
michael@0 102
michael@0 103 bool IsActive() const { return key_[0] != '\0'; }
michael@0 104 const char *GetKey() const { return key_; }
michael@0 105 const char *GetValue() const { return value_; }
michael@0 106
michael@0 107 // Don't change this without considering the fixed size
michael@0 108 // of MachMessage (in MachIPC.h)
michael@0 109 // (see also struct KeyValueMessageData in Inspector.h)
michael@0 110 enum {MAX_STRING_STORAGE_SIZE = 256};
michael@0 111
michael@0 112 private:
michael@0 113 char key_[MAX_STRING_STORAGE_SIZE];
michael@0 114 char value_[MAX_STRING_STORAGE_SIZE];
michael@0 115 };
michael@0 116
michael@0 117 //==============================================================================
michael@0 118 // This class is not an efficient dictionary, but for the purposes of breakpad
michael@0 119 // will be just fine. We're just dealing with ten or so distinct
michael@0 120 // key/value pairs. The idea is to avoid any malloc() or free() calls
michael@0 121 // in certain important methods to be called when a process is in a
michael@0 122 // crashed state. Each key and value string are limited to
michael@0 123 // KeyValueEntry::MAX_STRING_STORAGE_SIZE-1 bytes (not glyphs). Strings passed
michael@0 124 // in exceeding this length will be truncated.
michael@0 125 //
michael@0 126 class SimpleStringDictionary {
michael@0 127 public:
michael@0 128 SimpleStringDictionary() {}; // entries will all be cleared
michael@0 129
michael@0 130 // Returns the number of active key/value pairs. The upper limit for this
michael@0 131 // is MAX_NUM_ENTRIES.
michael@0 132 int GetCount() const;
michael@0 133
michael@0 134 // Given |key|, returns its corresponding |value|.
michael@0 135 // If |key| is NULL, an assert will fire or NULL will be returned. If |key|
michael@0 136 // is not found or is an empty string, NULL is returned.
michael@0 137 const char *GetValueForKey(const char *key) const;
michael@0 138
michael@0 139 // Stores a string |value| represented by |key|. If |key| is NULL or an empty
michael@0 140 // string, this will assert (or do nothing). If |value| is NULL then
michael@0 141 // the |key| will be removed. An empty string is OK for |value|.
michael@0 142 void SetKeyValue(const char *key, const char *value);
michael@0 143
michael@0 144 // Given |key|, removes any associated value. It will assert (or do nothing)
michael@0 145 // if NULL is passed in. It will do nothing if |key| is not found.
michael@0 146 void RemoveKey(const char *key);
michael@0 147
michael@0 148 // This is the maximum number of key/value pairs which may be set in the
michael@0 149 // dictionary. An assert may fire if more values than this are set.
michael@0 150 // Don't change this without also changing comment in GoogleBreakpad.h
michael@0 151 enum {MAX_NUM_ENTRIES = 64};
michael@0 152
michael@0 153 private:
michael@0 154 friend class SimpleStringDictionaryIterator;
michael@0 155
michael@0 156 const KeyValueEntry *GetEntry(int i) const;
michael@0 157
michael@0 158 KeyValueEntry entries_[MAX_NUM_ENTRIES];
michael@0 159 };
michael@0 160
michael@0 161 //==============================================================================
michael@0 162 class SimpleStringDictionaryIterator {
michael@0 163 public:
michael@0 164 SimpleStringDictionaryIterator(const SimpleStringDictionary &dict)
michael@0 165 : dict_(dict), i_(0) {
michael@0 166 }
michael@0 167
michael@0 168 // Initializes iterator to the beginning (may later call Next() )
michael@0 169 void Start() {
michael@0 170 i_ = 0;
michael@0 171 }
michael@0 172
michael@0 173 // like the nextObject method of NSEnumerator (in Cocoa)
michael@0 174 // returns NULL when there are no more entries
michael@0 175 //
michael@0 176 const KeyValueEntry* Next() {
michael@0 177 for (; i_ < SimpleStringDictionary::MAX_NUM_ENTRIES; ++i_) {
michael@0 178 const KeyValueEntry *entry = dict_.GetEntry(i_);
michael@0 179 if (entry->IsActive()) {
michael@0 180 i_++; // move to next entry for next time
michael@0 181 return entry;
michael@0 182 }
michael@0 183 }
michael@0 184
michael@0 185 return NULL; // reached end of array
michael@0 186 }
michael@0 187
michael@0 188 private:
michael@0 189 const SimpleStringDictionary& dict_;
michael@0 190 int i_;
michael@0 191 };
michael@0 192
michael@0 193 } // namespace google_breakpad
michael@0 194
michael@0 195 #endif // SimpleStringDictionary_H__

mercurial