Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / restart_browser.mm
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.
4
5 #import "chrome/browser/ui/cocoa/restart_browser.h"
6
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"
13
14 // Helper to clean up after the notification that the alert was dismissed.
15 @interface RestartHelper : NSObject {
16  @private
17   NSAlert* alert_;
18 }
19 - (NSAlert*)alert;
20 - (void)alertDidEnd:(NSAlert*)alert
21          returnCode:(int)returnCode
22         contextInfo:(void*)contextInfo;
23 @end
24
25 @implementation RestartHelper
26
27 - (NSAlert*)alert {
28   alert_ = [[NSAlert alloc] init];
29   return alert_;
30 }
31
32 - (void)dealloc {
33   [alert_ release];
34   [super dealloc];
35 }
36
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];
48 }
49
50 @end
51
52 namespace restart_browser {
53
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);
64
65   RestartHelper* helper = [[RestartHelper alloc] init];
66
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];
73
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   }
87 }
88
89 }  // namespace restart_browser