OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/sync/one_click_signin_sync_observer.h" |
| 6 |
| 7 #include "chrome/browser/profiles/profile.h" |
| 8 #include "chrome/browser/signin/signin_promo.h" |
| 9 #include "chrome/browser/sync/profile_sync_service.h" |
| 10 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 11 #include "content/public/browser/web_contents.h" |
| 12 #include "content/public/browser/web_contents_delegate.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 void CloseTab(content::WebContents* tab) { |
| 17 content::WebContentsDelegate* tab_delegate = tab->GetDelegate(); |
| 18 if (tab_delegate) |
| 19 tab_delegate->CloseContents(tab); |
| 20 } |
| 21 |
| 22 } // namespace |
| 23 |
| 24 |
| 25 OneClickSigninSyncObserver::OneClickSigninSyncObserver( |
| 26 content::WebContents* web_contents, |
| 27 const GURL& continue_url) |
| 28 : content::WebContentsObserver(web_contents), |
| 29 continue_url_(continue_url) { |
| 30 DCHECK(!continue_url_.is_empty()); |
| 31 |
| 32 ProfileSyncService* sync_service = GetSyncService(web_contents); |
| 33 if (sync_service) { |
| 34 sync_service->AddObserver(this); |
| 35 } else { |
| 36 LoadContinueUrl(); |
| 37 delete this; |
| 38 } |
| 39 } |
| 40 |
| 41 OneClickSigninSyncObserver::~OneClickSigninSyncObserver() {} |
| 42 |
| 43 void OneClickSigninSyncObserver::WebContentsDestroyed( |
| 44 content::WebContents* web_contents) { |
| 45 ProfileSyncService* sync_service = GetSyncService(web_contents); |
| 46 if (sync_service) |
| 47 sync_service->RemoveObserver(this); |
| 48 |
| 49 delete this; |
| 50 } |
| 51 |
| 52 void OneClickSigninSyncObserver::OnStateChanged() { |
| 53 ProfileSyncService* sync_service = GetSyncService(web_contents()); |
| 54 |
| 55 // At this point, the sign-in process is complete, and control has been handed |
| 56 // back to the sync engine. Close the gaia sign in tab if the |continue_url_| |
| 57 // contains the |auto_close| parameter. Otherwise, wait for sync setup to |
| 58 // complete and then navigate to the |continue_url_|. |
| 59 if (signin::IsAutoCloseEnabledInURL(continue_url_)) { |
| 60 // Close the Gaia sign-in tab via a task to make sure we aren't in the |
| 61 // middle of any WebUI handler code. |
| 62 base::MessageLoop::current()->PostTask( |
| 63 FROM_HERE, |
| 64 base::Bind(&CloseTab, base::Unretained(web_contents()))); |
| 65 } else { |
| 66 if (sync_service->FirstSetupInProgress()) { |
| 67 // Sync setup has not completed yet. Wait for it to complete. |
| 68 return; |
| 69 } |
| 70 |
| 71 if (sync_service->sync_initialized() && |
| 72 signin::GetSourceForPromoURL(continue_url_) |
| 73 != signin::SOURCE_SETTINGS) { |
| 74 // TODO(isherman): Redirecting after Sync is set up still has some bugs: |
| 75 // http://crbug.com/355885 |
| 76 // Having multiple settings pages open can cause issues. |
| 77 // http://crbug.com/XXXXXX |
| 78 // Selecting anything other than "Sync Everything" when configuring Sync |
| 79 // prevents the redirect. |
| 80 LoadContinueUrl(); |
| 81 } |
| 82 } |
| 83 |
| 84 sync_service->RemoveObserver(this); |
| 85 delete this; |
| 86 } |
| 87 |
| 88 void OneClickSigninSyncObserver::LoadContinueUrl() { |
| 89 web_contents()->GetController().LoadURL( |
| 90 continue_url_, |
| 91 content::Referrer(), |
| 92 content::PAGE_TRANSITION_AUTO_TOPLEVEL, |
| 93 std::string()); |
| 94 } |
| 95 |
| 96 ProfileSyncService* OneClickSigninSyncObserver::GetSyncService( |
| 97 content::WebContents* web_contents) { |
| 98 Profile* profile = |
| 99 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 100 return ProfileSyncServiceFactory::GetForProfile(profile); |
| 101 } |
OLD | NEW |