summaryrefslogtreecommitdiffstats
path: root/toolkit/crashreporter/client/crashreporter_osx.mm
blob: 38b3349a04666a26f9ecd8c1bdb80f49849fa283 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#import <Cocoa/Cocoa.h>
#import <CoreFoundation/CoreFoundation.h>
#include "crashreporter.h"
#include "crashreporter_osx.h"
#include <crt_externs.h>
#include <spawn.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sstream>

using std::ostringstream;
using std::string;
using std::vector;

using namespace CrashReporter;

static NSAutoreleasePool* gMainPool;
static CrashReporterUI* gUI = 0;
static StringTable gFiles;
static Json::Value gQueryParameters;
static string gURLParameter;
static string gSendURL;
static vector<string> gRestartArgs;
static bool gDidTrySend = false;
static bool gRTLlayout = false;

static cpu_type_t pref_cpu_types[2] = {
#if defined(__i386__)
    CPU_TYPE_X86,
#elif defined(__x86_64__)
    CPU_TYPE_X86_64,
#elif defined(__ppc__)
    CPU_TYPE_POWERPC,
#elif defined(__aarch64__)
    CPU_TYPE_ARM64,
#endif
    CPU_TYPE_ANY};

#define NSSTR(s) [NSString stringWithUTF8String:(s).c_str()]

static NSString* Str(const char* aName) {
  string str = gStrings[aName];
  if (str.empty()) str = "?";
  return NSSTR(str);
}

static bool RestartApplication() {
  vector<char*> argv(gRestartArgs.size() + 1);

  posix_spawnattr_t spawnattr;
  if (posix_spawnattr_init(&spawnattr) != 0) {
    return false;
  }

  // Set spawn attributes.
  size_t attr_count = sizeof(pref_cpu_types) / sizeof(pref_cpu_types[0]);
  size_t attr_ocount = 0;
  if (posix_spawnattr_setbinpref_np(&spawnattr, attr_count, pref_cpu_types, &attr_ocount) != 0 ||
      attr_ocount != attr_count) {
    posix_spawnattr_destroy(&spawnattr);
    return false;
  }

  unsigned int i;
  for (i = 0; i < gRestartArgs.size(); i++) {
    argv[i] = (char*)gRestartArgs[i].c_str();
  }
  argv[i] = 0;

  char** env = NULL;
  char*** nsEnv = _NSGetEnviron();
  if (nsEnv) env = *nsEnv;
  int result = posix_spawnp(NULL, argv[0], NULL, &spawnattr, &argv[0], env);

  posix_spawnattr_destroy(&spawnattr);

  return result == 0;
}

@implementation CrashReporterUI

