Chromium Code Reviews| Index: chrome/browser/ui/views/profiles/signin_view_controller.cc |
| diff --git a/chrome/browser/ui/views/profiles/signin_view_controller.cc b/chrome/browser/ui/views/profiles/signin_view_controller.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6c405959bd8d42d5cea620b46272971a8dbb0c0f |
| --- /dev/null |
| +++ b/chrome/browser/ui/views/profiles/signin_view_controller.cc |
| @@ -0,0 +1,154 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/views/profiles/signin_view_controller.h" |
| + |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/profiles/profile_avatar_icon_util.h" |
| +#include "chrome/browser/signin/signin_error_controller_factory.h" |
| +#include "chrome/browser/signin/signin_promo.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| +#include "components/constrained_window/constrained_window_views.h" |
| +#include "components/signin/core/browser/signin_error_controller.h" |
| +#include "components/signin/core/common/profile_management_switches.h" |
| +#include "content/public/browser/render_widget_host_view.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "ui/views/controls/webview/webview.h" |
| +#include "ui/views/widget/widget.h" |
| +#include "ui/views/widget/widget_delegate.h" |
| +#include "ui/views/window/dialog_delegate.h" |
| + |
| +const int kPasswordCombinedFixedGaiaViewHeight = 440; |
| +const int kPasswordCombinedFixedGaiaViewWidth = 360; |
| +const int kFixedGaiaViewHeight = 512; |
| +const int kFixedGaiaViewWidth = 448; |
| + |
| +class ModalSigninDelegate : public views::DialogDelegateView, |
| + public content::WebContentsDelegate, |
| + public SigninViewController::Observer { |
| + public: |
| + ModalSigninDelegate(SigninViewController* signin_view_controller, |
| + views::WebView* content_view, |
| + Browser* browser) |
| + : content_view_(content_view), |
| + signin_view_controller_(signin_view_controller) { |
| + signin_view_controller_->AddObserver(this); |
| + content_view_->GetWebContents()->SetDelegate(this); |
| + modal_signin_widget_ = constrained_window::ShowWebModalDialogViews( |
| + this, browser->tab_strip_model()->GetActiveWebContents()); |
| + } |
| + |
| + void CloseModalSignin() { |
| + modal_signin_widget_->Close(); |
| + } |
| + |
| + // views::DialogDelegateView: |
| + views::View* GetContentsView() override { |
| + return content_view_; |
| + } |
| + |
| + views::Widget* GetWidget() override { |
| + return content_view_->GetWidget(); |
| + } |
| + |
| + const views::Widget* GetWidget() const override { |
| + return content_view_->GetWidget(); |
| + } |
| + |
| + void DeleteDelegate() override { |
| + // Stop observing the view controller, as the widget is already closed at |
| + // this point. Since this can be initiated directly by the views code, this |
| + // function can be called without OnCloseRequested ever being called. |
| + signin_view_controller_->RemoveObserver(this); |
|
sky
2015/12/01 21:12:37
Again, won't this crash if SigninViewController ha
anthonyvd
2015/12/01 22:25:04
You're right, I was focusing on the other way arou
|
| + delete this; |
| + } |
| + |
| + ui::ModalType GetModalType() const override { |
| + return ui::MODAL_TYPE_CHILD; |
| + } |
| + |
| + bool ShouldShowCloseButton() const override { |
| + return false; |
| + } |
| + |
| + int GetDialogButtons() const override { |
| + return ui::DIALOG_BUTTON_NONE; |
| + } |
| + |
| + // SigninViewController::Observer: |
| + void OnCloseRequested() override { |
| + signin_view_controller_->RemoveObserver(this); |
| + CloseModalSignin(); |
| + } |
| + |
| + private: |
| + views::WebView* content_view_; |
| + SigninViewController* signin_view_controller_; // Not owned. |
| + views::Widget* modal_signin_widget_; // Not owned. |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ModalSigninDelegate); |
| +}; |
| + |
| +SigninViewController::SigninViewController() {} |
| + |
| +SigninViewController::~SigninViewController() { |
| + CloseModalSignin(); |
| +} |
| + |
| +// static |
| +views::WebView* SigninViewController::CreateGaiaWebView( |
| + WebContentsDelegate* delegate, |
| + profiles::BubbleViewMode mode, |
| + Profile* profile) { |
| + GURL url = signin::GetSigninURLFromBubbleViewMode(profile, mode); |
| + |
| + // Adds Gaia signin webview. |
| + const gfx::Size pref_size = switches::UsePasswordSeparatedSigninFlow() |
| + ? gfx::Size(kFixedGaiaViewWidth, kFixedGaiaViewHeight) |
| + : gfx::Size(kPasswordCombinedFixedGaiaViewWidth, |
| + kPasswordCombinedFixedGaiaViewHeight); |
| + views::WebView* web_view = new views::WebView(profile); |
| + web_view->LoadInitialURL(url); |
| + web_view->GetWebContents()->SetDelegate(delegate); |
| + web_view->SetPreferredSize(pref_size); |
| + content::RenderWidgetHostView* rwhv = |
| + web_view->GetWebContents()->GetRenderWidgetHostView(); |
| + if (rwhv) |
| + rwhv->SetBackgroundColor(profiles::kAvatarBubbleGaiaBackgroundColor); |
| + |
| + return web_view; |
| +} |
| + |
| +void SigninViewController::ShowModalSignin( |
| + profiles::BubbleViewMode mode, Browser* browser) { |
| + CloseModalSignin(); |
| + // The delegate will delete itself on request of the views code when the |
| + // widget is closed. |
| + new ModalSigninDelegate( |
| + this, CreateGaiaWebView(this, mode, browser->profile()), browser); |
| +} |
| + |
| +void SigninViewController::CloseModalSignin() { |
| + // Notify all observers so that they can close the widget they own if it |
| + // wasn't already closed by something else. |
| + FOR_EACH_OBSERVER(Observer, observers_, OnCloseRequested()); |
| +} |
| + |
| +void SigninViewController::AddObserver(Observer* observer) { |
| + observers_.AddObserver(observer); |
| +} |
| + |
| +void SigninViewController::RemoveObserver(Observer* observer) { |
| + observers_.RemoveObserver(observer); |
| +} |
| + |
| +// static |
| +bool SigninViewController::ShouldShowModalSigninForMode( |
| + profiles::BubbleViewMode mode) { |
| + return switches::UsePasswordSeparatedSigninFlow() && |
| + (mode == profiles::BUBBLE_VIEW_MODE_GAIA_SIGNIN || |
| + mode == profiles::BUBBLE_VIEW_MODE_GAIA_ADD_ACCOUNT || |
| + mode == profiles::BUBBLE_VIEW_MODE_GAIA_REAUTH); |
| +} |