autofill_dialog_cocoa.mm revision f2477e01787aa58f445919b809d89e252beef54f
1// Copyright (c) 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.
4
5#include "chrome/browser/ui/cocoa/autofill/autofill_dialog_cocoa.h"
6
7#include "base/bind.h"
8#include "base/mac/scoped_nsobject.h"
9#include "base/message_loop/message_loop.h"
10#include "base/strings/sys_string_conversions.h"
11#include "chrome/browser/ui/autofill/autofill_dialog_view_delegate.h"
12#import "chrome/browser/ui/cocoa/autofill/autofill_details_container.h"
13#import "chrome/browser/ui/cocoa/autofill/autofill_dialog_window_controller.h"
14#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_sheet.h"
15
16namespace autofill {
17
18// static
19AutofillDialogView* AutofillDialogView::Create(
20    AutofillDialogViewDelegate* delegate) {
21  return new AutofillDialogCocoa(delegate);
22}
23
24AutofillDialogCocoa::AutofillDialogCocoa(AutofillDialogViewDelegate* delegate)
25    : close_weak_ptr_factory_(this),
26      delegate_(delegate) {
27}
28
29AutofillDialogCocoa::~AutofillDialogCocoa() {
30}
31
32void AutofillDialogCocoa::Show() {
33  // This should only be called once.
34  DCHECK(!sheet_delegate_.get());
35  sheet_delegate_.reset([[AutofillDialogWindowController alloc]
36       initWithWebContents:delegate_->GetWebContents()
37                    dialog:this]);
38  base::scoped_nsobject<CustomConstrainedWindowSheet> sheet(
39      [[CustomConstrainedWindowSheet alloc]
40          initWithCustomWindow:[sheet_delegate_ window]]);
41  constrained_window_.reset(
42      new ConstrainedWindowMac(this, delegate_->GetWebContents(), sheet));
43  [sheet_delegate_ show];
44}
45
46void AutofillDialogCocoa::Hide() {
47  [sheet_delegate_ hide];
48}
49
50void AutofillDialogCocoa::PerformClose() {
51  if (!close_weak_ptr_factory_.HasWeakPtrs()) {
52    base::MessageLoop::current()->PostTask(
53        FROM_HERE,
54        base::Bind(&AutofillDialogCocoa::CloseNow,
55                   close_weak_ptr_factory_.GetWeakPtr()));
56  }
57}
58
59void AutofillDialogCocoa::CloseNow() {
60  constrained_window_->CloseWebContentsModalDialog();
61}
62
63void AutofillDialogCocoa::UpdatesStarted() {
64}
65
66void AutofillDialogCocoa::UpdatesFinished() {
67}
68
69void AutofillDialogCocoa::UpdateAccountChooser() {
70  [sheet_delegate_ updateAccountChooser];
71}
72
73void AutofillDialogCocoa::UpdateButtonStrip() {
74  [sheet_delegate_ updateButtonStrip];
75}
76
77void AutofillDialogCocoa::UpdateOverlay() {
78  // TODO(estade): only update the overlay.
79  UpdateButtonStrip();
80}
81
82void AutofillDialogCocoa::UpdateDetailArea() {
83}
84
85void AutofillDialogCocoa::UpdateForErrors() {
86}
87
88void AutofillDialogCocoa::UpdateNotificationArea() {
89  [sheet_delegate_ updateNotificationArea];
90}
91
92void AutofillDialogCocoa::UpdateSection(DialogSection section) {
93  [sheet_delegate_ updateSection:section];
94}
95
96void AutofillDialogCocoa::FillSection(DialogSection section,
97                                      const DetailInput& originating_input) {
98  [sheet_delegate_ fillSection:section forInput:originating_input];
99}
100
101void AutofillDialogCocoa::GetUserInput(DialogSection section,
102                                       FieldValueMap* output) {
103  [sheet_delegate_ getInputs:output forSection:section];
104}
105
106string16 AutofillDialogCocoa::GetCvc() {
107  return base::SysNSStringToUTF16([sheet_delegate_ getCvc]);
108}
109
110bool AutofillDialogCocoa::HitTestInput(const DetailInput& input,
111                                       const gfx::Point& screen_point) {
112  // TODO(dbeam): implement.
113  return false;
114}
115
116bool AutofillDialogCocoa::SaveDetailsLocally() {
117  return [sheet_delegate_ saveDetailsLocally];
118}
119
120const content::NavigationController* AutofillDialogCocoa::ShowSignIn() {
121  return [sheet_delegate_ showSignIn];
122}
123
124void AutofillDialogCocoa::HideSignIn() {
125  [sheet_delegate_ hideSignIn];
126}
127
128void AutofillDialogCocoa::ModelChanged() {
129  [sheet_delegate_ modelChanged];
130}
131
132void AutofillDialogCocoa::UpdateErrorBubble() {
133  [sheet_delegate_ updateErrorBubble];
134}
135
136TestableAutofillDialogView* AutofillDialogCocoa::GetTestableView() {
137  return this;
138}
139
140void AutofillDialogCocoa::OnSignInResize(const gfx::Size& pref_size) {
141  [sheet_delegate_ onSignInResize:
142      NSMakeSize(pref_size.width(), pref_size.height())];
143}
144
145void AutofillDialogCocoa::SubmitForTesting() {
146  [sheet_delegate_ accept:nil];
147}
148
149void AutofillDialogCocoa::CancelForTesting() {
150  [sheet_delegate_ cancel:nil];
151}
152
153string16 AutofillDialogCocoa::GetTextContentsOfInput(const DetailInput& input) {
154  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
155    DialogSection section = static_cast<DialogSection>(i);
156    FieldValueMap contents;
157    [sheet_delegate_ getInputs:&contents forSection:section];
158    FieldValueMap::const_iterator it = contents.find(input.type);
159    if (it != contents.end())
160      return it->second;
161  }
162
163  NOTREACHED();
164  return string16();
165}
166
167void AutofillDialogCocoa::SetTextContentsOfInput(const DetailInput& input,
168                                                 const string16& contents) {
169  [sheet_delegate_ setTextContents:base::SysUTF16ToNSString(contents)
170                          forInput:input];
171}
172
173void AutofillDialogCocoa::SetTextContentsOfSuggestionInput(
174    DialogSection section,
175    const base::string16& text) {
176  [sheet_delegate_ setTextContents:base::SysUTF16ToNSString(text)
177            ofSuggestionForSection:section];
178}
179
180void AutofillDialogCocoa::ActivateInput(const DetailInput& input) {
181  [sheet_delegate_ activateFieldForInput:input];
182}
183
184gfx::Size AutofillDialogCocoa::GetSize() const {
185  return gfx::Size(NSSizeToCGSize([[sheet_delegate_ window] frame].size));
186}
187
188content::WebContents* AutofillDialogCocoa::GetSignInWebContents() {
189  return [sheet_delegate_ getSignInWebContents];
190}
191
192
193bool AutofillDialogCocoa::IsShowingOverlay() const {
194  return [sheet_delegate_ isShowingOverlay];
195}
196
197void AutofillDialogCocoa::OnConstrainedWindowClosed(
198    ConstrainedWindowMac* window) {
199  constrained_window_.reset();
200  // |this| belongs to |delegate_|, so no self-destruction here.
201  delegate_->ViewClosed();
202}
203
204}  // autofill
205