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