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.
5 #include "chrome/browser/ui/cocoa/autofill/autofill_dialog_cocoa.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"
19 AutofillDialogView* AutofillDialogView::Create(
20 AutofillDialogViewDelegate* delegate) {
21 return new AutofillDialogCocoa(delegate);
24 AutofillDialogCocoa::AutofillDialogCocoa(AutofillDialogViewDelegate* delegate)
25 : close_weak_ptr_factory_(this),
29 AutofillDialogCocoa::~AutofillDialogCocoa() {
32 void AutofillDialogCocoa::Show() {
33 // This should only be called once.
34 DCHECK(!sheet_delegate_.get());
35 sheet_delegate_.reset([[AutofillDialogWindowController alloc]
36 initWithWebContents:delegate_->GetWebContents()
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];
46 void AutofillDialogCocoa::Hide() {
47 [sheet_delegate_ hide];
50 void AutofillDialogCocoa::PerformClose() {
51 if (!close_weak_ptr_factory_.HasWeakPtrs()) {
52 base::MessageLoop::current()->PostTask(
54 base::Bind(&AutofillDialogCocoa::CloseNow,
55 close_weak_ptr_factory_.GetWeakPtr()));
59 void AutofillDialogCocoa::CloseNow() {
60 constrained_window_->CloseWebContentsModalDialog();
63 void AutofillDialogCocoa::UpdatesStarted() {
66 void AutofillDialogCocoa::UpdatesFinished() {
69 void AutofillDialogCocoa::UpdateAccountChooser() {
70 [sheet_delegate_ updateAccountChooser];
73 void AutofillDialogCocoa::UpdateButtonStrip() {
74 [sheet_delegate_ updateButtonStrip];
77 void AutofillDialogCocoa::UpdateOverlay() {
78 // TODO(estade): only update the overlay.
82 void AutofillDialogCocoa::UpdateDetailArea() {
85 void AutofillDialogCocoa::UpdateForErrors() {
86 [sheet_delegate_ updateForErrors];
89 void AutofillDialogCocoa::UpdateNotificationArea() {
90 [sheet_delegate_ updateNotificationArea];
93 void AutofillDialogCocoa::UpdateSection(DialogSection section) {
94 [sheet_delegate_ updateSection:section];
97 void AutofillDialogCocoa::FillSection(DialogSection section,
98 ServerFieldType originating_type) {
99 [sheet_delegate_ fillSection:section forType:originating_type];
102 void AutofillDialogCocoa::GetUserInput(DialogSection section,
103 FieldValueMap* output) {
104 [sheet_delegate_ getInputs:output forSection:section];
107 base::string16 AutofillDialogCocoa::GetCvc() {
108 return base::SysNSStringToUTF16([sheet_delegate_ getCvc]);
111 bool AutofillDialogCocoa::HitTestInput(ServerFieldType type,
112 const gfx::Point& screen_point) {
113 // TODO(dbeam): implement.
117 bool AutofillDialogCocoa::SaveDetailsLocally() {
118 return [sheet_delegate_ saveDetailsLocally];
121 const content::NavigationController* AutofillDialogCocoa::ShowSignIn() {
122 return [sheet_delegate_ showSignIn];
125 void AutofillDialogCocoa::HideSignIn() {
126 [sheet_delegate_ hideSignIn];
129 void AutofillDialogCocoa::ModelChanged() {
130 [sheet_delegate_ modelChanged];
133 void AutofillDialogCocoa::UpdateErrorBubble() {
134 [sheet_delegate_ updateErrorBubble];
137 TestableAutofillDialogView* AutofillDialogCocoa::GetTestableView() {
141 void AutofillDialogCocoa::OnSignInResize(const gfx::Size& pref_size) {
142 [sheet_delegate_ onSignInResize:
143 NSMakeSize(pref_size.width(), pref_size.height())];
146 void AutofillDialogCocoa::SubmitForTesting() {
147 [sheet_delegate_ accept:nil];
150 void AutofillDialogCocoa::CancelForTesting() {
151 [sheet_delegate_ cancel:nil];
154 base::string16 AutofillDialogCocoa::GetTextContentsOfInput(
155 ServerFieldType type) {
156 for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
157 DialogSection section = static_cast<DialogSection>(i);
158 FieldValueMap contents;
159 [sheet_delegate_ getInputs:&contents forSection:section];
160 FieldValueMap::const_iterator it = contents.find(type);
161 if (it != contents.end())
166 return base::string16();
169 void AutofillDialogCocoa::SetTextContentsOfInput(
170 ServerFieldType type,
171 const base::string16& contents) {
172 [sheet_delegate_ setTextContents:base::SysUTF16ToNSString(contents)
176 void AutofillDialogCocoa::SetTextContentsOfSuggestionInput(
177 DialogSection section,
178 const base::string16& text) {
179 [sheet_delegate_ setTextContents:base::SysUTF16ToNSString(text)
180 ofSuggestionForSection:section];
183 void AutofillDialogCocoa::ActivateInput(ServerFieldType type) {
184 [sheet_delegate_ activateFieldForType:type];
187 gfx::Size AutofillDialogCocoa::GetSize() const {
188 return gfx::Size(NSSizeToCGSize([[sheet_delegate_ window] frame].size));
191 content::WebContents* AutofillDialogCocoa::GetSignInWebContents() {
192 return [sheet_delegate_ getSignInWebContents];
196 bool AutofillDialogCocoa::IsShowingOverlay() const {
197 return [sheet_delegate_ isShowingOverlay];
200 void AutofillDialogCocoa::OnConstrainedWindowClosed(
201 ConstrainedWindowMac* window) {
202 constrained_window_.reset();
203 // |this| belongs to |delegate_|, so no self-destruction here.
204 delegate_->ViewClosed();