1// Copyright (c) 2012 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#ifndef CHROME_BROWSER_UI_WEBUI_OPTIONS_AUTOFILL_OPTIONS_HANDLER_H_
6#define CHROME_BROWSER_UI_WEBUI_OPTIONS_AUTOFILL_OPTIONS_HANDLER_H_
7
8#include <string>
9
10#include "base/compiler_specific.h"
11#include "chrome/browser/ui/webui/options/options_ui.h"
12#include "components/autofill/core/browser/personal_data_manager_observer.h"
13
14namespace autofill {
15class AutofillProfile;
16class PersonalDataManager;
17}  // namespace autofill
18
19namespace base {
20class DictionaryValue;
21class ListValue;
22}
23
24namespace options {
25
26class AutofillOptionsHandler : public OptionsPageUIHandler,
27                               public autofill::PersonalDataManagerObserver {
28 public:
29  AutofillOptionsHandler();
30  virtual ~AutofillOptionsHandler();
31
32  // OptionsPageUIHandler implementation.
33  virtual void GetLocalizedValues(
34      base::DictionaryValue* localized_strings) OVERRIDE;
35  virtual void InitializeHandler() OVERRIDE;
36  virtual void InitializePage() OVERRIDE;
37  virtual void RegisterMessages() OVERRIDE;
38
39  // PersonalDataManagerObserver implementation.
40  virtual void OnPersonalDataChanged() OVERRIDE;
41
42 private:
43  FRIEND_TEST_ALL_PREFIXES(AutofillOptionsHandlerTest, AddressToDictionary);
44
45  // Loads the strings for the address and credit card overlays.
46  void SetAddressOverlayStrings(base::DictionaryValue* localized_strings);
47  void SetCreditCardOverlayStrings(base::DictionaryValue* localized_strings);
48
49  // Loads Autofill addresses and credit cards using the PersonalDataManager.
50  void LoadAutofillData();
51
52#if defined(OS_MACOSX) && !defined(OS_IOS)
53  // The user wants to grant Chrome access to the user's Address Book.
54  // Immediately try to access the Address Book so that the blocking dialog is
55  // shown in context, rather than at a later, surprising time.
56  void AccessAddressBook(const base::ListValue* args);
57#endif  // defined(OS_MACOSX) && !defined(OS_IOS)
58
59  // Removes data from the PersonalDataManager.
60  // |args| - A string, the GUID of the address or credit card to remove.
61  void RemoveData(const base::ListValue* args);
62
63  // Requests profile data for a specific address. Calls into WebUI with the
64  // loaded profile data to open the address editor.
65  // |args| - A string, the GUID of the address to load.
66  void LoadAddressEditor(const base::ListValue* args);
67
68  // Requests input form layout information for a specific country code. Calls
69  // into WebUI with the layout information.
70  // |args| - A string, the country code to load.
71  void LoadAddressEditorComponents(const base::ListValue* args);
72
73  // Requests profile data for a specific credit card. Calls into WebUI with the
74  // loaded profile data to open the credit card editor.
75  // |args| - A string, the GUID of the credit card to load.
76  void LoadCreditCardEditor(const base::ListValue* args);
77
78  // Adds or updates an address, depending on the GUID of the profile. If the
79  // GUID is empty, a new address is added to the WebDatabase; otherwise, the
80  // address with the matching GUID is updated. Called from WebUI.
81  // |args| - an array containing the GUID of the address followed by the
82  // address data.
83  void SetAddress(const base::ListValue* args);
84
85  // Adds or updates a credit card, depending on the GUID of the profile. If the
86  // GUID is empty, a new credit card is added to the WebDatabase; otherwise,
87  // the credit card with the matching GUID is updated. Called from WebUI.
88  // |args| - an array containing the GUID of the credit card followed by the
89  // credit card data.
90  void SetCreditCard(const base::ListValue* args);
91
92  // Validates a list of phone numbers.  The resulting validated list of
93  // numbers is then sent back to the WebUI.
94  // |args| - an array containing the index of the modified or added number, the
95  // array of numbers, and the country code string set on the profile.
96  void ValidatePhoneNumbers(const base::ListValue* args);
97
98  // Returns true if |personal_data_| is non-null and loaded.
99  bool IsPersonalDataLoaded() const;
100
101  // Fills in |address| with the data format that the options js expects.
102  static void AutofillProfileToDictionary(
103      const autofill::AutofillProfile& profile,
104      base::DictionaryValue* address);
105
106  // The personal data manager, used to load Autofill profiles and credit cards.
107  // Unowned pointer, may not be NULL.
108  autofill::PersonalDataManager* personal_data_;
109
110  DISALLOW_COPY_AND_ASSIGN(AutofillOptionsHandler);
111};
112
113}  // namespace options
114
115#endif  // CHROME_BROWSER_UI_WEBUI_OPTIONS_AUTOFILL_OPTIONS_HANDLER_H_
116