Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: ios/chrome/browser/crash_report/crash_report_multi_parameter.mm

Issue 1207353005: [iOS] Upstream some stability code (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "ios/chrome/browser/crash_report/crash_report_multi_parameter.h"
6
7 #include "base/json/json_writer.h"
8 #include "base/logging.h"
9 #import "base/mac/scoped_nsobject.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "base/values.h"
13 #import "ios/chrome/browser/crash_report/breakpad_helper.h"
14
15 namespace {
16 // Maximum size of a breakpad parameter. he length of the dictionary serialized
17 // into JSON cannot exceed this length. See declaration in (BreakPad.h) for
18 // details.
19 const int kMaximumBreakpadValueSize = 255;
20 }
21
22 @implementation CrashReportMultiParameter {
23 base::scoped_nsobject<NSString> crashReportKey_;
24 scoped_ptr<base::DictionaryValue> dictionary_;
25 }
26
27 - (instancetype)init {
28 NOTREACHED();
29 return nil;
30 }
31
32 - (instancetype)initWithKey:(NSString*)key {
33 if ((self = [super init])) {
34 DCHECK([key length] && ([key length] <= kMaximumBreakpadValueSize));
35 dictionary_.reset(new base::DictionaryValue());
36 crashReportKey_.reset([key copy]);
37 }
38 return self;
39 }
40
41 - (void)removeValue:(NSString*)key {
42 dictionary_->Remove(base::SysNSStringToUTF8(key).c_str(), nullptr);
43 [self updateCrashReport];
44 }
45
46 - (void)setValue:(NSString*)key withValue:(int)value {
47 dictionary_->SetInteger(base::SysNSStringToUTF8(key).c_str(), value);
48 [self updateCrashReport];
49 }
50
51 - (void)incrementValue:(NSString*)key {
52 int value;
53 std::string utf8_string = base::SysNSStringToUTF8(key);
54 if (dictionary_->GetInteger(utf8_string.c_str(), &value)) {
55 dictionary_->SetInteger(utf8_string.c_str(), value + 1);
56 } else {
57 dictionary_->SetInteger(utf8_string.c_str(), 1);
58 }
59 [self updateCrashReport];
60 }
61
62 - (void)decrementValue:(NSString*)key {
63 int value;
64 std::string utf8_string = base::SysNSStringToUTF8(key);
65 if (dictionary_->GetInteger(utf8_string.c_str(), &value)) {
66 if (value <= 1) {
67 dictionary_->Remove(utf8_string.c_str(), nullptr);
68 } else {
69 dictionary_->SetInteger(utf8_string.c_str(), value - 1);
70 }
71 [self updateCrashReport];
72 }
73 }
74
75 - (void)updateCrashReport {
76 std::string stateAsJson;
77 base::JSONWriter::Write(*dictionary_.get(), &stateAsJson);
78 if (stateAsJson.length() > kMaximumBreakpadValueSize) {
79 NOTREACHED();
80 return;
81 }
82 breakpad_helper::AddReportParameter(
83 crashReportKey_,
84 [NSString stringWithCString:stateAsJson.c_str()
85 encoding:[NSString defaultCStringEncoding]],
86 true);
87 }
88
89 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698