Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / autofill / autofill_sign_in_container.mm
blob92fd47e847b0ed38df03d5ac3caf1cdbb62c0c94
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"
22 namespace {
24 // Platform version of the sign-in delegate, allows handling of hotkeys.
25 class CocoaSignInDelegate : public autofill::AutofillDialogSignInDelegate {
26  public:
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,
32                       NSView* view)
33       : AutofillDialogSignInDelegate(dialog_view,
34                                      dialog_web_contents,
35                                      originating_web_contents,
36                                      minimum_size,
37                                      maximum_size),
38         view_(view) {}
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;
46  private:
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])
54     return;
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];
63 }  // namespace
65 @implementation AutofillSignInContainer
67 @synthesize preferredSize = preferredSize_;
69 - (id)initWithDialog:(autofill::AutofillDialogCocoa*)dialog {
70   if (self = [super init]) {
71     dialog_ = dialog;
72   }
73   return self;
76 - (void)loadView {
77   webContents_.reset(
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(
96           dialog_,
97           webContents_.get(),
98           dialog_->delegate()->GetWebContents(),
99           gfx::Size(NSSizeToCGSize(initialMinSize)),
100           gfx::Size(NSSizeToCGSize(maxSize_)),
101           [self view]));
102   webContents_->GetController().LoadURL(
103       dialog_->delegate()->SignInUrl(),
104       content::Referrer(),
105       content::PAGE_TRANSITION_AUTO_TOPLEVEL,
106       std::string());
109 - (content::NavigationController*)navigationController {
110   return &webContents_->GetController();
113 - (content::WebContents*)webContents {
114   return webContents_.get();
117 - (void)constrainSizeToMinimum:(NSSize)minSize maximum:(NSSize)maxSize {
118   minSize_ = minSize;
119   maxSize_ = 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_)));
131   }
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)];
146 @end