| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/ui/sync/one_click_signin_sync_observer.h" | 5 #include "chrome/browser/ui/sync/one_click_signin_sync_observer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 content::WebContentsDelegate* tab_delegate = tab->GetDelegate(); | 21 content::WebContentsDelegate* tab_delegate = tab->GetDelegate(); |
| 22 if (tab_delegate) | 22 if (tab_delegate) |
| 23 tab_delegate->CloseContents(tab); | 23 tab_delegate->CloseContents(tab); |
| 24 } | 24 } |
| 25 | 25 |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 | 28 |
| 29 OneClickSigninSyncObserver::OneClickSigninSyncObserver( | 29 OneClickSigninSyncObserver::OneClickSigninSyncObserver( |
| 30 content::WebContents* web_contents, | 30 content::WebContents* web_contents, |
| 31 Profile* profile, |
| 32 Browser* browser, |
| 33 const GURL& activity_controls_url, |
| 31 const GURL& continue_url) | 34 const GURL& continue_url) |
| 32 : content::WebContentsObserver(web_contents), | 35 : content::WebContentsObserver(web_contents), |
| 36 profile_(profile), |
| 37 browser_(browser), |
| 38 activity_controls_url_(activity_controls_url), |
| 33 continue_url_(continue_url), | 39 continue_url_(continue_url), |
| 34 weak_ptr_factory_(this) { | 40 weak_ptr_factory_(this) { |
| 35 DCHECK(!continue_url_.is_empty()); | 41 DCHECK(!continue_url_.is_empty() || !activity_controls_url.is_empty()); |
| 36 | 42 |
| 37 ProfileSyncService* sync_service = GetSyncService(web_contents); | 43 ProfileSyncService* sync_service = GetSyncService(); |
| 38 if (sync_service) { | 44 if (sync_service) { |
| 39 sync_service->AddObserver(this); | 45 sync_service->AddObserver(this); |
| 40 } else { | 46 } else { |
| 41 LoadContinueUrl(); | 47 LoadContinueUrl(); |
| 42 // Post a task rather than calling |delete this| here, so that the | 48 // Post a task rather than calling |delete this| here, so that the |
| 43 // destructor is not called directly from the constructor. Note that it's | 49 // destructor is not called directly from the constructor. Note that it's |
| 44 // important to pass a weak pointer rather than base::Unretained(this) | 50 // important to pass a weak pointer rather than base::Unretained(this) |
| 45 // because it's possible for e.g. WebContentsDestroyed() to be called | 51 // because it's possible for e.g. WebContentsDestroyed() to be called |
| 46 // before this task has a chance to run. | 52 // before this task has a chance to run. |
| 47 base::ThreadTaskRunnerHandle::Get()->PostTask( | 53 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 48 FROM_HERE, base::Bind(&OneClickSigninSyncObserver::DeleteObserver, | 54 FROM_HERE, base::Bind(&OneClickSigninSyncObserver::DeleteObserver, |
| 49 weak_ptr_factory_.GetWeakPtr())); | 55 weak_ptr_factory_.GetWeakPtr())); |
| 50 } | 56 } |
| 51 } | 57 } |
| 52 | 58 |
| 53 OneClickSigninSyncObserver::~OneClickSigninSyncObserver() {} | 59 OneClickSigninSyncObserver::~OneClickSigninSyncObserver() {} |
| 54 | 60 |
| 55 void OneClickSigninSyncObserver::WebContentsDestroyed() { | 61 void OneClickSigninSyncObserver::WebContentsDestroyed() { |
| 56 ProfileSyncService* sync_service = GetSyncService(web_contents()); | 62 ProfileSyncService* sync_service = GetSyncService(); |
| 57 if (sync_service) | 63 if (sync_service) |
| 58 sync_service->RemoveObserver(this); | 64 sync_service->RemoveObserver(this); |
| 59 | 65 |
| 60 delete this; | 66 delete this; |
| 61 } | 67 } |
| 62 | 68 |
| 63 void OneClickSigninSyncObserver::OnStateChanged() { | 69 void OneClickSigninSyncObserver::OnStateChanged() { |
| 64 ProfileSyncService* sync_service = GetSyncService(web_contents()); | 70 ProfileSyncService* sync_service = GetSyncService(); |
| 65 | 71 |
| 66 // At this point, the sign-in process is complete, and control has been handed | 72 // 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_| | 73 // 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 | 74 // contains the |auto_close| parameter. Otherwise, wait for sync setup to |
| 69 // complete and then navigate to the |continue_url_|. | 75 // complete and then navigate to the |continue_url_|. |
| 70 if (signin::IsAutoCloseEnabledInURL(continue_url_)) { | 76 if (signin::IsAutoCloseEnabledInURL(continue_url_)) { |
| 71 // Close the Gaia sign-in tab via a task to make sure we aren't in the | 77 // Close the Gaia sign-in tab via a task to make sure we aren't in the |
| 72 // middle of any WebUI handler code. | 78 // middle of any WebUI handler code. |
| 73 base::ThreadTaskRunnerHandle::Get()->PostTask( | 79 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 74 FROM_HERE, base::Bind(&CloseTab, base::Unretained(web_contents()))); | 80 FROM_HERE, base::Bind(&CloseTab, base::Unretained(web_contents()))); |
| 75 } else { | 81 } else { |
| 76 if (sync_service->IsFirstSetupInProgress()) { | 82 if (sync_service->IsFirstSetupInProgress()) { |
| 77 // Sync setup has not completed yet. Wait for it to complete. | 83 // Sync setup has not completed yet. Wait for it to complete. |
| 78 return; | 84 return; |
| 79 } | 85 } |
| 80 | 86 |
| 81 if (sync_service->IsSyncActive() && | 87 if (!activity_controls_url_.is_empty()) { |
| 88 // Open the 'Activity controls' section of the privacy settings. |
| 89 content::OpenURLParams params(activity_controls_url_, |
| 90 content::Referrer(), |
| 91 NEW_FOREGROUND_TAB, |
| 92 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, |
| 93 false); |
| 94 browser_->OpenURL(params); |
| 95 } |
| 96 |
| 97 if (sync_service->IsSyncActive() && !continue_url_.is_empty() && |
| 82 signin::GetAccessPointForPromoURL(continue_url_) != | 98 signin::GetAccessPointForPromoURL(continue_url_) != |
| 83 signin_metrics::AccessPoint::ACCESS_POINT_SETTINGS) { | 99 signin_metrics::AccessPoint::ACCESS_POINT_SETTINGS) { |
| 84 // TODO(isherman): Having multiple settings pages open can cause issues | 100 // TODO(isherman): Having multiple settings pages open can cause issues |
| 85 // redirecting after Sync is set up: http://crbug.com/355885 | 101 // redirecting after Sync is set up: http://crbug.com/355885 |
| 86 LoadContinueUrl(); | 102 LoadContinueUrl(); |
| 87 } | 103 } |
| 88 } | 104 } |
| 89 | 105 |
| 90 sync_service->RemoveObserver(this); | 106 sync_service->RemoveObserver(this); |
| 91 delete this; | 107 delete this; |
| 92 } | 108 } |
| 93 | 109 |
| 94 void OneClickSigninSyncObserver::LoadContinueUrl() { | 110 void OneClickSigninSyncObserver::LoadContinueUrl() { |
| 95 web_contents()->GetController().LoadURL( | 111 web_contents()->GetController().LoadURL( |
| 96 continue_url_, | 112 continue_url_, |
| 97 content::Referrer(), | 113 content::Referrer(), |
| 98 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, | 114 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, |
| 99 std::string()); | 115 std::string()); |
| 100 } | 116 } |
| 101 | 117 |
| 102 ProfileSyncService* OneClickSigninSyncObserver::GetSyncService( | 118 ProfileSyncService* OneClickSigninSyncObserver::GetSyncService() { |
| 103 content::WebContents* web_contents) { | 119 return ProfileSyncServiceFactory::GetForProfile(profile_); |
| 104 Profile* profile = | |
| 105 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
| 106 return ProfileSyncServiceFactory::GetForProfile(profile); | |
| 107 } | 120 } |
| 108 | 121 |
| 109 // static | 122 // static |
| 110 void OneClickSigninSyncObserver::DeleteObserver( | 123 void OneClickSigninSyncObserver::DeleteObserver( |
| 111 base::WeakPtr<OneClickSigninSyncObserver> observer) { | 124 base::WeakPtr<OneClickSigninSyncObserver> observer) { |
| 112 if (observer) | 125 if (observer) |
| 113 delete observer.get(); | 126 delete observer.get(); |
| 114 } | 127 } |
| OLD | NEW |