1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/client/ios/BreakpadController.mm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,294 @@ 1.4 +// Copyright (c) 2012, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 + 1.33 +#import "BreakpadController.h" 1.34 + 1.35 +#import <UIKit/UIKit.h> 1.36 +#include <asl.h> 1.37 +#include <execinfo.h> 1.38 +#include <signal.h> 1.39 +#include <unistd.h> 1.40 +#include <sys/sysctl.h> 1.41 + 1.42 +#include <common/scoped_ptr.h> 1.43 + 1.44 +#pragma mark - 1.45 +#pragma mark Private Methods 1.46 + 1.47 +@interface BreakpadController () 1.48 + 1.49 +// Init the singleton instance. 1.50 +- (id)initSingleton; 1.51 + 1.52 +// Load a crash report and send it to the server. 1.53 +- (void)sendStoredCrashReports; 1.54 + 1.55 +// Returns when a report can be sent. |-1| means never, |0| means that a report 1.56 +// can be sent immediately, a positive number is the number of seconds to wait 1.57 +// before being allowed to upload a report. 1.58 +- (int)sendDelay; 1.59 + 1.60 +// Notifies that a report will be sent, and update the last sending time 1.61 +// accordingly. 1.62 +- (void)reportWillBeSent; 1.63 + 1.64 +@end 1.65 + 1.66 +#pragma mark - 1.67 +#pragma mark Anonymous namespace 1.68 + 1.69 +namespace { 1.70 + 1.71 +// The name of the user defaults key for the last submission to the crash 1.72 +// server. 1.73 +NSString* const kLastSubmission = @"com.google.Breakpad.LastSubmission"; 1.74 + 1.75 +// Returns a NSString describing the current platform. 1.76 +NSString* GetPlatform() { 1.77 + // Name of the system call for getting the platform. 1.78 + static const char kHwMachineSysctlName[] = "hw.machine"; 1.79 + 1.80 + NSString* result = nil; 1.81 + 1.82 + size_t size = 0; 1.83 + if (sysctlbyname(kHwMachineSysctlName, NULL, &size, NULL, 0) || size == 0) 1.84 + return nil; 1.85 + google_breakpad::scoped_array<char> machine(new char[size]); 1.86 + if (sysctlbyname(kHwMachineSysctlName, machine.get(), &size, NULL, 0) == 0) 1.87 + result = [NSString stringWithUTF8String:machine.get()]; 1.88 + return result; 1.89 +} 1.90 + 1.91 +} // namespace 1.92 + 1.93 +#pragma mark - 1.94 +#pragma mark BreakpadController Implementation 1.95 + 1.96 +@implementation BreakpadController 1.97 + 1.98 ++ (BreakpadController*)sharedInstance { 1.99 + @synchronized(self) { 1.100 + static BreakpadController* sharedInstance_ = 1.101 + [[BreakpadController alloc] initSingleton]; 1.102 + return sharedInstance_; 1.103 + } 1.104 +} 1.105 + 1.106 +- (id)init { 1.107 + return nil; 1.108 +} 1.109 + 1.110 +- (id)initSingleton { 1.111 + self = [super init]; 1.112 + if (self) { 1.113 + queue_ = dispatch_queue_create("com.google.BreakpadQueue", NULL); 1.114 + configuration_ = [[[NSBundle mainBundle] infoDictionary] mutableCopy]; 1.115 + enableUploads_ = NO; 1.116 + started_ = NO; 1.117 + NSString* uploadInterval = 1.118 + [configuration_ valueForKey:@BREAKPAD_REPORT_INTERVAL]; 1.119 + [self setUploadInterval:[uploadInterval intValue]]; 1.120 + } 1.121 + return self; 1.122 +} 1.123 + 1.124 +// Since this class is a singleton, this method is not expected to be called. 1.125 +- (void)dealloc { 1.126 + assert(!breakpadRef_); 1.127 + dispatch_release(queue_); 1.128 + [configuration_ release]; 1.129 + [super dealloc]; 1.130 +} 1.131 + 1.132 +#pragma mark - 1.133 + 1.134 +- (void)start:(BOOL)onCurrentThread { 1.135 + if (started_) 1.136 + return; 1.137 + started_ = YES; 1.138 + void(^startBlock)() = ^{ 1.139 + assert(!breakpadRef_); 1.140 + breakpadRef_ = BreakpadCreate(configuration_); 1.141 + if (breakpadRef_) { 1.142 + BreakpadAddUploadParameter(breakpadRef_, @"platform", GetPlatform()); 1.143 + } 1.144 + }; 1.145 + if (onCurrentThread) 1.146 + startBlock(); 1.147 + else 1.148 + dispatch_async(queue_, startBlock); 1.149 +} 1.150 + 1.151 +- (void)stop { 1.152 + if (!started_) 1.153 + return; 1.154 + started_ = NO; 1.155 + dispatch_sync(queue_, ^{ 1.156 + if (breakpadRef_) { 1.157 + BreakpadRelease(breakpadRef_); 1.158 + breakpadRef_ = NULL; 1.159 + } 1.160 + }); 1.161 +} 1.162 + 1.163 +- (void)setUploadingEnabled:(BOOL)enabled { 1.164 + NSAssert(started_, 1.165 + @"The controller must be started before setUploadingEnabled is called"); 1.166 + dispatch_async(queue_, ^{ 1.167 + if (enabled == enableUploads_) 1.168 + return; 1.169 + if (enabled) { 1.170 + // Set this before calling doSendStoredCrashReport, because that 1.171 + // calls sendDelay, which in turn checks this flag. 1.172 + enableUploads_ = YES; 1.173 + [self sendStoredCrashReports]; 1.174 + } else { 1.175 + enableUploads_ = NO; 1.176 + [NSObject cancelPreviousPerformRequestsWithTarget:self 1.177 + selector:@selector(sendStoredCrashReports) 1.178 + object:nil]; 1.179 + } 1.180 + }); 1.181 +} 1.182 + 1.183 +- (void)updateConfiguration:(NSDictionary*)configuration { 1.184 + NSAssert(!started_, 1.185 + @"The controller must not be started when updateConfiguration is called"); 1.186 + [configuration_ addEntriesFromDictionary:configuration]; 1.187 + NSString* uploadInterval = 1.188 + [configuration_ valueForKey:@BREAKPAD_REPORT_INTERVAL]; 1.189 + if (uploadInterval) 1.190 + [self setUploadInterval:[uploadInterval intValue]]; 1.191 +} 1.192 + 1.193 +- (void)setUploadingURL:(NSString*)url { 1.194 + NSAssert(!started_, 1.195 + @"The controller must not be started when setUploadingURL is called"); 1.196 + [configuration_ setValue:url forKey:@BREAKPAD_URL]; 1.197 +} 1.198 + 1.199 +- (void)setUploadInterval:(int)intervalInSeconds { 1.200 + NSAssert(!started_, 1.201 + @"The controller must not be started when setUploadInterval is called"); 1.202 + [configuration_ removeObjectForKey:@BREAKPAD_REPORT_INTERVAL]; 1.203 + uploadIntervalInSeconds_ = intervalInSeconds; 1.204 + if (uploadIntervalInSeconds_ < 0) 1.205 + uploadIntervalInSeconds_ = 0; 1.206 +} 1.207 + 1.208 +- (void)addUploadParameter:(NSString*)value forKey:(NSString*)key { 1.209 + NSAssert(started_, 1.210 + @"The controller must be started before addUploadParameter is called"); 1.211 + dispatch_async(queue_, ^{ 1.212 + if (breakpadRef_) 1.213 + BreakpadAddUploadParameter(breakpadRef_, key, value); 1.214 + }); 1.215 +} 1.216 + 1.217 +- (void)removeUploadParameterForKey:(NSString*)key { 1.218 + NSAssert(started_, @"The controller must be started before " 1.219 + "removeUploadParameterForKey is called"); 1.220 + dispatch_async(queue_, ^{ 1.221 + if (breakpadRef_) 1.222 + BreakpadRemoveUploadParameter(breakpadRef_, key); 1.223 + }); 1.224 +} 1.225 + 1.226 +- (void)withBreakpadRef:(void(^)(BreakpadRef))callback { 1.227 + NSAssert(started_, 1.228 + @"The controller must be started before withBreakpadRef is called"); 1.229 + dispatch_async(queue_, ^{ 1.230 + callback(breakpadRef_); 1.231 + }); 1.232 +} 1.233 + 1.234 +- (void)hasReportToUpload:(void(^)(BOOL))callback { 1.235 + NSAssert(started_, @"The controller must be started before " 1.236 + "hasReportToUpload is called"); 1.237 + dispatch_async(queue_, ^{ 1.238 + callback(breakpadRef_ && BreakpadHasCrashReportToUpload(breakpadRef_)); 1.239 + }); 1.240 +} 1.241 + 1.242 +#pragma mark - 1.243 + 1.244 +- (int)sendDelay { 1.245 + if (!breakpadRef_ || uploadIntervalInSeconds_ <= 0 || !enableUploads_) 1.246 + return -1; 1.247 + 1.248 + // To prevent overloading the crash server, crashes are not sent than one 1.249 + // report every |uploadIntervalInSeconds_|. A value in the user defaults is 1.250 + // used to keep the time of the last upload. 1.251 + NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 1.252 + NSNumber *lastTimeNum = [userDefaults objectForKey:kLastSubmission]; 1.253 + NSTimeInterval lastTime = lastTimeNum ? [lastTimeNum floatValue] : 0; 1.254 + NSTimeInterval spanSeconds = CFAbsoluteTimeGetCurrent() - lastTime; 1.255 + 1.256 + if (spanSeconds >= uploadIntervalInSeconds_) 1.257 + return 0; 1.258 + return uploadIntervalInSeconds_ - static_cast<int>(spanSeconds); 1.259 +} 1.260 + 1.261 +- (void)reportWillBeSent { 1.262 + NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 1.263 + [userDefaults setObject:[NSNumber numberWithDouble:CFAbsoluteTimeGetCurrent()] 1.264 + forKey:kLastSubmission]; 1.265 + [userDefaults synchronize]; 1.266 +} 1.267 + 1.268 +- (void)sendStoredCrashReports { 1.269 + dispatch_async(queue_, ^{ 1.270 + if (!BreakpadHasCrashReportToUpload(breakpadRef_)) 1.271 + return; 1.272 + 1.273 + int timeToWait = [self sendDelay]; 1.274 + 1.275 + // Unable to ever send report. 1.276 + if (timeToWait == -1) 1.277 + return; 1.278 + 1.279 + // A report can be sent now. 1.280 + if (timeToWait == 0) { 1.281 + [self reportWillBeSent]; 1.282 + BreakpadUploadNextReport(breakpadRef_); 1.283 + 1.284 + // If more reports must be sent, make sure this method is called again. 1.285 + if (BreakpadHasCrashReportToUpload(breakpadRef_)) 1.286 + timeToWait = uploadIntervalInSeconds_; 1.287 + } 1.288 + 1.289 + // A report must be sent later. 1.290 + if (timeToWait > 0) 1.291 + [self performSelector:@selector(sendStoredCrashReports) 1.292 + withObject:nil 1.293 + afterDelay:timeToWait]; 1.294 + }); 1.295 +} 1.296 + 1.297 +@end