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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: ios/chrome/browser/crash_report/crash_report_multi_parameter.mm
diff --git a/ios/chrome/browser/crash_report/crash_report_multi_parameter.mm b/ios/chrome/browser/crash_report/crash_report_multi_parameter.mm
new file mode 100644
index 0000000000000000000000000000000000000000..052fa78e1a55075de4070b1dc702d6f918719a6a
--- /dev/null
+++ b/ios/chrome/browser/crash_report/crash_report_multi_parameter.mm
@@ -0,0 +1,89 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#import "ios/chrome/browser/crash_report/crash_report_multi_parameter.h"
+
+#include "base/json/json_writer.h"
+#include "base/logging.h"
+#import "base/mac/scoped_nsobject.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/strings/sys_string_conversions.h"
+#include "base/values.h"
+#import "ios/chrome/browser/crash_report/breakpad_helper.h"
+
+namespace {
+// Maximum size of a breakpad parameter. he length of the dictionary serialized
+// into JSON cannot exceed this length. See declaration in (BreakPad.h) for
+// details.
+const int kMaximumBreakpadValueSize = 255;
+}
+
+@implementation CrashReportMultiParameter {
+ base::scoped_nsobject<NSString> crashReportKey_;
+ scoped_ptr<base::DictionaryValue> dictionary_;
+}
+
+- (instancetype)init {
+ NOTREACHED();
+ return nil;
+}
+
+- (instancetype)initWithKey:(NSString*)key {
+ if ((self = [super init])) {
+ DCHECK([key length] && ([key length] <= kMaximumBreakpadValueSize));
+ dictionary_.reset(new base::DictionaryValue());
+ crashReportKey_.reset([key copy]);
+ }
+ return self;
+}
+
+- (void)removeValue:(NSString*)key {
+ dictionary_->Remove(base::SysNSStringToUTF8(key).c_str(), nullptr);
+ [self updateCrashReport];
+}
+
+- (void)setValue:(NSString*)key withValue:(int)value {
+ dictionary_->SetInteger(base::SysNSStringToUTF8(key).c_str(), value);
+ [self updateCrashReport];
+}
+
+- (void)incrementValue:(NSString*)key {
+ int value;
+ std::string utf8_string = base::SysNSStringToUTF8(key);
+ if (dictionary_->GetInteger(utf8_string.c_str(), &value)) {
+ dictionary_->SetInteger(utf8_string.c_str(), value + 1);
+ } else {
+ dictionary_->SetInteger(utf8_string.c_str(), 1);
+ }
+ [self updateCrashReport];
+}
+
+- (void)decrementValue:(NSString*)key {
+ int value;
+ std::string utf8_string = base::SysNSStringToUTF8(key);
+ if (dictionary_->GetInteger(utf8_string.c_str(), &value)) {
+ if (value <= 1) {
+ dictionary_->Remove(utf8_string.c_str(), nullptr);
+ } else {
+ dictionary_->SetInteger(utf8_string.c_str(), value - 1);
+ }
+ [self updateCrashReport];
+ }
+}
+
+- (void)updateCrashReport {
+ std::string stateAsJson;
+ base::JSONWriter::Write(*dictionary_.get(), &stateAsJson);
+ if (stateAsJson.length() > kMaximumBreakpadValueSize) {
+ NOTREACHED();
+ return;
+ }
+ breakpad_helper::AddReportParameter(
+ crashReportKey_,
+ [NSString stringWithCString:stateAsJson.c_str()
+ encoding:[NSString defaultCStringEncoding]],
+ true);
+}
+
+@end

Powered by Google App Engine
This is Rietveld 408576698