1// Copyright 2014 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#include "chrome/browser/ui/autofill/chrome_autofill_client.h"
6
7#import <Cocoa/Cocoa.h>
8
9#include "base/logging.h"
10#include "base/mac/scoped_nsobject.h"
11#import "chrome/browser/mac/keystone_glue.h"
12#include "components/autofill/core/browser/personal_data_manager.h"
13
14// Listens to Keystone notifications and passes relevant ones on to the
15// PersonalDataManager.
16@interface AutofillKeystoneBridge : NSObject {
17 @private
18  // The PersonalDataManager, if it exists, is expected to outlive this object.
19  autofill::PersonalDataManager* personal_data_manager_;
20}
21
22// Designated initializer. The PersonalDataManager, if it exists, is expected
23// to outlive this object. The PersonalDataManager may be NULL in tests.
24- (instancetype)initWithPersonalDataManager:
25    (autofill::PersonalDataManager*)personal_data_manager;
26
27// Receieved a notification from Keystone.
28- (void)handleStatusNotification:(NSNotification*)notification;
29@end
30
31@implementation AutofillKeystoneBridge
32
33- (instancetype)init {
34  NOTREACHED();
35  return nil;
36}
37
38- (instancetype)initWithPersonalDataManager:
39    (autofill::PersonalDataManager*)personal_data_manager {
40  self = [super init];
41  if (self) {
42    personal_data_manager_ = personal_data_manager;
43    NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
44    [center addObserver:self
45               selector:@selector(handleStatusNotification:)
46                   name:kAutoupdateStatusNotification
47                 object:nil];
48  }
49  return self;
50}
51
52- (void)dealloc {
53  [[NSNotificationCenter defaultCenter] removeObserver:self];
54  [super dealloc];
55}
56
57- (void)handleStatusNotification:(NSNotification*)notification {
58  if (!personal_data_manager_)
59    return;
60
61  NSNumber* statusNumber =
62      [[notification userInfo] objectForKey:kAutoupdateStatusStatus];
63  DCHECK(statusNumber);
64  DCHECK([statusNumber isKindOfClass:[NSNumber class]]);
65  AutoupdateStatus status =
66      static_cast<AutoupdateStatus>([statusNumber intValue]);
67
68  switch (status) {
69    case kAutoupdateInstalling:
70    case kAutoupdateInstalled: {
71      personal_data_manager_->BinaryChanging();
72      return;
73    }
74    default:
75      return;
76  }
77}
78
79@end
80
81namespace autofill {
82
83class AutofillKeystoneBridgeWrapper {
84 public:
85  explicit AutofillKeystoneBridgeWrapper(
86      PersonalDataManager* personal_data_manager) {
87    bridge_.reset([[AutofillKeystoneBridge alloc]
88        initWithPersonalDataManager:personal_data_manager]);
89  }
90  ~AutofillKeystoneBridgeWrapper() {}
91
92 private:
93  base::scoped_nsobject<AutofillKeystoneBridge> bridge_;
94
95  DISALLOW_COPY_AND_ASSIGN(AutofillKeystoneBridgeWrapper);
96};
97
98void ChromeAutofillClient::RegisterForKeystoneNotifications() {
99  bridge_wrapper_ = new AutofillKeystoneBridgeWrapper(GetPersonalDataManager());
100}
101
102void ChromeAutofillClient::UnregisterFromKeystoneNotifications() {
103  delete bridge_wrapper_;
104}
105
106}  // namespace autofill
107