Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / ios / chrome / browser / geolocation / omnibox_geolocation_authorization_alert.mm
blob244c07b6ceef5f07c2e7f8c1ed05f14867493c0c
1 // Copyright 2013 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 #include "ios/chrome/browser/geolocation/omnibox_geolocation_authorization_alert.h"
7 #import <UIKit/UIKit.h>
9 #import "base/ios/weak_nsobject.h"
10 #include "base/logging.h"
11 #include "base/mac/scoped_nsobject.h"
12 #include "base/strings/sys_string_conversions.h"
13 #include "ios/chrome/grit/ios_strings_resources.h"
14 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h"
15 #include "ios/public/provider/chrome/browser/string_provider.h"
16 #include "ui/base/l10n/l10n_util_mac.h"
18 @interface OmniboxGeolocationAuthorizationAlert () <UIAlertViewDelegate> {
19   base::WeakNSProtocol<id<OmniboxGeolocationAuthorizationAlertDelegate>>
20       delegate_;
23 @end
25 @implementation OmniboxGeolocationAuthorizationAlert
27 - (instancetype)initWithDelegate:
28         (id<OmniboxGeolocationAuthorizationAlertDelegate>)delegate {
29   self = [super init];
30   if (self) {
31     delegate_.reset(delegate);
32   }
33   return self;
36 - (instancetype)init {
37   return [self initWithDelegate:nil];
40 - (id<OmniboxGeolocationAuthorizationAlertDelegate>)delegate {
41   return delegate_.get();
44 - (void)setDelegate:(id<OmniboxGeolocationAuthorizationAlertDelegate>)delegate {
45   delegate_.reset(delegate);
48 - (void)showAuthorizationAlert {
49   NSString* message =
50       l10n_util::GetNSString(IDS_IOS_LOCATION_AUTHORIZATION_ALERT);
51   NSString* cancel = l10n_util::GetNSString(IDS_IOS_LOCATION_USAGE_CANCEL);
52   NSString* ok = base::SysUTF16ToNSString(
53       ios::GetChromeBrowserProvider()->GetStringProvider()->GetOKString());
55   // Use a UIAlertView to match the style of the iOS system location alert.
56   base::scoped_nsobject<UIAlertView> alertView(
57       [[UIAlertView alloc] initWithTitle:nil
58                                  message:message
59                                 delegate:self
60                        cancelButtonTitle:cancel
61                        otherButtonTitles:ok, nil]);
63   [alertView show];
66 #pragma mark - UIAlertViewDelegate
68 - (void)alertView:(UIAlertView*)alertView
69     clickedButtonAtIndex:(NSInteger)buttonIndex {
70   switch (buttonIndex) {
71     case 0:
72       [delegate_ authorizationAlertDidCancel:self];
73       break;
74     case 1:
75       [delegate_ authorizationAlertDidAuthorize:self];
76       break;
77     default:
78       NOTREACHED();
79       break;
80   }
83 @end