| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/identity/web_auth_flow.h" | 5 #include "chrome/browser/extensions/api/identity/experimental_web_auth_flow.h" |
| 6 | 6 |
| 7 #include "base/location.h" | 7 #include "base/location.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/browser/ui/browser_navigator.h" | 10 #include "chrome/browser/ui/browser_navigator.h" |
| 11 #include "content/public/browser/load_notification_details.h" | 11 #include "content/public/browser/load_notification_details.h" |
| 12 #include "content/public/browser/navigation_controller.h" | 12 #include "content/public/browser/navigation_controller.h" |
| 13 #include "content/public/browser/notification_details.h" | 13 #include "content/public/browser/notification_details.h" |
| 14 #include "content/public/browser/notification_source.h" | 14 #include "content/public/browser/notification_source.h" |
| 15 #include "content/public/browser/notification_types.h" | 15 #include "content/public/browser/notification_types.h" |
| 16 #include "content/public/browser/resource_request_details.h" | 16 #include "content/public/browser/resource_request_details.h" |
| 17 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 18 #include "content/public/common/page_transition_types.h" | 18 #include "content/public/common/page_transition_types.h" |
| 19 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
| 20 #include "ui/base/window_open_disposition.h" | 20 #include "ui/base/window_open_disposition.h" |
| 21 | 21 |
| 22 using content::LoadNotificationDetails; | 22 using content::LoadNotificationDetails; |
| 23 using content::NavigationController; | 23 using content::NavigationController; |
| 24 using content::RenderViewHost; | 24 using content::RenderViewHost; |
| 25 using content::ResourceRedirectDetails; | 25 using content::ResourceRedirectDetails; |
| 26 using content::WebContents; | 26 using content::WebContents; |
| 27 using content::WebContentsObserver; | 27 using content::WebContentsObserver; |
| 28 | 28 |
| 29 namespace extensions { | 29 namespace extensions { |
| 30 | 30 |
| 31 WebAuthFlow::WebAuthFlow( | 31 ExperimentalWebAuthFlow::ExperimentalWebAuthFlow( |
| 32 Delegate* delegate, | 32 Delegate* delegate, |
| 33 Profile* profile, | 33 Profile* profile, |
| 34 const GURL& provider_url, | 34 const GURL& provider_url, |
| 35 Mode mode, | 35 Mode mode, |
| 36 const gfx::Rect& initial_bounds, | 36 const gfx::Rect& initial_bounds, |
| 37 chrome::HostDesktopType host_desktop_type) | 37 chrome::HostDesktopType host_desktop_type) |
| 38 : delegate_(delegate), | 38 : delegate_(delegate), |
| 39 profile_(profile), | 39 profile_(profile), |
| 40 provider_url_(provider_url), | 40 provider_url_(provider_url), |
| 41 mode_(mode), | 41 mode_(mode), |
| 42 initial_bounds_(initial_bounds), | 42 initial_bounds_(initial_bounds), |
| 43 host_desktop_type_(host_desktop_type), | 43 host_desktop_type_(host_desktop_type), |
| 44 popup_shown_(false), | 44 popup_shown_(false), |
| 45 contents_(NULL) { | 45 contents_(NULL) { |
| 46 } | 46 } |
| 47 | 47 |
| 48 WebAuthFlow::~WebAuthFlow() { | 48 ExperimentalWebAuthFlow::~ExperimentalWebAuthFlow() { |
| 49 // Stop listening to notifications first since some of the code | 49 // Stop listening to notifications first since some of the code |
| 50 // below may generate notifications. | 50 // below may generate notifications. |
| 51 registrar_.RemoveAll(); | 51 registrar_.RemoveAll(); |
| 52 WebContentsObserver::Observe(NULL); | 52 WebContentsObserver::Observe(NULL); |
| 53 | 53 |
| 54 if (contents_) { | 54 if (contents_) { |
| 55 // The popup owns the contents if it was displayed. | 55 // The popup owns the contents if it was displayed. |
| 56 if (popup_shown_) | 56 if (popup_shown_) |
| 57 contents_->Close(); | 57 contents_->Close(); |
| 58 else | 58 else |
| 59 delete contents_; | 59 delete contents_; |
| 60 } | 60 } |
| 61 } | 61 } |
| 62 | 62 |
| 63 void WebAuthFlow::Start() { | 63 void ExperimentalWebAuthFlow::Start() { |
| 64 contents_ = CreateWebContents(); | 64 contents_ = CreateWebContents(); |
| 65 WebContentsObserver::Observe(contents_); | 65 WebContentsObserver::Observe(contents_); |
| 66 | 66 |
| 67 NavigationController* controller = &(contents_->GetController()); | 67 NavigationController* controller = &(contents_->GetController()); |
| 68 | 68 |
| 69 // Register for appropriate notifications to intercept navigation to the | 69 // Register for appropriate notifications to intercept navigation to the |
| 70 // redirect URLs. | 70 // redirect URLs. |
| 71 registrar_.Add( | 71 registrar_.Add( |
| 72 this, | 72 this, |
| 73 content::NOTIFICATION_RESOURCE_RECEIVED_REDIRECT, | 73 content::NOTIFICATION_RESOURCE_RECEIVED_REDIRECT, |
| 74 content::Source<WebContents>(contents_)); | 74 content::Source<WebContents>(contents_)); |
| 75 | 75 |
| 76 controller->LoadURL( | 76 controller->LoadURL( |
| 77 provider_url_, | 77 provider_url_, |
| 78 content::Referrer(), | 78 content::Referrer(), |
| 79 content::PAGE_TRANSITION_AUTO_TOPLEVEL, | 79 content::PAGE_TRANSITION_AUTO_TOPLEVEL, |
| 80 std::string()); | 80 std::string()); |
| 81 } | 81 } |
| 82 | 82 |
| 83 WebContents* WebAuthFlow::CreateWebContents() { | 83 WebContents* ExperimentalWebAuthFlow::CreateWebContents() { |
| 84 return WebContents::Create(WebContents::CreateParams(profile_)); | 84 return WebContents::Create(WebContents::CreateParams(profile_)); |
| 85 } | 85 } |
| 86 | 86 |
| 87 void WebAuthFlow::ShowAuthFlowPopup() { | 87 void ExperimentalWebAuthFlow::ShowAuthFlowPopup() { |
| 88 Browser::CreateParams browser_params(Browser::TYPE_POPUP, profile_, | 88 Browser::CreateParams browser_params(Browser::TYPE_POPUP, profile_, |
| 89 host_desktop_type_); | 89 host_desktop_type_); |
| 90 browser_params.initial_bounds = initial_bounds_; | 90 browser_params.initial_bounds = initial_bounds_; |
| 91 Browser* browser = new Browser(browser_params); | 91 Browser* browser = new Browser(browser_params); |
| 92 chrome::NavigateParams params(browser, contents_); | 92 chrome::NavigateParams params(browser, contents_); |
| 93 params.disposition = CURRENT_TAB; | 93 params.disposition = CURRENT_TAB; |
| 94 params.window_action = chrome::NavigateParams::SHOW_WINDOW; | 94 params.window_action = chrome::NavigateParams::SHOW_WINDOW; |
| 95 chrome::Navigate(¶ms); | 95 chrome::Navigate(¶ms); |
| 96 // Observe method and WebContentsObserver::* methods will be called | 96 // Observe method and WebContentsObserver::* methods will be called |
| 97 // for varous navigation events. That is where we check for redirect | 97 // for varous navigation events. That is where we check for redirect |
| 98 // to the right URL. | 98 // to the right URL. |
| 99 popup_shown_ = true; | 99 popup_shown_ = true; |
| 100 } | 100 } |
| 101 | 101 |
| 102 void WebAuthFlow::BeforeUrlLoaded(const GURL& url) { | 102 void ExperimentalWebAuthFlow::BeforeUrlLoaded(const GURL& url) { |
| 103 delegate_->OnAuthFlowURLChange(url); | 103 delegate_->OnAuthFlowURLChange(url); |
| 104 } | 104 } |
| 105 | 105 |
| 106 void WebAuthFlow::AfterUrlLoaded() { | 106 void ExperimentalWebAuthFlow::AfterUrlLoaded() { |
| 107 // Do nothing if a popup is already created. | 107 // Do nothing if a popup is already created. |
| 108 if (popup_shown_) | 108 if (popup_shown_) |
| 109 return; | 109 return; |
| 110 | 110 |
| 111 // Report results directly if not in interactive mode. | 111 // Report results directly if not in interactive mode. |
| 112 if (mode_ != WebAuthFlow::INTERACTIVE) { | 112 if (mode_ != ExperimentalWebAuthFlow::INTERACTIVE) { |
| 113 delegate_->OnAuthFlowFailure(WebAuthFlow::INTERACTION_REQUIRED); | 113 delegate_->OnAuthFlowFailure(ExperimentalWebAuthFlow::INTERACTION_REQUIRED); |
| 114 return; | 114 return; |
| 115 } | 115 } |
| 116 | 116 |
| 117 // We are in interactive mode and window is not shown yet; show the window. | 117 // We are in interactive mode and window is not shown yet; show the window. |
| 118 ShowAuthFlowPopup(); | 118 ShowAuthFlowPopup(); |
| 119 } | 119 } |
| 120 | 120 |
| 121 void WebAuthFlow::Observe(int type, | 121 void ExperimentalWebAuthFlow::Observe(int type, |
| 122 const content::NotificationSource& source, | 122 const content::NotificationSource& source, |
| 123 const content::NotificationDetails& details) { | 123 const content::NotificationDetails& details) { |
| 124 switch (type) { | 124 switch (type) { |
| 125 case content::NOTIFICATION_RESOURCE_RECEIVED_REDIRECT: { | 125 case content::NOTIFICATION_RESOURCE_RECEIVED_REDIRECT: { |
| 126 ResourceRedirectDetails* redirect_details = | 126 ResourceRedirectDetails* redirect_details = |
| 127 content::Details<ResourceRedirectDetails>(details).ptr(); | 127 content::Details<ResourceRedirectDetails>(details).ptr(); |
| 128 if (redirect_details != NULL) | 128 if (redirect_details != NULL) |
| 129 BeforeUrlLoaded(redirect_details->new_url); | 129 BeforeUrlLoaded(redirect_details->new_url); |
| 130 } | 130 } |
| 131 break; | 131 break; |
| 132 default: | 132 default: |
| 133 NOTREACHED() << "Got a notification that we did not register for: " | 133 NOTREACHED() << "Got a notification that we did not register for: " |
| 134 << type; | 134 << type; |
| 135 break; | 135 break; |
| 136 } | 136 } |
| 137 } | 137 } |
| 138 | 138 |
| 139 void WebAuthFlow::ProvisionalChangeToMainFrameUrl( | 139 void ExperimentalWebAuthFlow::ProvisionalChangeToMainFrameUrl( |
| 140 const GURL& url, | 140 const GURL& url, |
| 141 RenderViewHost* render_view_host) { | 141 RenderViewHost* render_view_host) { |
| 142 BeforeUrlLoaded(url); | 142 BeforeUrlLoaded(url); |
| 143 } | 143 } |
| 144 | 144 |
| 145 void WebAuthFlow::DidStopLoading(RenderViewHost* render_view_host) { | 145 void ExperimentalWebAuthFlow::DidStopLoading(RenderViewHost* render_view_host) { |
| 146 AfterUrlLoaded(); | 146 AfterUrlLoaded(); |
| 147 } | 147 } |
| 148 | 148 |
| 149 void WebAuthFlow::WebContentsDestroyed(WebContents* web_contents) { | 149 void ExperimentalWebAuthFlow::WebContentsDestroyed(WebContents* web_contents) { |
| 150 contents_ = NULL; | 150 contents_ = NULL; |
| 151 delegate_->OnAuthFlowFailure(WebAuthFlow::WINDOW_CLOSED); | 151 delegate_->OnAuthFlowFailure(ExperimentalWebAuthFlow::WINDOW_CLOSED); |
| 152 } | 152 } |
| 153 | 153 |
| 154 } // namespace extensions | 154 } // namespace extensions |
| OLD | NEW |