| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/extensions/app_notify_channel_ui.h" | |
| 6 | |
| 7 #include "base/utf_string_conversions.h" | |
| 8 #include "chrome/browser/infobars/infobar_tab_helper.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | |
| 10 #include "chrome/browser/signin/signin_manager.h" | |
| 11 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 12 #include "chrome/browser/sync/profile_sync_service.h" | |
| 13 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
| 14 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" | |
| 15 #include "chrome/browser/ui/browser.h" | |
| 16 #include "chrome/browser/ui/browser_finder.h" | |
| 17 #include "chrome/browser/ui/chrome_pages.h" | |
| 18 #include "chrome/browser/ui/tab_contents/tab_contents.h" | |
| 19 #include "chrome/browser/ui/webui/signin/login_ui_service.h" | |
| 20 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" | |
| 21 #include "chrome/common/url_constants.h" | |
| 22 #include "content/public/browser/notification_details.h" | |
| 23 #include "content/public/browser/notification_observer.h" | |
| 24 #include "content/public/browser/notification_registrar.h" | |
| 25 #include "content/public/browser/notification_source.h" | |
| 26 #include "content/public/browser/notification_service.h" | |
| 27 #include "content/public/browser/notification_types.h" | |
| 28 #include "grit/generated_resources.h" | |
| 29 #include "ui/base/l10n/l10n_util.h" | |
| 30 | |
| 31 class AppNotifyChannelUIImpl::InfoBar : public ConfirmInfoBarDelegate { | |
| 32 public: | |
| 33 InfoBar(AppNotifyChannelUIImpl* creator, | |
| 34 InfoBarTabHelper* helper, | |
| 35 const std::string& app_name); | |
| 36 virtual ~InfoBar(); | |
| 37 | |
| 38 // ConfirmInfoBarDelegate. | |
| 39 virtual string16 GetMessageText() const OVERRIDE; | |
| 40 virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; | |
| 41 virtual bool Accept() OVERRIDE; | |
| 42 virtual bool Cancel() OVERRIDE; | |
| 43 virtual void InfoBarDismissed() OVERRIDE; | |
| 44 | |
| 45 private: | |
| 46 AppNotifyChannelUIImpl* creator_; | |
| 47 std::string app_name_; | |
| 48 | |
| 49 DISALLOW_COPY_AND_ASSIGN(InfoBar); | |
| 50 }; | |
| 51 | |
| 52 AppNotifyChannelUIImpl::InfoBar::InfoBar( | |
| 53 AppNotifyChannelUIImpl* creator, | |
| 54 InfoBarTabHelper* helper, | |
| 55 const std::string& app_name) | |
| 56 : ConfirmInfoBarDelegate(helper), creator_(creator), app_name_(app_name) { | |
| 57 } | |
| 58 | |
| 59 AppNotifyChannelUIImpl::InfoBar::~InfoBar() {} | |
| 60 | |
| 61 string16 AppNotifyChannelUIImpl::InfoBar::GetMessageText() const { | |
| 62 return l10n_util::GetStringFUTF16(IDS_APP_NOTIFICATION_NEED_SIGNIN, | |
| 63 UTF8ToUTF16(app_name_)); | |
| 64 } | |
| 65 | |
| 66 string16 AppNotifyChannelUIImpl::InfoBar::GetButtonLabel( | |
| 67 InfoBarButton button) const { | |
| 68 if (button == BUTTON_OK) { | |
| 69 return l10n_util::GetStringUTF16(IDS_APP_NOTIFICATION_NEED_SIGNIN_ACCEPT); | |
| 70 } else if (button == BUTTON_CANCEL) { | |
| 71 return l10n_util::GetStringUTF16(IDS_APP_NOTIFICATION_NEED_SIGNIN_CANCEL); | |
| 72 } else { | |
| 73 NOTREACHED(); | |
| 74 } | |
| 75 return string16(); | |
| 76 } | |
| 77 | |
| 78 bool AppNotifyChannelUIImpl::InfoBar::Accept() { | |
| 79 creator_->OnInfoBarResult(true); | |
| 80 return true; | |
| 81 } | |
| 82 | |
| 83 bool AppNotifyChannelUIImpl::InfoBar::Cancel() { | |
| 84 creator_->OnInfoBarResult(false); | |
| 85 return true; | |
| 86 } | |
| 87 | |
| 88 void AppNotifyChannelUIImpl::InfoBar::InfoBarDismissed() { | |
| 89 Cancel(); | |
| 90 } | |
| 91 | |
| 92 | |
| 93 AppNotifyChannelUIImpl::AppNotifyChannelUIImpl( | |
| 94 Profile* profile, | |
| 95 TabContents* tab_contents, | |
| 96 const std::string& app_name, | |
| 97 AppNotifyChannelUI::UIType ui_type) | |
| 98 : profile_(profile->GetOriginalProfile()), | |
| 99 tab_contents_(tab_contents), | |
| 100 app_name_(app_name), | |
| 101 ui_type_(ui_type), | |
| 102 delegate_(NULL), | |
| 103 observing_sync_(false), | |
| 104 wizard_shown_to_user_(false) { | |
| 105 } | |
| 106 | |
| 107 AppNotifyChannelUIImpl::~AppNotifyChannelUIImpl() { | |
| 108 // We should have either not started observing sync, or already called | |
| 109 // StopObservingSync by this point. | |
| 110 CHECK(!observing_sync_); | |
| 111 } | |
| 112 | |
| 113 void AppNotifyChannelUIImpl::PromptSyncSetup( | |
| 114 AppNotifyChannelUI::Delegate* delegate) { | |
| 115 CHECK(delegate_ == NULL); | |
| 116 delegate_ = delegate; | |
| 117 | |
| 118 if (!ProfileSyncServiceFactory::GetInstance()->HasProfileSyncService( | |
| 119 profile_)) { | |
| 120 delegate_->OnSyncSetupResult(false); | |
| 121 return; | |
| 122 } | |
| 123 | |
| 124 if (ui_type_ == NO_INFOBAR) { | |
| 125 OnInfoBarResult(true); | |
| 126 return; | |
| 127 } | |
| 128 | |
| 129 InfoBarTabHelper* helper = tab_contents_->infobar_tab_helper(); | |
| 130 helper->AddInfoBar(new AppNotifyChannelUIImpl::InfoBar( | |
| 131 this, helper, app_name_)); | |
| 132 } | |
| 133 | |
| 134 void AppNotifyChannelUIImpl::OnInfoBarResult(bool accepted) { | |
| 135 if (!accepted) { | |
| 136 delegate_->OnSyncSetupResult(false); | |
| 137 return; | |
| 138 } | |
| 139 | |
| 140 StartObservingSync(); | |
| 141 // Bring up the login page. | |
| 142 LoginUIService* login_ui_service = | |
| 143 LoginUIServiceFactory::GetForProfile(profile_); | |
| 144 LoginUIService::LoginUI* login_ui = login_ui_service->current_login_ui(); | |
| 145 if (login_ui) { | |
| 146 // Some sort of login UI is already visible. | |
| 147 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_); | |
| 148 if (signin->GetAuthenticatedUsername().empty()) { | |
| 149 // User is not logged in yet, so just bring up the login UI (could be | |
| 150 // the promo UI). | |
| 151 login_ui->FocusUI(); | |
| 152 return; | |
| 153 } else { | |
| 154 // User is already logged in, so close whatever sync config UI the | |
| 155 // user is looking at and display new login UI. | |
| 156 login_ui->CloseUI(); | |
| 157 DCHECK(!login_ui_service->current_login_ui()); | |
| 158 } | |
| 159 } | |
| 160 // Any existing UI is now closed - display new login UI. | |
| 161 Browser* browser = browser::FindBrowserWithWebContents( | |
| 162 tab_contents_->web_contents()); | |
| 163 chrome::ShowSettingsSubPage(browser, chrome::kSyncSetupForceLoginSubPage); | |
| 164 } | |
| 165 | |
| 166 void AppNotifyChannelUIImpl::OnStateChanged() { | |
| 167 #if !defined(OS_ANDROID) | |
| 168 ProfileSyncService* sync_service = | |
| 169 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_); | |
| 170 LoginUIService* login_service = | |
| 171 LoginUIServiceFactory::GetForProfile(profile_); | |
| 172 | |
| 173 bool wizard_visible = (login_service->current_login_ui() != NULL); | |
| 174 // ProfileSyncService raises OnStateChanged many times. Even multiple | |
| 175 // times before the wizard actually becomes visible for the first time. | |
| 176 // So we have to wait for the wizard to become visible once and then we | |
| 177 // wait for it to get dismissed. | |
| 178 bool finished = wizard_shown_to_user_ && !wizard_visible; | |
| 179 if (wizard_visible) | |
| 180 wizard_shown_to_user_ = true; | |
| 181 | |
| 182 if (finished) { | |
| 183 StopObservingSync(); | |
| 184 delegate_->OnSyncSetupResult(sync_service->HasSyncSetupCompleted()); | |
| 185 } | |
| 186 #endif // !defined(OS_ANDROID) | |
| 187 } | |
| 188 | |
| 189 void AppNotifyChannelUIImpl::StartObservingSync() { | |
| 190 CHECK(!observing_sync_); | |
| 191 observing_sync_ = true; | |
| 192 ProfileSyncServiceFactory::GetInstance()->GetForProfile( | |
| 193 profile_)->AddObserver(this); | |
| 194 } | |
| 195 | |
| 196 void AppNotifyChannelUIImpl::StopObservingSync() { | |
| 197 CHECK(observing_sync_); | |
| 198 observing_sync_ = false; | |
| 199 ProfileSyncServiceFactory::GetInstance()->GetForProfile( | |
| 200 profile_)->RemoveObserver(this); | |
| 201 } | |
| OLD | NEW |