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"
23 // Platform version of the sign-in delegate, allows handling of hotkeys.
24 class CocoaSignInDelegate : public autofill::AutofillDialogSignInDelegate {
26 CocoaSignInDelegate(autofill::AutofillDialogView* dialog_view,
27 content::WebContents* dialog_web_contents,
28 content::WebContents* originating_web_contents,
29 const gfx::Size& minimum_size,
30 const gfx::Size& maximum_size,
32 : AutofillDialogSignInDelegate(dialog_view,
34 originating_web_contents,
39 // WebContentsDelegate implementation. Forwards all unhandled keyboard events
40 // to the current window.
41 void HandleKeyboardEvent(
42 content::WebContents* source,
43 const content::NativeWebKeyboardEvent& event) override;
46 NSView* view_; // WebContentsView, used to redispatch key events.
49 void CocoaSignInDelegate::HandleKeyboardEvent(
50 content::WebContents* source,
51 const content::NativeWebKeyboardEvent& event) {
52 if (![BrowserWindowUtils shouldHandleKeyboardEvent:event])
55 // Not invoking +[BrowserWindowUtils handleKeyboardEvent here], since the
56 // window in question is a ConstrainedWindowCustomWindow, not a BrowserWindow.
57 ChromeEventProcessingWindow* event_window =
58 base::mac::ObjCCastStrict<ChromeEventProcessingWindow>([view_ window]);
59 [event_window redispatchKeyEvent:event.os_event];
64 @implementation AutofillSignInContainer
66 @synthesize preferredSize = preferredSize_;
68 - (id)initWithDialog:(autofill::AutofillDialogCocoa*)dialog {
69 if (self = [super init]) {
77 content::WebContents::Create(
78 content::WebContents::CreateParams(dialog_->delegate()->profile())));
79 NSView* webContentView = webContents_->GetNativeView();
80 [self setView:webContentView];
83 - (void)loadSignInPage:(const GURL&)url {
84 DCHECK(webContents_.get());
86 // Ensure initial minimum size doesn't cause resize.
87 NSSize initialMinSize = [[self view] frame].size;
89 // Ensure |maxSize_| is bigger than |initialMinSize|.
90 maxSize_.height = std::max(maxSize_.height, initialMinSize.height);
91 maxSize_.width = std::max(maxSize_.width, initialMinSize.width);
93 signInDelegate_.reset(
94 new CocoaSignInDelegate(
97 dialog_->delegate()->GetWebContents(),
98 gfx::Size(NSSizeToCGSize(initialMinSize)),
99 gfx::Size(NSSizeToCGSize(maxSize_)),
101 webContents_->GetController().LoadURL(url,
103 ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
107 - (content::NavigationController*)navigationController {
108 return &webContents_->GetController();
111 - (content::WebContents*)webContents {
112 return webContents_.get();
115 - (void)constrainSizeToMinimum:(NSSize)minSize maximum:(NSSize)maxSize {
119 // Constrain the web view to be a little shorter than the given sizes, leaving
120 // room for some padding below the web view.
121 minSize_.height -= chrome_style::kClientBottomPadding;
122 maxSize_.height -= chrome_style::kClientBottomPadding;
124 // Notify the web contents of its new auto-resize limits.
125 if (signInDelegate_ && ![[self view] isHidden]) {
126 signInDelegate_->UpdateLimitsAndEnableAutoResize(
127 gfx::Size(NSSizeToCGSize(minSize_)),
128 gfx::Size(NSSizeToCGSize(maxSize_)));
132 - (void)setPreferredSize:(NSSize)size {
133 // The |size| is the preferred size requested by the web view. Tack onto that
134 // a bit of extra padding at the bottom.
135 preferredSize_ = size;
136 preferredSize_.height += chrome_style::kClientBottomPadding;
138 // Always request re-layout if preferredSize changes.
139 id delegate = [[[self view] window] windowController];
140 if ([delegate respondsToSelector:@selector(requestRelayout)])
141 [delegate performSelector:@selector(requestRelayout)];