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