7293c4b3f718e7e732d7571c161a80513b0065d4
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / autofill / autofill_sign_in_container.mm
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 #import "chrome/browser/ui/cocoa/autofill/autofill_sign_in_container.h"
6
7 #import <Cocoa/Cocoa.h>
8
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
21 namespace {
22
23 // Platform version of the sign-in delegate, allows handling of hotkeys.
24 class CocoaSignInDelegate : public autofill::AutofillDialogSignInDelegate {
25  public:
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,
31                       NSView* view)
32       : AutofillDialogSignInDelegate(dialog_view,
33                                      dialog_web_contents,
34                                      originating_web_contents,
35                                      minimum_size,
36                                      maximum_size),
37         view_(view) {}
38
39   // WebContentsDelegate implementation. Forwards all unhandled keyboard events
40   // to the current window.
41   virtual void HandleKeyboardEvent(
42       content::WebContents* source,
43       const content::NativeWebKeyboardEvent& event) OVERRIDE;
44
45  private:
46   NSView* view_;  // WebContentsView, used to redispatch key events.
47 };
48
49 void CocoaSignInDelegate::HandleKeyboardEvent(
50     content::WebContents* source,
51     const content::NativeWebKeyboardEvent& event) {
52   if (![BrowserWindowUtils shouldHandleKeyboardEvent:event])
53     return;
54
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];
60 }
61
62 }  // namespace
63
64 @implementation AutofillSignInContainer
65
66 @synthesize preferredSize = preferredSize_;
67
68 - (id)initWithDialog:(autofill::AutofillDialogCocoa*)dialog {
69   if (self = [super init]) {
70     dialog_ = dialog;
71   }
72   return self;
73 }
74
75 - (void)loadView {
76   webContents_.reset(
77       content::WebContents::Create(
78           content::WebContents::CreateParams(dialog_->delegate()->profile())));
79   NSView* webContentView = webContents_->GetNativeView();
80   [self setView:webContentView];
81 }
82
83 - (void)loadSignInPage {
84   DCHECK(webContents_.get());
85
86   // Ensure initial minimum size doesn't cause resize.
87   NSSize initialMinSize = [[self view] frame].size;
88
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);
92
93   signInDelegate_.reset(
94       new CocoaSignInDelegate(
95           dialog_,
96           webContents_.get(),
97           dialog_->delegate()->GetWebContents(),
98           gfx::Size(NSSizeToCGSize(initialMinSize)),
99           gfx::Size(NSSizeToCGSize(maxSize_)),
100           [self view]));
101   webContents_->GetController().LoadURL(
102       dialog_->delegate()->SignInUrl(),
103       content::Referrer(),
104       content::PAGE_TRANSITION_AUTO_TOPLEVEL,
105       std::string());
106 }
107
108 - (content::NavigationController*)navigationController {
109   return &webContents_->GetController();
110 }
111
112 - (content::WebContents*)webContents {
113   return webContents_.get();
114 }
115
116 - (void)constrainSizeToMinimum:(NSSize)minSize maximum:(NSSize)maxSize {
117   minSize_ = minSize;
118   maxSize_ = maxSize;
119
120   // Constrain the web view to be a little shorter than the given sizes, leaving
121   // room for some padding below the web view.
122   minSize_.height -= chrome_style::kClientBottomPadding;
123   maxSize_.height -= chrome_style::kClientBottomPadding;
124
125   // Notify the web contents of its new auto-resize limits.
126   if (signInDelegate_ && ![[self view] isHidden]) {
127     signInDelegate_->UpdateLimitsAndEnableAutoResize(
128         gfx::Size(NSSizeToCGSize(minSize_)),
129         gfx::Size(NSSizeToCGSize(maxSize_)));
130   }
131 }
132
133 - (void)setPreferredSize:(NSSize)size {
134   // The |size| is the preferred size requested by the web view. Tack onto that
135   // a bit of extra padding at the bottom.
136   preferredSize_ = size;
137   preferredSize_.height += chrome_style::kClientBottomPadding;
138
139   // Always request re-layout if preferredSize changes.
140   id delegate = [[[self view] window] windowController];
141   if ([delegate respondsToSelector:@selector(requestRelayout)])
142     [delegate performSelector:@selector(requestRelayout)];
143 }
144
145 @end