Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 | |
| 15 #include "minidump/minidump_simple_string_dictionary_writer.h" | |
| 16 | |
| 17 #include "base/logging.h" | |
| 18 #include "util/numeric/safe_assignment.h" | |
| 19 | |
| 20 namespace crashpad { | |
| 21 | |
| 22 MinidumpSimpleStringDictionaryEntryWriter:: | |
| 23 MinidumpSimpleStringDictionaryEntryWriter() | |
| 24 : MinidumpWritable(), entry_(), key_(), value_() { | |
| 25 } | |
| 26 | |
| 27 MinidumpSimpleStringDictionaryEntryWriter:: | |
| 28 ~MinidumpSimpleStringDictionaryEntryWriter() { | |
| 29 } | |
| 30 | |
| 31 const MinidumpSimpleStringDictionaryEntry* | |
| 32 MinidumpSimpleStringDictionaryEntryWriter::MinidumpSimpleStringDictionaryEntry() | |
| 33 const { | |
| 34 DCHECK_EQ(state(), kStateWritable); | |
| 35 | |
| 36 return &entry_; | |
| 37 } | |
| 38 | |
| 39 void MinidumpSimpleStringDictionaryEntryWriter::SetKeyValue( | |
| 40 const std::string& key, | |
| 41 const std::string& value) { | |
| 42 key_.SetUTF8(key); | |
| 43 value_.SetUTF8(value); | |
| 44 } | |
| 45 | |
| 46 bool MinidumpSimpleStringDictionaryEntryWriter::Freeze() { | |
| 47 DCHECK_EQ(state(), kStateMutable); | |
| 48 | |
| 49 if (!MinidumpWritable::Freeze()) { | |
| 50 return false; | |
| 51 } | |
| 52 | |
| 53 key_.RegisterRVA(&entry_.key); | |
| 54 value_.RegisterRVA(&entry_.value); | |
| 55 | |
| 56 return true; | |
| 57 } | |
| 58 | |
| 59 size_t MinidumpSimpleStringDictionaryEntryWriter::SizeOfObject() { | |
| 60 DCHECK_GE(state(), kStateFrozen); | |
| 61 | |
| 62 // This object doesn’t directly write anything itself. Its | |
| 63 // MinidumpSimpleStringDictionaryEntry is written by its parent as part of a | |
| 64 // MinidumpSimpleStringDictionary, and its children are responsible for | |
| 65 // writing themselves. | |
| 66 return 0; | |
| 67 } | |
| 68 | |
| 69 std::vector<internal::MinidumpWritable*> | |
| 70 MinidumpSimpleStringDictionaryEntryWriter::Children() { | |
| 71 DCHECK_GE(state(), kStateFrozen); | |
| 72 | |
| 73 std::vector<MinidumpWritable*> children(1, &key_); | |
| 74 children.push_back(&value_); | |
| 75 return children; | |
| 76 } | |
| 77 | |
| 78 bool MinidumpSimpleStringDictionaryEntryWriter::WriteObject( | |
| 79 FileWriterInterface* file_writer) { | |
| 80 DCHECK_EQ(state(), kStateWritable); | |
| 81 | |
| 82 // This object doesn’t directly write anything itself. Its | |
| 83 // MinidumpSimpleStringDictionaryEntry is written by its parent as part of a | |
| 84 // MinidumpSimpleStringDictionary, and its children are responsible for | |
| 85 // writing themselves. | |
| 86 return true; | |
| 87 } | |
| 88 | |
| 89 MinidumpSimpleStringDictionaryWriter::MinidumpSimpleStringDictionaryWriter() | |
| 90 : MinidumpWritable(), simple_string_dictionary_base_(), entries_() { | |
| 91 } | |
| 92 | |
| 93 MinidumpSimpleStringDictionaryWriter::~MinidumpSimpleStringDictionaryWriter() { | |
| 94 } | |
| 95 | |
| 96 void MinidumpSimpleStringDictionaryWriter::AddEntry( | |
| 97 MinidumpSimpleStringDictionaryEntryWriter* entry) { | |
| 98 DCHECK_GE(state(), kStateMutable); | |
| 99 | |
| 100 entries_[entry->Key()] = entry; | |
| 101 } | |
| 102 | |
| 103 bool MinidumpSimpleStringDictionaryWriter::Freeze() { | |
| 104 DCHECK_EQ(state(), kStateMutable); | |
| 105 | |
| 106 if (!MinidumpWritable::Freeze()) { | |
| 107 return false; | |
| 108 } | |
| 109 | |
| 110 size_t entry_count = entries_.size(); | |
| 111 if (!AssignIfInRange(&simple_string_dictionary_base_.count, entry_count)) { | |
| 112 LOG(ERROR) << "entry_count " << entry_count << " out of range"; | |
| 113 return false; | |
| 114 } | |
| 115 | |
| 116 return true; | |
| 117 } | |
| 118 | |
| 119 size_t MinidumpSimpleStringDictionaryWriter::SizeOfObject() { | |
| 120 DCHECK_GE(state(), kStateFrozen); | |
| 121 | |
| 122 return sizeof(simple_string_dictionary_base_) + | |
| 123 entries_.size() * sizeof(MinidumpSimpleStringDictionaryEntry); | |
| 124 } | |
| 125 | |
| 126 std::vector<internal::MinidumpWritable*> | |
| 127 MinidumpSimpleStringDictionaryWriter::Children() { | |
| 128 DCHECK_GE(state(), kStateMutable); | |
| 129 | |
| 130 std::vector<MinidumpWritable*> children; | |
| 131 for (const std::pair<std::string, MinidumpSimpleStringDictionaryEntryWriter*> | |
|
Robert Sesek
2014/10/16 18:07:59
This would probably be better written with auto. S
| |
| 132 ke : entries_) { | |
|
Robert Sesek
2014/10/16 18:07:59
|ke| ?
| |
| 133 children.push_back(ke.second); | |
| 134 } | |
| 135 | |
| 136 return children; | |
| 137 } | |
| 138 | |
| 139 bool MinidumpSimpleStringDictionaryWriter::WriteObject( | |
| 140 FileWriterInterface* file_writer) { | |
| 141 DCHECK_GE(state(), kStateWritable); | |
| 142 | |
| 143 WritableIoVec iov; | |
| 144 iov.iov_base = &simple_string_dictionary_base_; | |
| 145 iov.iov_len = sizeof(simple_string_dictionary_base_); | |
| 146 std::vector<WritableIoVec> iovecs(1, iov); | |
| 147 | |
| 148 if (!entries_.empty()) { | |
| 149 iov.iov_len = sizeof(MinidumpSimpleStringDictionaryEntry); | |
| 150 for (const std::pair<std::string, | |
| 151 MinidumpSimpleStringDictionaryEntryWriter*> ke : | |
| 152 entries_) { | |
| 153 iov.iov_base = ke.second->MinidumpSimpleStringDictionaryEntry(); | |
| 154 iovecs.push_back(iov); | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 return file_writer->WriteIoVec(&iovecs); | |
| 159 } | |
| 160 | |
| 161 } // namespace crashpad | |
| OLD | NEW |