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.
5 #include "chrome/browser/ui/sync/one_click_signin_sync_observer.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/signin/signin_promo.h"
13 #include "chrome/browser/sync/profile_sync_service.h"
14 #include "chrome/browser/sync/profile_sync_service_factory.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_contents_delegate.h"
20 void CloseTab(content::WebContents
* tab
) {
21 content::WebContentsDelegate
* tab_delegate
= tab
->GetDelegate();
23 tab_delegate
->CloseContents(tab
);
29 OneClickSigninSyncObserver::OneClickSigninSyncObserver(
30 content::WebContents
* web_contents
,
31 const GURL
& continue_url
)
32 : content::WebContentsObserver(web_contents
),
33 continue_url_(continue_url
),
34 weak_ptr_factory_(this) {
35 DCHECK(!continue_url_
.is_empty());
37 ProfileSyncService
* sync_service
= GetSyncService(web_contents
);
39 sync_service
->AddObserver(this);
42 // 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
44 // important to pass a weak pointer rather than base::Unretained(this)
45 // because it's possible for e.g. WebContentsDestroyed() to be called
46 // before this task has a chance to run.
47 base::ThreadTaskRunnerHandle::Get()->PostTask(
48 FROM_HERE
, base::Bind(&OneClickSigninSyncObserver::DeleteObserver
,
49 weak_ptr_factory_
.GetWeakPtr()));
53 OneClickSigninSyncObserver::~OneClickSigninSyncObserver() {}
55 void OneClickSigninSyncObserver::WebContentsDestroyed() {
56 ProfileSyncService
* sync_service
= GetSyncService(web_contents());
58 sync_service
->RemoveObserver(this);
63 void OneClickSigninSyncObserver::OnStateChanged() {
64 ProfileSyncService
* sync_service
= GetSyncService(web_contents());
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::ThreadTaskRunnerHandle::Get()->PostTask(
74 FROM_HERE
, base::Bind(&CloseTab
, base::Unretained(web_contents())));
76 if (sync_service
->FirstSetupInProgress()) {
77 // Sync setup has not completed yet. Wait for it to complete.
81 if (sync_service
->IsSyncActive() &&
82 signin::GetSourceForPromoURL(continue_url_
)
83 != signin_metrics::SOURCE_SETTINGS
) {
84 // TODO(isherman): Having multiple settings pages open can cause issues
85 // redirecting after Sync is set up: http://crbug.com/355885
90 sync_service
->RemoveObserver(this);
94 void OneClickSigninSyncObserver::LoadContinueUrl() {
95 web_contents()->GetController().LoadURL(
98 ui::PAGE_TRANSITION_AUTO_TOPLEVEL
,
102 ProfileSyncService
* OneClickSigninSyncObserver::GetSyncService(
103 content::WebContents
* web_contents
) {
105 Profile::FromBrowserContext(web_contents
->GetBrowserContext());
106 return ProfileSyncServiceFactory::GetForProfile(profile
);
110 void OneClickSigninSyncObserver::DeleteObserver(
111 base::WeakPtr
<OneClickSigninSyncObserver
> observer
) {
113 delete observer
.get();