1 // Copyright 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/autofill/autofill_dialog_sign_in_delegate.h"
7 #include "chrome/browser/ui/autofill/autofill_dialog_view.h"
8 #include "chrome/browser/ui/browser_finder.h"
9 #include "chrome/browser/ui/browser_tabstrip.h"
10 #include "components/autofill/content/browser/wallet/wallet_service_url.h"
11 #include "content/public/browser/render_view_host.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/common/renderer_preferences.h"
14 #include "ui/base/window_open_disposition.h"
20 using content::OpenURLParams
;
22 // Signals if |params| require opening inside the current WebContents.
23 bool IsInPageTransition(const OpenURLParams
& params
) {
24 return params
.disposition
== CURRENT_TAB
;
27 // Indicates if the open action specified by |params| should happen in the
28 // Autofill dialog (when true) or in the browser (when false).
29 bool ShouldOpenInBrowser(const OpenURLParams
& params
) {
30 return !IsInPageTransition(params
) || !wallet::IsSignInRelatedUrl(params
.url
);
33 // Adjusts |params| to account for the fact that the open request originated in
34 // the dialog, but will be executed in the browser.
35 OpenURLParams
AdjustToOpenInBrowser(const OpenURLParams
& params
) {
36 if (!IsInPageTransition(params
) || wallet::IsSignInRelatedUrl(params
.url
))
39 content::OpenURLParams new_params
= params
;
40 new_params
.disposition
= NEW_FOREGROUND_TAB
;
46 AutofillDialogSignInDelegate::AutofillDialogSignInDelegate(
47 AutofillDialogView
* dialog_view
,
48 content::WebContents
* dialog_web_contents
,
49 content::WebContents
* originating_web_contents
,
50 const gfx::Size
& minimum_size
,
51 const gfx::Size
& maximum_size
)
52 : WebContentsObserver(dialog_web_contents
),
53 dialog_view_(dialog_view
),
54 originating_web_contents_(originating_web_contents
) {
56 dialog_web_contents
->SetDelegate(this);
58 content::RendererPreferences
* prefs
=
59 dialog_web_contents
->GetMutableRendererPrefs();
60 prefs
->browser_handles_non_local_top_level_requests
= true;
61 dialog_web_contents
->GetRenderViewHost()->SyncRendererPrefs();
63 UpdateLimitsAndEnableAutoResize(minimum_size
, maximum_size
);
66 void AutofillDialogSignInDelegate::ResizeDueToAutoResize(
67 content::WebContents
* source
,
68 const gfx::Size
& preferred_size
) {
69 dialog_view_
->OnSignInResize(preferred_size
);
72 content::WebContents
* AutofillDialogSignInDelegate::OpenURLFromTab(
73 content::WebContents
* source
,
74 const content::OpenURLParams
& params
) {
75 if (ShouldOpenInBrowser(params
))
76 return originating_web_contents_
->OpenURL(AdjustToOpenInBrowser(params
));
78 source
->GetController().LoadURL(params
.url
,
85 // This gets invoked whenever there is an attempt to open a new window/tab.
86 // Reroute to the original browser.
87 void AutofillDialogSignInDelegate::AddNewContents(
88 content::WebContents
* source
,
89 content::WebContents
* new_contents
,
90 WindowOpenDisposition disposition
,
91 const gfx::Rect
& initial_pos
,
94 chrome::AddWebContents(
95 chrome::FindBrowserWithWebContents(originating_web_contents_
),
96 source
, new_contents
, disposition
, initial_pos
, user_gesture
,
100 void AutofillDialogSignInDelegate::RenderViewCreated(
101 content::RenderViewHost
* render_view_host
) {
104 // Set the initial size as soon as we have an RVH to avoid bad size jumping.
105 dialog_view_
->OnSignInResize(minimum_size_
);
108 void AutofillDialogSignInDelegate::UpdateLimitsAndEnableAutoResize(
109 const gfx::Size
& minimum_size
,
110 const gfx::Size
& maximum_size
) {
111 minimum_size_
= minimum_size
;
112 maximum_size_
= maximum_size
;
116 void AutofillDialogSignInDelegate::EnableAutoResize() {
117 DCHECK(!minimum_size_
.IsEmpty());
118 DCHECK(!maximum_size_
.IsEmpty());
119 content::RenderViewHost
* host
= web_contents()->GetRenderViewHost();
121 host
->EnableAutoResize(minimum_size_
, maximum_size_
);
124 } // namespace autofill