Chromium Code Reviews| 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..969ae1464af04b5b24a0f268e08ab9f27d8a1692 |
| --- /dev/null |
| +++ b/ios/chrome/browser/crash_report/crash_report_multi_parameter.mm |
| @@ -0,0 +1,87 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
|
lpromero
2015/07/08 13:37:16
I don't know the rules when upstreaming. Should we
droger
2015/07/08 14:04:37
The policy is to keep the original year when upstr
|
| +// 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/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 { |
| + @private |
| + base::scoped_nsobject<NSString> crashReportKey_; |
| + scoped_ptr<base::DictionaryValue> dictionary_; |
| +} |
| + |
| +- (id)init { |
| + NOTREACHED(); |
| + return nil; |
| +} |
| + |
| +- (id)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([key UTF8String], NULL); |
| + [self updateCrashReport]; |
| +} |
| + |
| +- (void)setValue:(NSString*)key withValue:(int)value { |
| + dictionary_->SetInteger([key UTF8String], value); |
| + [self updateCrashReport]; |
| +} |
| + |
| +- (void)incrementValue:(NSString*)key { |
| + int value; |
| + if (dictionary_->GetInteger([key UTF8String], &value)) { |
| + dictionary_->SetInteger([key UTF8String], value + 1); |
| + } else { |
| + dictionary_->SetInteger([key UTF8String], 1); |
| + } |
| + [self updateCrashReport]; |
| +} |
| + |
| +- (void)decrementValue:(NSString*)key { |
| + int value; |
| + if (dictionary_->GetInteger([key UTF8String], &value)) { |
| + if (value <= 1) { |
| + dictionary_->Remove([key UTF8String], NULL); |
| + } else { |
| + dictionary_->SetInteger([key UTF8String], 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 |