Home | History | Annotate | Download | only in autofill
      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.
      4 
      5 #include "chrome/browser/ui/autofill/autofill_dialog_sign_in_delegate.h"
      6 
      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 "third_party/WebKit/public/web/WebInputEvent.h"
     15 #include "ui/base/window_open_disposition.h"
     16 
     17 namespace autofill {
     18 
     19 namespace {
     20 
     21 using content::OpenURLParams;
     22 
     23 // Signals if |params| require opening inside the current WebContents.
     24 bool IsInPageTransition(const OpenURLParams& params) {
     25   return params.disposition == CURRENT_TAB;
     26 }
     27 
     28 // Indicates if the open action specified by |params| should happen in the
     29 // Autofill dialog (when true) or in the browser (when false).
     30 bool ShouldOpenInBrowser(const OpenURLParams& params) {
     31   return !IsInPageTransition(params) || !wallet::IsSignInRelatedUrl(params.url);
     32 }
     33 
     34 // Adjusts |params| to account for the fact that the open request originated in
     35 // the dialog, but will be executed in the browser.
     36 OpenURLParams AdjustToOpenInBrowser(const OpenURLParams& params) {
     37   if (!IsInPageTransition(params) || wallet::IsSignInRelatedUrl(params.url))
     38     return params;
     39 
     40   content::OpenURLParams new_params = params;
     41   new_params.disposition = NEW_FOREGROUND_TAB;
     42   return new_params;
     43 }
     44 
     45 }  // namespace
     46 
     47 AutofillDialogSignInDelegate::AutofillDialogSignInDelegate(
     48     AutofillDialogView* dialog_view,
     49     content::WebContents* dialog_web_contents,
     50     content::WebContents* originating_web_contents,
     51     const gfx::Size& minimum_size,
     52     const gfx::Size& maximum_size)
     53     : WebContentsObserver(dialog_web_contents),
     54       dialog_view_(dialog_view),
     55       originating_web_contents_(originating_web_contents) {
     56   DCHECK(dialog_view_);
     57   dialog_web_contents->SetDelegate(this);
     58 
     59   content::RendererPreferences* prefs =
     60       dialog_web_contents->GetMutableRendererPrefs();
     61   prefs->browser_handles_non_local_top_level_requests = true;
     62   dialog_web_contents->GetRenderViewHost()->SyncRendererPrefs();
     63 
     64   UpdateLimitsAndEnableAutoResize(minimum_size, maximum_size);
     65 }
     66 
     67 void AutofillDialogSignInDelegate::ResizeDueToAutoResize(
     68     content::WebContents* source,
     69     const gfx::Size& preferred_size) {
     70   dialog_view_->OnSignInResize(preferred_size);
     71 }
     72 
     73 content::WebContents* AutofillDialogSignInDelegate::OpenURLFromTab(
     74     content::WebContents* source,
     75     const content::OpenURLParams& params) {
     76   if (ShouldOpenInBrowser(params))
     77     return originating_web_contents_->OpenURL(AdjustToOpenInBrowser(params));
     78 
     79   source->GetController().LoadURL(params.url,
     80                                   params.referrer,
     81                                   params.transition,
     82                                   std::string());
     83   return source;
     84 }
     85 
     86 // This gets invoked whenever there is an attempt to open a new window/tab.
     87 // Reroute to the original browser.
     88 void AutofillDialogSignInDelegate::AddNewContents(
     89     content::WebContents* source,
     90     content::WebContents* new_contents,
     91     WindowOpenDisposition disposition,
     92     const gfx::Rect& initial_pos,
     93     bool user_gesture,
     94     bool* was_blocked) {
     95   chrome::AddWebContents(
     96       chrome::FindBrowserWithWebContents(originating_web_contents_),
     97       source, new_contents, disposition, initial_pos, user_gesture,
     98       was_blocked);
     99 }
    100 
    101 bool AutofillDialogSignInDelegate::PreHandleGestureEvent(
    102     content::WebContents* source,
    103     const blink::WebGestureEvent& event) {
    104   // Disable pinch zooming.
    105   return event.type == blink::WebGestureEvent::GesturePinchBegin ||
    106       event.type == blink::WebGestureEvent::GesturePinchUpdate ||
    107       event.type == blink::WebGestureEvent::GesturePinchEnd;
    108 }
    109 
    110 void AutofillDialogSignInDelegate::RenderViewCreated(
    111     content::RenderViewHost* render_view_host) {
    112   EnableAutoResize();
    113 
    114   // Set the initial size as soon as we have an RVH to avoid bad size jumping.
    115   dialog_view_->OnSignInResize(minimum_size_);
    116 }
    117 
    118 void AutofillDialogSignInDelegate::UpdateLimitsAndEnableAutoResize(
    119     const gfx::Size& minimum_size,
    120     const gfx::Size& maximum_size) {
    121   minimum_size_ = minimum_size;
    122   maximum_size_ = maximum_size;
    123   EnableAutoResize();
    124 }
    125 
    126 void AutofillDialogSignInDelegate::EnableAutoResize() {
    127   DCHECK(!minimum_size_.IsEmpty());
    128   DCHECK(!maximum_size_.IsEmpty());
    129   content::RenderViewHost* host = web_contents()->GetRenderViewHost();
    130   if (host)
    131     host->EnableAutoResize(minimum_size_, maximum_size_);
    132 }
    133 
    134 }  // namespace autofill
    135