Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / restart_browser.mm
blob8887d39315218221de08b8654240751b50ea3870
1 // Copyright (c) 2011 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.
5 #import "chrome/browser/ui/cocoa/restart_browser.h"
7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/lifetime/application_lifetime.h"
9 #include "chrome/grit/chromium_strings.h"
10 #include "chrome/grit/generated_resources.h"
11 #include "ui/base/l10n/l10n_util.h"
12 #include "ui/base/l10n/l10n_util_mac.h"
14 // Helper to clean up after the notification that the alert was dismissed.
15 @interface RestartHelper : NSObject {
16  @private
17   NSAlert* alert_;
19 - (NSAlert*)alert;
20 - (void)alertDidEnd:(NSAlert*)alert
21          returnCode:(int)returnCode
22         contextInfo:(void*)contextInfo;
23 @end
25 @implementation RestartHelper
27 - (NSAlert*)alert {
28   alert_ = [[NSAlert alloc] init];
29   return alert_;
32 - (void)dealloc {
33   [alert_ release];
34   [super dealloc];
37 - (void)alertDidEnd:(NSAlert*)alert
38          returnCode:(int)returnCode
39         contextInfo:(void*)contextInfo {
40   if (returnCode == NSAlertFirstButtonReturn) {
41     chrome::AttemptRestart();
42   } else if (returnCode == NSAlertSecondButtonReturn) {
43     // Nothing to do. User will restart later.
44   } else {
45     NOTREACHED();
46   }
47   [self autorelease];
50 @end
52 namespace restart_browser {
54 void RequestRestart(NSWindow* parent) {
55   NSString* title =
56       l10n_util::GetNSStringFWithFixup(IDS_PLEASE_RELAUNCH_BROWSER,
57           l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
58   NSString* text =
59       l10n_util::GetNSStringFWithFixup(IDS_UPDATE_RECOMMENDED,
60           l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
61   NSString* notNowButton = l10n_util::GetNSStringWithFixup(IDS_NOT_NOW);
62   NSString* restartButton =
63       l10n_util::GetNSStringWithFixup(IDS_RELAUNCH_AND_UPDATE);
65   RestartHelper* helper = [[RestartHelper alloc] init];
67   NSAlert* alert = [helper alert];
68   [alert setAlertStyle:NSInformationalAlertStyle];
69   [alert setMessageText:title];
70   [alert setInformativeText:text];
71   [alert addButtonWithTitle:restartButton];
72   [alert addButtonWithTitle:notNowButton];
74   if (parent) {
75     [alert beginSheetModalForWindow:parent
76                       modalDelegate:helper
77                      didEndSelector:@selector(alertDidEnd:
78                                                returnCode:
79                                               contextInfo:)
80                         contextInfo:nil];
81   } else {
82     NSInteger returnCode = [alert runModal];
83     [helper alertDidEnd:alert
84              returnCode:returnCode
85             contextInfo:NULL];
86   }
89 }  // namespace restart_browser