Chromium Code Reviews| 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 DLOG(WARNING) << "Reached OneClickSigninSyncObserver::OnStateChanged()"; | |
| 54 ProfileSyncService* sync_service = GetSyncService(web_contents()); | |
| 55 | |
| 56 // At this point, the sign-in process is complete, and control has been handed | |
| 57 // back to the sync engine. Close the gaia sign in tab if the |continue_url_| | |
| 58 // contains the |auto_close| parameter. Otherwise, wait for sync setup to | |
| 59 // complete and then navigate to the |continue_url_|. | |
| 60 if (signin::IsAutoCloseEnabledInURL(continue_url_)) { | |
| 61 DLOG(WARNING) << "Auto-close is enabled in the continue url."; | |
| 62 // Close the Gaia sign-in tab via a task to make sure we aren't in the | |
| 63 // middle of any WebUI handler code. | |
| 64 base::MessageLoop::current()->PostTask( | |
| 65 FROM_HERE, | |
| 66 base::Bind(&CloseTab, base::Unretained(web_contents()))); | |
| 67 } else { | |
| 68 DLOG(WARNING) << "Auto-close is not enabled in the continue url."; | |
| 69 if (sync_service->FirstSetupInProgress()) { | |
| 70 DLOG(WARNING) << "First setup is still in progress; return early."; | |
| 71 // Sync setup has not completed yet. Wait for it to complete. | |
| 72 return; | |
| 73 } | |
| 74 | |
| 75 if (sync_service->sync_initialized() && | |
| 76 signin::GetSourceForPromoURL(continue_url_) | |
| 77 != signin::SOURCE_SETTINGS) { | |
| 78 DLOG(WARNING) << "Initialized successfully! Time to redirect to the conti nue URL."; | |
|
guohui
2014/03/24 20:55:58
nits: 80 chars
Ilya Sherman
2014/03/26 08:21:39
I'm not planning to commit these DLOG statements.
| |
| 79 // TODO(isherman): I'm seeing this case reached with the following STR: | |
| 80 // 0. Open a tab pointed to chrome://settings. | |
| 81 // 1. In a new tab, navigate to the Chrome Webstore. | |
| 82 // 2. Trigger the private API to sign in to Chrome. | |
| 83 // 3. Check the box to "Choose what to syc". | |
| 84 // 4. Enter username and password to sign in. | |
| 85 // 5. Wait for the current tab to be redirected to | |
| 86 // chrome://settings/syncSetup | |
| 87 // 6. Close the original chrome://settings tab. | |
| 88 // 7. At this point, the current tab will redirect back to the Chrome | |
| 89 // Webstore, even though the user hasn't actually configured Sync. | |
| 90 // I don't really understand why closing the first chrome://settings tab | |
| 91 // triggers an OnStateChanged() call. Interestingly, the behavior is | |
| 92 // different from the case where the user clicks "Cancel" in the current | |
| 93 // tab that's showing the "Configure sync" dialog. Also, this code | |
| 94 // doesn't seem to handle the case where the user opts to choose something | |
| 95 // other than "Sync everything". Again, I don't know why -- it works fine | |
| 96 // for the "Sync everything" case, and it's unchanged from the | |
| 97 // pre-existing OneClickSigninHelper logic. This is admittedly all a pile | |
| 98 // of edge-cases, but I'm worried that there might be a lot of these edge | |
| 99 // cases that add up to a bad user experience vs. opening a new tab that | |
| 100 // houses the "Configure sync" settings dialog. WDYT? | |
| 101 LoadContinueUrl(); | |
| 102 } | |
| 103 | |
| 104 // TODO(isherman): What's the appropriate behavior if the user presses | |
| 105 // "Cancel" after opting to "Choose what to sync"? | |
| 106 } | |
| 107 | |
| 108 DLOG(WARNING) << "Sync setup is done. Removing self as an observer and dying. "; | |
|
guohui
2014/03/24 20:55:58
nits: 80 chars
| |
| 109 sync_service->RemoveObserver(this); | |
| 110 delete this; | |
| 111 } | |
| 112 | |
| 113 void OneClickSigninSyncObserver::LoadContinueUrl() { | |
| 114 web_contents()->GetController().LoadURL( | |
| 115 continue_url_, | |
| 116 content::Referrer(), | |
| 117 content::PAGE_TRANSITION_AUTO_TOPLEVEL, | |
| 118 std::string()); | |
| 119 } | |
| 120 | |
| 121 ProfileSyncService* OneClickSigninSyncObserver::GetSyncService( | |
| 122 content::WebContents* web_contents) { | |
| 123 Profile* profile = | |
| 124 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
| 125 return ProfileSyncServiceFactory::GetForProfile(profile); | |
| 126 } | |
| OLD | NEW |