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 #import "chrome/browser/ui/cocoa/autofill/autofill_sign_in_container.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/mac/foundation_util.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/autofill/autofill_dialog_sign_in_delegate.h"
13 #import "chrome/browser/ui/cocoa/browser_window_utils.h"
14 #include "chrome/browser/ui/cocoa/chrome_event_processing_window.h"
15 #include "chrome/browser/ui/chrome_style.h"
16 #include "chrome/browser/ui/cocoa/autofill/autofill_dialog_cocoa.h"
17 #include "components/autofill/content/browser/wallet/wallet_service_url.h"
18 #include "content/public/browser/native_web_keyboard_event.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/browser/web_contents_view.h"
24 // Platform version of the sign-in delegate, allows handling of hotkeys.
25 class CocoaSignInDelegate : public autofill::AutofillDialogSignInDelegate {
27 CocoaSignInDelegate(autofill::AutofillDialogView* dialog_view,
28 content::WebContents* dialog_web_contents,
29 content::WebContents* originating_web_contents,
30 const gfx::Size& minimum_size,
31 const gfx::Size& maximum_size,
33 : AutofillDialogSignInDelegate(dialog_view,
35 originating_web_contents,
40 // WebContentsDelegate implementation. Forwards all unhandled keyboard events
41 // to the current window.
42 virtual void HandleKeyboardEvent(
43 content::WebContents* source,
44 const content::NativeWebKeyboardEvent& event) OVERRIDE;
47 NSView* view_; // WebContentsView, used to redispatch key events.
50 void CocoaSignInDelegate::HandleKeyboardEvent(
51 content::WebContents* source,
52 const content::NativeWebKeyboardEvent& event) {
53 if (![BrowserWindowUtils shouldHandleKeyboardEvent:event])
56 // Not invoking +[BrowserWindowUtils handleKeyboardEvent here], since the
57 // window in question is a ConstrainedWindowCustomWindow, not a BrowserWindow.
58 ChromeEventProcessingWindow* event_window =
59 base::mac::ObjCCastStrict<ChromeEventProcessingWindow>([view_ window]);
60 [event_window redispatchKeyEvent:event.os_event];
65 @implementation AutofillSignInContainer
67 @synthesize preferredSize = preferredSize_;
69 - (id)initWithDialog:(autofill::AutofillDialogCocoa*)dialog {
70 if (self = [super init]) {
78 content::WebContents::Create(
79 content::WebContents::CreateParams(dialog_->delegate()->profile())));
80 NSView* webContentView = webContents_->GetView()->GetNativeView();
81 [self setView:webContentView];
84 - (void)loadSignInPage {
85 DCHECK(webContents_.get());
87 // Ensure initial minimum size doesn't cause resize.
88 NSSize initialMinSize = [[self view] frame].size;
90 // Ensure |maxSize_| is bigger than |initialMinSize|.
91 maxSize_.height = std::max(maxSize_.height, initialMinSize.height);
92 maxSize_.width = std::max(maxSize_.width, initialMinSize.width);
94 signInDelegate_.reset(
95 new CocoaSignInDelegate(
98 dialog_->delegate()->GetWebContents(),
99 gfx::Size(NSSizeToCGSize(initialMinSize)),
100 gfx::Size(NSSizeToCGSize(maxSize_)),
102 webContents_->GetController().LoadURL(
103 dialog_->delegate()->SignInUrl(),
105 content::PAGE_TRANSITION_AUTO_TOPLEVEL,
109 - (content::NavigationController*)navigationController {
110 return &webContents_->GetController();
113 - (content::WebContents*)webContents {
114 return webContents_.get();
117 - (void)constrainSizeToMinimum:(NSSize)minSize maximum:(NSSize)maxSize {
121 // Constrain the web view to be a little shorter than the given sizes, leaving
122 // room for some padding below the web view.
123 minSize_.height -= chrome_style::kClientBottomPadding;
124 maxSize_.height -= chrome_style::kClientBottomPadding;
126 // Notify the web contents of its new auto-resize limits.
127 if (signInDelegate_ && ![[self view] isHidden]) {
128 signInDelegate_->UpdateLimitsAndEnableAutoResize(
129 gfx::Size(NSSizeToCGSize(minSize_)),
130 gfx::Size(NSSizeToCGSize(maxSize_)));
134 - (void)setPreferredSize:(NSSize)size {
135 // The |size| is the preferred size requested by the web view. Tack onto that
136 // a bit of extra padding at the bottom.
137 preferredSize_ = size;
138 preferredSize_.height += chrome_style::kClientBottomPadding;
140 // Always request re-layout if preferredSize changes.
141 id delegate = [[[self view] window] windowController];
142 if ([delegate respondsToSelector:@selector(requestRelayout)])
143 [delegate performSelector:@selector(requestRelayout)];