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