- (void)awakeFromNib {
  gUI = self;
  [mWindow center];

  [mWindow setTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"]];
  [NSApp activateIgnoringOtherApps:YES];
}

- (void)showCrashUI:(const StringTable&)files
    queryParameters:(const Json::Value&)queryParameters
            sendURL:(const string&)sendURL {
  gFiles = files;
  gQueryParameters = queryParameters;
  gSendURL = sendURL;

  if (gAutoSubmit) {
    gDidTrySend = true;
    [self sendReport];
    return;
  }

  [mWindow setTitle:Str(ST_CRASHREPORTERTITLE)];
  [mHeaderLabel setStringValue:Str(ST_CRASHREPORTERHEADER)];

  NSRect viewReportFrame = [mViewReportButton frame];
  [mViewReportButton setTitle:Str(ST_VIEWREPORT)];
  [mViewReportButton sizeToFit];
  if (gRTLlayout) {
    // sizeToFit will keep the left side fixed, so realign
    float oldWidth = viewReportFrame.size.width;
    viewReportFrame = [mViewReportButton frame];
    viewReportFrame.origin.x += oldWidth - viewReportFrame.size.width;
    [mViewReportButton setFrame:viewReportFrame];
  }

  [mSubmitReportButton setTitle:Str(ST_CHECKSUBMIT)];
  [mIncludeURLButton setTitle:Str(ST_CHECKURL)];
  [mViewReportOkButton setTitle:Str(ST_OK)];

  [mCommentText setPlaceholder:Str(ST_COMMENTGRAYTEXT)];
  if (gRTLlayout) [mCommentText toggleBaseWritingDirection:self];

  if (gQueryParameters.isMember("URL")) {
    // save the URL value in case the checkbox gets unchecked
    gURLParameter = gQueryParameters["URL"].asString();
  } else {
    // no URL specified, hide checkbox
    [mIncludeURLButton removeFromSuperview];
    // shrink window to fit
    NSRect frame = [mWindow frame];
    NSRect includeURLFrame = [mIncludeURLButton frame];
    NSRect emailFrame = [mEmailMeButton frame];
    int buttonMask = [mViewReportButton autoresizingMask];
    int checkMask = [mSubmitReportButton autoresizingMask];
    int commentScrollMask = [mCommentScrollView autoresizingMask];

    [mViewReportButton setAutoresizingMask:NSViewMinYMargin];
    [mSubmitReportButton setAutoresizingMask:NSViewMinYMargin];
    [mCommentScrollView setAutoresizingMask:NSViewMinYMargin];

    // remove all the space in between
    frame.size.height -= includeURLFrame.origin.y - emailFrame.origin.y;
    [mWindow setFrame:frame display:true animate:NO];

    [mViewReportButton setAutoresizingMask:buttonMask];
    [mSubmitReportButton setAutoresizingMask:checkMask];
    [mCommentScrollView setAutoresizingMask:commentScrollMask];
  }

  // resize some buttons horizontally and possibly some controls vertically
  [self doInitialResizing];

  // load default state of submit checkbox
  // we don't just do this via IB because we want the default to be
  // off a certain percentage of the time
  BOOL submitChecked = YES;
  NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
  if (nil != [userDefaults objectForKey:@"submitReport"]) {
    submitChecked = [userDefaults boolForKey:@"submitReport"];
  } else {
    [userDefaults setBool:submitChecked forKey:@"submitReport"];
  }
  [mSubmitReportButton setState:(submitChecked ? NSOnState : NSOffState)];

  // load default state of include URL checkbox
  BOOL includeChecked = YES;
  if (nil != [userDefaults objectForKey:@"IncludeURL"]) {
    includeChecked = [userDefaults boolForKey:@"IncludeURL"];
  } else {
    [userDefaults setBool:includeChecked forKey:@"IncludeURL"];
  }
  [mIncludeURLButton setState:(includeChecked ? NSOnState : NSOffState)];

  [self updateSubmit];
  [self updateURL];
  [self updateEmail];

  [mWindow makeKeyAndOrderFront:nil];
}

- (void)showErrorUI:(const string&)message {
  [self setView:mErrorView animate:NO];

  [mErrorHeaderLabel setStringValue:Str(ST_CRASHREPORTERHEADER)];
  [self setStringFitVertically:mErrorLabel string:NSSTR(message) resizeWindow:YES];
  [mErrorCloseButton setTitle:Str(ST_OK)];

  [mErrorCloseButton setKeyEquivalent:@"\r"];
  [mWindow makeFirstResponder:mErrorCloseButton];
  [mWindow makeKeyAndOrderFront:nil];
}

- (void)showReportInfo {
  NSDictionary* boldAttr = @{
    NSFontAttributeName : [NSFont boldSystemFontOfSize:[NSFont smallSystemFontSize]],
    NSForegroundColorAttributeName : NSColor.textColor,
  };
  NSDictionary* normalAttr = @{
    NSFontAttributeName : [NSFont systemFontOfSize:[NSFont smallSystemFontSize]],
    NSForegroundColorAttributeName : NSColor.textColor,
  };

  [mViewReportTextView setString:@""];
  for (Json::ValueConstIterator iter = gQueryParameters.begin(); iter != gQueryParameters.end();
       ++iter) {
    NSAttributedString* key = [[NSAttributedString alloc] initWithString:NSSTR(iter.name() + ": ")
                                                              attributes:boldAttr];
    string str;
    if (iter->isString()) {
      str = iter->asString();
    } else {
      Json::StreamWriterBuilder builder;
      builder["indentation"] = "";
      str = writeString(builder, *iter);
    }
    NSAttributedString* value = [[NSAttributedString alloc] initWithString:NSSTR(str + "\n")
                                                                attributes:normalAttr];
    [[mViewReportTextView textStorage] appendAttributedString:key];
    [[mViewReportTextView textStorage] appendAttributedString:value];
    [key release];
    [value release];
  }

  NSAttributedString* extra =
      [[NSAttributedString alloc] initWithString:NSSTR("\n" + gStrings[ST_EXTRAREPORTINFO])
                                      attributes:normalAttr];
  [[mViewReportTextView textStorage] appendAttributedString:extra];
  [extra release];
}

- (void)maybeSubmitReport {
  if ([mSubmitReportButton state] == NSOnState) {
    [self setStringFitVertically:mProgressText string:Str(ST_REPORTDURINGSUBMIT) resizeWindow:YES];
    // disable all the controls
    [self enableControls:NO];
    [mSubmitReportButton setEnabled:NO];
    [mRestartButton setEnabled:NO];
    [mCloseButton setEnabled:NO];
    [mProgressIndicator startAnimation:self];
    gDidTrySend = true;
    [self sendReport];
  } else {
    [NSApp terminate:self];
  }
}

- (void)closeMeDown:(id)unused {
  [NSApp terminate:self];
}

- (IBAction)submitReportClicked:(id)sender {
  [self updateSubmit];
  NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
  [userDefaults setBool:([mSubmitReportButton state] == NSOnState) forKey:@"submitReport"];
  [userDefaults synchronize];
}

- (IBAction)viewReportClicked:(id)sender {
  [self showReportInfo];
  [NSApp beginSheet:mViewReportWindow
      modalForWindow:mWindow
       modalDelegate:nil
      didEndSelector:nil
         contextInfo:nil];
}

- (IBAction)viewReportOkClicked:(id)sender {
  [mViewReportWindow orderOut:nil];
  [NSApp endSheet:mViewReportWindow];
}

- (IBAction)closeClicked:(id)sender {
  [self maybeSubmitReport];
}

- (IBAction)restartClicked:(id)sender {
  RestartApplication();
  [self maybeSubmitReport];
}

- (IBAction)includeURLClicked:(id)sender {
  [self updateURL];
  NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
  [userDefaults setBool:([mIncludeURLButton state] == NSOnState) forKey:@"IncludeURL"];
  [userDefaults synchronize];
}

- (void)textDidChange:(NSNotification*)aNotification {
  // update comment parameter
  if ([[[mCommentText textStorage] mutableString] length] > 0)
    gQueryParameters["Comments"] = [[[mCommentText textStorage] mutableString] UTF8String];
  else
    gQueryParameters.removeMember("Comments");
}

// Limit the comment field to 500 bytes in UTF-8
- (BOOL)textView:(NSTextView*)aTextView
    shouldChangeTextInRange:(NSRange)affectedCharRange
          replacementString:(NSString*)replacementString {
  // current string length + replacement text length - replaced range length
  if (([[aTextView string] lengthOfBytesUsingEncoding:NSUTF8StringEncoding] +
       [replacementString lengthOfBytesUsingEncoding:NSUTF8StringEncoding] -
       [[[aTextView string] substringWithRange:affectedCharRange]
           lengthOfBytesUsingEncoding:NSUTF8StringEncoding]) > MAX_COMMENT_LENGTH) {
    return NO;
  }
  return YES;
}

- (void)doInitialResizing {
  NSRect windowFrame = [mWindow frame];
  NSRect restartFrame = [mRestartButton frame];
  NSRect closeFrame = [mCloseButton frame];
  // resize close button to fit text
  float oldCloseWidth = closeFrame.size.width;
  [mCloseButton setTitle:Str(ST_QUIT)];
  [mCloseButton sizeToFit];
  closeFrame = [mCloseButton frame];
  // move close button left if it grew
  if (!gRTLlayout) {
    closeFrame.origin.x -= closeFrame.size.width - oldCloseWidth;
  }

  if (gRestartArgs.size() == 0) {
    [mRestartButton removeFromSuperview];
    if (!gRTLlayout) {
      closeFrame.origin.x =
          restartFrame.origin.x + (restartFrame.size.width - closeFrame.size.width);
    } else {
      closeFrame.origin.x = restartFrame.origin.x;
    }
    [mCloseButton setFrame:closeFrame];
    [mCloseButton setKeyEquivalent:@"\r"];
  } else {
    [mRestartButton setTitle:Str(ST_RESTART)];
    // resize "restart" button
    float oldRestartWidth = restartFrame.size.width;
    [mRestartButton sizeToFit];
    restartFrame = [mRestartButton frame];
    if (!gRTLlayout) {
      // move left by the amount that the button grew
      restartFrame.origin.x -= restartFrame.size.width - oldRestartWidth;
      closeFrame.origin.x -= restartFrame.size.width - oldRestartWidth;
    } else {
      // shift the close button right in RTL
      closeFrame.origin.x += restartFrame.size.width - oldRestartWidth;
    }
    [mRestartButton setFrame:restartFrame];
    [mCloseButton setFrame:closeFrame];
    // possibly resize window if both buttons no longer fit
    // leave 20 px from either side of the window, and 12 px
    // between the buttons
    float neededWidth = closeFrame.size.width + restartFrame.size.width + 2 * 20 + 12;

    if (neededWidth > windowFrame.size.width) {
      windowFrame.size.width = neededWidth;
      [mWindow setFrame:windowFrame display:true animate:NO];
    }
    [mRestartButton setKeyEquivalent:@"\r"];
  }

  NSButton* checkboxes[] = {mSubmitReportButton, mIncludeURLButton};

  for (auto checkbox : checkboxes) {
    NSRect frame = [checkbox frame];
    [checkbox sizeToFit];
    if (gRTLlayout) {
      // sizeToFit will keep the left side fixed, so realign
      float oldWidth = frame.size.width;
      frame = [checkbox frame];
      frame.origin.x += oldWidth - frame.size.width;
      [checkbox setFrame:frame];
    }
    // keep existing spacing on left side, + 20 px spare on right
    float neededWidth = frame.origin.x + checkbox.intrinsicContentSize.width + 20;
    if (neededWidth > windowFrame.size.width) {
      windowFrame.size.width = neededWidth;
      [mWindow setFrame:windowFrame display:true animate:NO];
    }
  }

  // do this down here because we may have made the window wider
  // up above
  [self setStringFitVertically:mDescriptionLabel
                        string:Str(ST_CRASHREPORTERDESCRIPTION)
                  resizeWindow:YES];

  // now pin all the controls (except quit/submit) in place,
  // if we lengthen the window after this, it's just to lengthen
  // the progress text, so nothing above that text should move.
  NSView* views[] = {mSubmitReportButton, mViewReportButton,  mCommentScrollView,
                     mIncludeURLButton,   mProgressIndicator, mProgressText};
  for (auto view : views) {
    [view setAutoresizingMask:NSViewMinYMargin];
  }
}

- (float)setStringFitVertically:(NSControl*)control
                         string:(NSString*)str
                   resizeWindow:(BOOL)resizeWindow {
  // hack to make the text field grow vertically
  NSRect frame = [control frame];
  float oldHeight = frame.size.height;

  frame.size.height = 10000;
  NSSize oldCellSize = [[control cell] cellSizeForBounds:frame];
  [control setStringValue:str];
  NSSize newCellSize = [[control cell] cellSizeForBounds:frame];

  float delta = newCellSize.height - oldCellSize.height;
  frame.origin.y -= delta;
  frame.size.height = oldHeight + delta;
  [control setFrame:frame];

  if (resizeWindow) {
    NSRect frame = [mWindow frame];
    frame.origin.y -= delta;
    frame.size.height += delta;
    [mWindow setFrame:frame display:true animate:NO];
  }

  return delta;
}

- (void)setView:(NSView*)v animate:(BOOL)animate {
  NSRect frame = [mWindow frame];

  NSRect oldViewFrame = [[mWindow contentView] frame];
  NSRect newViewFrame = [v frame];

  frame.origin.y += oldViewFrame.size.height - newViewFrame.size.height;
  frame.size.height -= oldViewFrame.size.height - newViewFrame.size.height;

  frame.origin.x += oldViewFrame.size.width - newViewFrame.size.width;
  frame.size.width -= oldViewFrame.size.width - newViewFrame.size.width;

  [mWindow setContentView:v];
  [mWindow setFrame:frame display:true animate:animate];
}

- (void)enableControls:(BOOL)enabled {
  [mViewReportButton setEnabled:enabled];
  [mIncludeURLButton setEnabled:enabled];
  [mCommentText setEnabled:enabled];
  [mCommentScrollView setHasVerticalScroller:enabled];
}

- (void)updateSubmit {
  if ([mSubmitReportButton state] == NSOnState) {
    [self setStringFitVertically:mProgressText string:Str(ST_REPORTPRESUBMIT) resizeWindow:YES];
    [mProgressText setHidden:NO];
    // enable all the controls
    [self enableControls:YES];
  } else {
    // not submitting, disable all the controls under
    // the submit checkbox, and hide the status text
    [mProgressText setHidden:YES];
    [self enableControls:NO];
  }
}

- (void)updateURL {
  if ([mIncludeURLButton state] == NSOnState && !gURLParameter.empty()) {
    gQueryParameters["URL"] = gURLParameter;
  } else {
    gQueryParameters.removeMember("URL");
  }
}

- (void)updateEmail {
  // In order to remove the email fields, we have to edit the .nib files which
  // we can't do with current xcode so we make them hidden; updating the
  // crashreporter interface for mac is covered in bug #1696164
  [mEmailMeButton setHidden:YES];
  [mEmailText setHidden:YES];
}

- (void)sendReport {
  if (![self setupPost]) {
    LogMessage("Crash report submission failed: could not set up POST data");

    if (gAutoSubmit) {
      [NSApp terminate:self];
    }

    [self setStringFitVertically:mProgressText string:Str(ST_SUBMITFAILED) resizeWindow:YES];
    // quit after 5 seconds
    [self performSelector:@selector(closeMeDown:) withObject:nil afterDelay:5.0];
  }

  [NSThread detachNewThreadSelector:@selector(uploadThread:) toTarget:self withObject:mPost];
}

- (bool)setupPost {
  NSURL* url =
      [NSURL URLWithString:[NSSTR(gSendURL)
                               stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  if (!url) return false;

  mPost = [[HTTPMultipartUpload alloc] initWithURL:url];
  if (!mPost) return false;

  for (StringTable::const_iterator i = gFiles.begin(); i != gFiles.end(); i++) {
    [mPost addFileAtPath:NSSTR(i->second) name:NSSTR(i->first)];
  }

  Json::StreamWriterBuilder builder;
  builder["indentation"] = "";
  string output = writeString(builder, gQueryParameters).append("\r\n");
  NSMutableString* parameters = [[NSMutableString alloc] initWithUTF8String:output.c_str()];

  [mPost setParameters:parameters];
  [parameters release];

  return true;
}

- (void)uploadComplete:(NSData*)data {
  NSHTTPURLResponse* response = [mPost response];
  [mPost release];

  bool success;
  string reply;
  if (!data || !response || [response statusCode] != 200) {
    success = false;
    reply = "";

    // if data is nil, we probably logged an error in uploadThread
    if (data != nil && response != nil) {
      ostringstream message;
      message << "Crash report submission failed: server returned status " << [response statusCode];
      LogMessage(message.str());
    }
  } else {
    success = true;
    LogMessage("Crash report submitted successfully");

    NSString* encodingName = [response textEncodingName];
    NSStringEncoding encoding;
    if (encodingName) {
      encoding = CFStringConvertEncodingToNSStringEncoding(
          CFStringConvertIANACharSetNameToEncoding((CFStringRef)encodingName));
    } else {
      encoding = NSISOLatin1StringEncoding;
    }
    NSString* r = [[NSString alloc] initWithData:data encoding:encoding];
    reply = [r UTF8String];
    [r release];
  }

  SendCompleted(success, reply);

  if (gAutoSubmit) {
    [NSApp terminate:self];
  }

  [mProgressIndicator stopAnimation:self];
  if (success) {
    [self setStringFitVertically:mProgressText string:Str(ST_REPORTSUBMITSUCCESS) resizeWindow:YES];
  } else {
    [self setStringFitVertically:mProgressText string:Str(ST_SUBMITFAILED) resizeWindow:YES];
  }
  // quit after 5 seconds
  [self performSelector:@selector(closeMeDown:) withObject:nil afterDelay:5.0];
}

- (void)uploadThread:(HTTPMultipartUpload*)post {
  NSAutoreleasePool* autoreleasepool = [[NSAutoreleasePool alloc] init];
  NSError* error = nil;
  NSData* data = [post send:&error];
  if (error) {
    data = nil;
    NSString* errorDesc = [error localizedDescription];
    string message = [errorDesc UTF8String];
    LogMessage("Crash report submission failed: " + message);
  }

  [self performSelectorOnMainThread:@selector(uploadComplete:) withObject:data waitUntilDone:YES];

  [autoreleasepool release];
}

// to get auto-quit when we close the window
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)theApplication {
  return YES;
}

- (void)applicationWillTerminate:(NSNotification*)aNotification {
  // since we use [NSApp terminate:] we never return to main,
  // so do our cleanup here
  if (!gDidTrySend) DeleteDump();
}

@end

@implementation TextViewWithPlaceHolder

- (BOOL)becomeFirstResponder {
  [self setNeedsDisplay:YES];
  return [super becomeFirstResponder];
}

- (void)drawRect:(NSRect)rect {
  [super drawRect:rect];
  if (mPlaceHolderString && [[self string] isEqualToString:@""] &&
      self != [[self window] firstResponder])
    [mPlaceHolderString drawInRect:[self frame]];
}

- (BOOL)resignFirstResponder {
  [self setNeedsDisplay:YES];
  return [super resignFirstResponder];
}

- (void)setPlaceholder:(NSString*)placeholder {
  NSColor* txtColor = [NSColor disabledControlTextColor];
  NSDictionary* txtDict =
      [NSDictionary dictionaryWithObjectsAndKeys:txtColor, NSForegroundColorAttributeName, nil];
  mPlaceHolderString = [[NSMutableAttributedString alloc] initWithString:placeholder
                                                              attributes:txtDict];
  if (gRTLlayout)
    [mPlaceHolderString setAlignment:NSTextAlignmentRight
                               range:NSMakeRange(0, [placeholder length])];
}

- (void)insertTab:(id)sender {
  // don't actually want to insert tabs, just tab to next control
  [[self window] selectNextKeyView:sender];
}

- (void)insertBacktab:(id)sender {
  [[self window] selectPreviousKeyView:sender];
}

- (void)setEnabled:(BOOL)enabled {
  [self setSelectable:enabled];
  [self setEditable:enabled];
  if (![[self string] isEqualToString:@""]) {
    NSAttributedString* colorString;
    NSColor* txtColor;
    if (enabled)
      txtColor = [NSColor textColor];
    else
      txtColor = [NSColor disabledControlTextColor];
    NSDictionary* txtDict =
        [NSDictionary dictionaryWithObjectsAndKeys:txtColor, NSForegroundColorAttributeName, nil];
    colorString = [[NSAttributedString alloc] initWithString:[self string] attributes:txtDict];
    [[self textStorage] setAttributedString:colorString];
    [self setInsertionPointColor:txtColor];
    [colorString release];
  }
}

- (void)dealloc {
  [mPlaceHolderString release];
  [super dealloc];
}

@end

/* === Crashreporter UI Functions === */

bool UIInit() {
  gMainPool = [[NSAutoreleasePool alloc] init];
  [NSApplication sharedApplication];

  if (gStrings.find("isRTL") != gStrings.end() && gStrings["isRTL"] == "yes") gRTLlayout = true;

  if (gAutoSubmit) {
    gUI = [[CrashReporterUI alloc] init];
  } else {
    [[NSBundle mainBundle] loadNibNamed:(gRTLlayout ? @"MainMenuRTL" : @"MainMenu")
                                  owner:NSApp
                        topLevelObjects:nil];
  }

  return true;
}

void UIShutdown() { [gMainPool release]; }

void UIShowDefaultUI() {
  [gUI showErrorUI:gStrings[ST_CRASHREPORTERDEFAULT]];
  [NSApp run];
}

bool UIShowCrashUI(const StringTable& files, const Json::Value& queryParameters,
                   const string& sendURL, const vector<string>& restartArgs) {
  gRestartArgs = restartArgs;

  [gUI showCrashUI:files queryParameters:queryParameters sendURL:sendURL];
  [NSApp run];

  return gDidTrySend;
}

void UIError_impl(const string& message) {
  if (!gUI) {
    // UI failed to initialize, printing is the best we can do
    printf("Error: %s\n", message.c_str());
    return;
  }

  [gUI showErrorUI:message];
  [NSApp run];
}

bool UIGetIniPath(string& path) {
  NSString* tmpPath = [NSString stringWithUTF8String:gArgv[0]];
  NSString* iniName = [tmpPath lastPathComponent];
  iniName = [iniName stringByAppendingPathExtension:@"ini"];
  tmpPath = [tmpPath stringByDeletingLastPathComponent];
  tmpPath = [tmpPath stringByDeletingLastPathComponent];
  tmpPath = [tmpPath stringByAppendingPathComponent:@"Resources"];
  tmpPath = [tmpPath stringByAppendingPathComponent:iniName];
  path = [tmpPath UTF8String];
  return true;
}

bool UIGetSettingsPath(const string& vendor, const string& product, string& settingsPath) {
  NSArray* paths =
      NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
  NSString* destPath = [paths firstObject];

  // Note that MacOS ignores the vendor when creating the profile hierarchy -
  // all application preferences directories live alongside one another in
  // ~/Library/Application Support/
  destPath = [destPath stringByAppendingPathComponent:NSSTR(product)];
  // Thunderbird stores its profile in ~/Library/Thunderbird,
  // but we're going to put stuff in ~/Library/Application Support/Thunderbird
  // anyway, so we have to ensure that path exists.
  string tempPath = [destPath UTF8String];
  if (!UIEnsurePathExists(tempPath)) return false;

  destPath = [destPath stringByAppendingPathComponent:@"Crash Reports"];

  settingsPath = [destPath UTF8String];

  return true;
}

bool UIMoveFile(const string& file, const string& newfile) {
  if (!rename(file.c_str(), newfile.c_str())) return true;
  if (errno != EXDEV) return false;

  NSFileManager* fileManager = [NSFileManager defaultManager];
  NSString* source = [fileManager stringWithFileSystemRepresentation:file.c_str()
                                                              length:file.length()];
  NSString* dest = [fileManager stringWithFileSystemRepresentation:newfile.c_str()
                                                            length:newfile.length()];
  if (!source || !dest) return false;

  [fileManager moveItemAtPath:source toPath:dest error:NULL];
  return UIFileExists(newfile);
}