Home | History | Annotate | Download | only in cocoa
      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 "grit/chromium_strings.h"
     10 #include "grit/generated_resources.h"
     11 #include "grit/ui_strings.h"
     12 #include "ui/base/l10n/l10n_util.h"
     13 #include "ui/base/l10n/l10n_util_mac.h"
     14 
     15 // Helper to clean up after the notification that the alert was dismissed.
     16 @interface RestartHelper : NSObject {
     17  @private
     18   NSAlert* alert_;
     19 }
     20 - (NSAlert*)alert;
     21 - (void)alertDidEnd:(NSAlert*)alert
     22          returnCode:(int)returnCode
     23         contextInfo:(void*)contextInfo;
     24 @end
     25 
     26 @implementation RestartHelper
     27 
     28 - (NSAlert*)alert {
     29   alert_ = [[NSAlert alloc] init];
     30   return alert_;
     31 }
     32 
     33 - (void)dealloc {
     34   [alert_ release];
     35   [super dealloc];
     36 }
     37 
     38 - (void)alertDidEnd:(NSAlert*)alert
     39          returnCode:(int)returnCode
     40         contextInfo:(void*)contextInfo {
     41   if (returnCode == NSAlertFirstButtonReturn) {
     42     chrome::AttemptRestart();
     43   } else if (returnCode == NSAlertSecondButtonReturn) {
     44     // Nothing to do. User will restart later.
     45   } else {
     46     NOTREACHED();
     47   }
     48   [self autorelease];
     49 }
     50 
     51 @end
     52 
     53 namespace restart_browser {
     54 
     55 void RequestRestart(NSWindow* parent) {
     56   NSString* title =
     57       l10n_util::GetNSStringFWithFixup(IDS_PLEASE_RELAUNCH_BROWSER,
     58           l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
     59   NSString* text =
     60       l10n_util::GetNSStringFWithFixup(IDS_UPDATE_RECOMMENDED,
     61           l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
     62   NSString* notNowButton = l10n_util::GetNSStringWithFixup(IDS_NOT_NOW);
     63   NSString* restartButton =
     64       l10n_util::GetNSStringWithFixup(IDS_RELAUNCH_AND_UPDATE);
     65 
     66   RestartHelper* helper = [[RestartHelper alloc] init];
     67 
     68   NSAlert* alert = [helper alert];
     69   [alert setAlertStyle:NSInformationalAlertStyle];
     70   [alert setMessageText:title];
     71   [alert setInformativeText:text];
     72   [alert addButtonWithTitle:restartButton];
     73   [alert addButtonWithTitle:notNowButton];
     74 
     75   if (parent) {
     76     [alert beginSheetModalForWindow:parent
     77                       modalDelegate:helper
     78                      didEndSelector:@selector(alertDidEnd:
     79                                                returnCode:
     80                                               contextInfo:)
     81                         contextInfo:nil];
     82   } else {
     83     NSInteger returnCode = [alert runModal];
     84     [helper alertDidEnd:alert
     85              returnCode:returnCode
     86             contextInfo:NULL];
     87   }
     88 }
     89 
     90 }  // namespace restart_browser
     91