summaryrefslogtreecommitdiffstats
path: root/components/autofill/content/browser/autocheckout_manager.h
blob: eec00f2678e701408ec5278b6848df3863e0a5df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_AUTOFILL_CONTENT_BROWSER_AUTOCHECKOUT_MANAGER_H_
#define COMPONENTS_AUTOFILL_CONTENT_BROWSER_AUTOCHECKOUT_MANAGER_H_

#include <string>

#include "base/callback_forward.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string16.h"
#include "base/threading/thread_checker.h"
#include "base/time.h"
#include "components/autofill/content/browser/autocheckout_page_meta_data.h"
#include "components/autofill/content/browser/autocheckout_statistic.h"
#include "components/autofill/core/common/autocheckout_status.h"

class GURL;

namespace content {
struct SSLStatus;
}

namespace gfx {
class RectF;
}

namespace net {
class URLRequestContextGetter;
}

namespace autofill {

class AutofillField;
class AutofillManager;
class AutofillMetrics;
class AutofillProfile;
class CreditCard;
class FormStructure;

struct FormData;
struct FormFieldData;

class AutocheckoutManager {
 public:
  explicit AutocheckoutManager(AutofillManager* autofill_manager);
  virtual ~AutocheckoutManager();

  // Fill all the forms seen by the Autofill manager with the information
  // gathered from the requestAutocomplete dialog.
  void FillForms();

  // Called when clicking a proceed element in an Autocheckout flow fails.
  // |status| is the reason for the failure.
  void OnClickFailed(AutocheckoutStatus status);

  // Sets |page_meta_data_| with the meta data for the current page.
  void OnLoadedPageMetaData(
      scoped_ptr<AutocheckoutPageMetaData> page_meta_data);

  // Called when a page containing forms is loaded.
  void OnFormsSeen();

  // Whether ajax on the current page should be ignored during
  // an Autocheckout flow.
  bool ShouldIgnoreAjax();

  // Causes the Autocheckout bubble to be displayed if the user hasn't seen it
  // yet for the current page. |frame_url| is the page where Autocheckout is
  // being initiated. |ssl_status| is the SSL status of the page. |bounding_box|
  // is the bounding box of the input field in focus.
  virtual void MaybeShowAutocheckoutBubble(const GURL& frame_url,
                                           const content::SSLStatus& ssl_status,
                                           const gfx::RectF& bounding_box);

  bool is_autocheckout_bubble_showing() const {
    return is_autocheckout_bubble_showing_;
  }

 protected:
  // Exposed for testing.
  bool in_autocheckout_flow() const { return in_autocheckout_flow_; }

  // Exposed for testing.
  bool autocheckout_offered() const { return autocheckout_offered_; }

  // Show the requestAutocomplete dialog if |show_dialog| is true. Also, does
  // bookkeeping for whether or not the bubble is showing.
  virtual void MaybeShowAutocheckoutDialog(const GURL& frame_url,
                                           const content::SSLStatus& ssl_status,
                                           bool show_dialog);

  // Callback called from AutofillDialogController on filling up the UI form.
  void ReturnAutocheckoutData(const FormStructure* result,
                              const std::string& google_transaction_id);

  const AutofillMetrics& metric_logger() const { return *metric_logger_; }
  void set_metric_logger(scoped_ptr<AutofillMetrics> metric_logger);

 private:
  // Shows the Autocheckout bubble. Must be called on the UI thread. |frame_url|
  // is the page where Autocheckout is being initiated. |ssl_status| is the SSL
  // status of the page. |bounding_box| is the bounding box of the input field
  // in focus. |cookies| is any Google Account cookies.
  void ShowAutocheckoutBubble(const GURL& frame_url,
                              const content::SSLStatus& ssl_status,
                              const gfx::RectF& bounding_box,
                              const std::string& cookies);

  // Whether or not the current page is the start of a multipage Autofill flow.
  bool IsStartOfAutofillableFlow() const;

  // Whether or not the current page is part of a multipage Autofill flow.
  bool IsInAutofillableFlow() const;

  // Sends |status| to Online Wallet using AutocheckoutRequestManager.
  void SendAutocheckoutStatus(AutocheckoutStatus status);

  // Sets value of form field data |field_to_fill| based on the Autofill
  // field type specified by |field|.
  void SetValue(const AutofillField& field, FormFieldData* field_to_fill);

  // Sets the progress of all steps for the given page to the provided value.
  void SetStepProgressForPage(int page_number, AutocheckoutStepStatus status);

  // Account time spent between now and |last_step_completion_timestamp_|
  // towards |page_number|.
  void RecordTimeTaken(int page_number);

  AutofillManager* autofill_manager_;  // WEAK; owns us

  // Credit card verification code.
  base::string16 cvv_;

  // Profile built using the data supplied by requestAutocomplete dialog.
  scoped_ptr<AutofillProfile> profile_;

  // Credit card built using the data supplied by requestAutocomplete dialog.
  scoped_ptr<CreditCard> credit_card_;

  // Billing address built using data supplied by requestAutocomplete dialog.
  scoped_ptr<AutofillProfile> billing_address_;

  // Autocheckout specific page meta data.
  scoped_ptr<AutocheckoutPageMetaData> page_meta_data_;

  scoped_ptr<AutofillMetrics> metric_logger_;

  // Whether or not the Autocheckout bubble has been displayed to the user for
  // the current forms. Ensures the Autocheckout bubble is only shown to a
  // user once per pageview.
  bool autocheckout_offered_;

  // Whether or not the Autocheckout bubble is being displayed to the user.
  bool is_autocheckout_bubble_showing_;

  // Whether or not the user is in an Autocheckout flow.
  bool in_autocheckout_flow_;

  // AutocheckoutStepTypes for the various pages of the flow.
  std::map<int, std::vector<AutocheckoutStepType> > page_types_;

  // Timestamp of last step's completion.
  base::TimeTicks last_step_completion_timestamp_;

  // Per page latency statistics.
  std::vector<AutocheckoutStatistic> latency_statistics_;

  std::string google_transaction_id_;

  base::WeakPtrFactory<AutocheckoutManager> weak_ptr_factory_;

  base::ThreadChecker thread_checker_;

  DISALLOW_COPY_AND_ASSIGN(AutocheckoutManager);
};

}  // namespace autofill

#endif  // COMPONENTS_AUTOFILL_CONTENT_BROWSER_AUTOCHECKOUT_MANAGER_H_