1eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Copyright 2013 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_EXTERNAL_DELEGATE_H_
6eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#define COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_EXTERNAL_DELEGATE_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/weak_ptr.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string16.h"
13eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_popup_delegate.h"
147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "components/autofill/core/common/form_data.h"
157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "components/autofill/core/common/form_field_data.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/rect.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace autofill {
19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochclass AutofillDriver;
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class AutofillManager;
22c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO(csharp): A lot of the logic in this class is copied from autofillagent.
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Once Autofill is moved out of WebKit this class should be the only home for
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this logic. See http://crbug.com/51644
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Delegate for in-browser Autocomplete and Autofill display and selection.
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AutofillExternalDelegate
29eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    : public AutofillPopupDelegate {
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Creates an AutofillExternalDelegate for the specified AutofillManager and
32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // AutofillDriver.
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AutofillExternalDelegate(AutofillManager* manager,
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                           AutofillDriver* driver);
357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual ~AutofillExternalDelegate();
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // AutofillPopupDelegate implementation.
380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  virtual void OnPopupShown() OVERRIDE;
390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  virtual void OnPopupHidden() OVERRIDE;
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void DidSelectSuggestion(const base::string16& value,
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                   int identifier) OVERRIDE;
42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void DidAcceptSuggestion(const base::string16& value,
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   int identifier) OVERRIDE;
44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void RemoveSuggestion(const base::string16& value,
45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                int identifier) OVERRIDE;
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void ClearPreviewedForm() OVERRIDE;
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Records and associates a query_id with web form data.  Called
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // when the renderer posts an Autofill query to the browser. |bounds|
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is window relative. |display_warning_if_disabled| tells us if we should
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // display warnings (such as autofill is disabled, but had suggestions).
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We might not want to display the warning if a website has disabled
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Autocomplete because they have their own popup, and showing our popup
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // on to of theirs would be a poor user experience.
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnQuery(int query_id,
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       const FormData& form,
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       const FormFieldData& field,
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       const gfx::RectF& element_bounds,
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       bool display_warning_if_disabled);
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Records query results and correctly formats them before sending them off
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to be displayed.  Called when an Autofill query result is available.
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnSuggestionsReturned(
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int query_id,
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const std::vector<base::string16>& values,
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const std::vector<base::string16>& labels,
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const std::vector<base::string16>& icons,
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const std::vector<int>& unique_ids);
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set the data list value associated with the current field.
71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void SetCurrentDataListValues(
72ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      const std::vector<base::string16>& data_list_values,
73ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      const std::vector<base::string16>& data_list_labels);
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Inform the delegate that the text field editing has ended. This is
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // used to help record the metrics of when a new popup is shown.
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void DidEndTextFieldEditing();
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the delegate to its starting state by removing any page specific
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // values or settings.
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Reset();
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // The renderer sent an IPC acknowledging an earlier ping IPC.
8446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void OnPingAck();
8546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
87c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::WeakPtr<AutofillExternalDelegate> GetWeakPtr();
88c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Fills the form with the Autofill data corresponding to |unique_id|.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If |is_preview| is true then this is just a preview to show the user what
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // would be selected and if |is_preview| is false then the user has selected
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // this data.
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void FillAutofillFormData(int unique_id, bool is_preview);
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Handle applying any Autofill warnings to the Autofill popup.
97a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void ApplyAutofillWarnings(std::vector<base::string16>* values,
98a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                             std::vector<base::string16>* labels,
99a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                             std::vector<base::string16>* icons,
100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                             std::vector<int>* unique_ids);
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Handle applying any Autofill option listings to the Autofill popup.
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This function should only get called when there is at least one
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // multi-field suggestion in the list of suggestions.
105a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void ApplyAutofillOptions(std::vector<base::string16>* values,
106a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                            std::vector<base::string16>* labels,
107a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                            std::vector<base::string16>* icons,
108a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                            std::vector<int>* unique_ids);
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Insert the data list values at the start of the given list, including
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // any required separators.
112a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void InsertDataListValues(std::vector<base::string16>* values,
113a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                            std::vector<base::string16>* labels,
114a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                            std::vector<base::string16>* icons,
115a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                            std::vector<int>* unique_ids);
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#if defined(OS_MACOSX) && !defined(OS_IOS)
11846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Pings the renderer.
11946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void PingRenderer();
12046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#endif  // defined(OS_MACOSX) && !defined(OS_IOS)
12146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
122a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AutofillManager* manager_;  // weak.
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Provides driver-level context to the shared code of the component. Must
1257dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // outlive this object.
126a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AutofillDriver* driver_;  // weak
1277dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The ID of the last request sent for form field Autofill.  Used to ignore
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // out of date responses.
130a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  int query_id_;
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The current form and field selected by Autofill.
133a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  FormData query_form_;
134a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  FormFieldData query_field_;
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The bounds of the form field that user is interacting with.
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::RectF element_bounds_;
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should we display a warning if Autofill is disabled?
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool display_warning_if_disabled_;
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Does the popup include any Autofill profile or credit card suggestions?
143a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool has_suggestion_;
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Have we already shown Autofill suggestions for the field the user is
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // currently editing?  Used to keep track of state for metrics logging.
147a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool has_shown_popup_for_current_edit_;
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The current data list values.
150c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::vector<base::string16> data_list_values_;
151c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::vector<base::string16> data_list_labels_;
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
153c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::WeakPtrFactory<AutofillExternalDelegate> weak_ptr_factory_;
154c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Whether the access Address Book prompt has ever been shown for the current
1561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // |query_form_|. This variable is only used on OSX.
1571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  bool has_shown_address_book_prompt;
1581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(AutofillExternalDelegate);
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
162c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace autofill
163c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
164eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_EXTERNAL_DELEGATE_H_
165