Chromium Code Reviews| Index: chrome/browser/extensions/app_notify_channel_ui.cc |
| diff --git a/chrome/browser/extensions/app_notify_channel_ui.cc b/chrome/browser/extensions/app_notify_channel_ui.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c5ab1737388d7fdf7c1f6e866cfbdb2072d0c22f |
| --- /dev/null |
| +++ b/chrome/browser/extensions/app_notify_channel_ui.cc |
| @@ -0,0 +1,144 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/extensions/app_notify_channel_ui.h" |
| + |
| +#include "base/utf_string_conversions.h" |
| +#include "chrome/browser/infobars/infobar_tab_helper.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/sync/profile_sync_service.h" |
| +#include "chrome/browser/sync/sync_setup_wizard.h" |
| +#include "chrome/browser/tab_contents/confirm_infobar_delegate.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| +#include "content/public/browser/notification_details.h" |
| +#include "content/public/browser/notification_observer.h" |
| +#include "content/public/browser/notification_registrar.h" |
| +#include "content/public/browser/notification_source.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "content/public/browser/notification_types.h" |
| +#include "grit/generated_resources.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| + |
| +class AppNotifyChannelUIImpl::InfoBar : public ConfirmInfoBarDelegate { |
| + public: |
| + InfoBar(AppNotifyChannelUIImpl* creator, |
| + InfoBarTabHelper* helper, |
| + const std::string& app_name); |
| + virtual ~InfoBar(); |
| + |
| + // ConfirmInfoBarDelegate. |
| + virtual string16 GetMessageText() const OVERRIDE; |
| + virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; |
| + virtual bool Accept() OVERRIDE; |
| + virtual bool Cancel() OVERRIDE; |
| + virtual void InfoBarDismissed() OVERRIDE; |
| + |
| + private: |
| + AppNotifyChannelUIImpl* creator_; |
| + std::string app_name_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(InfoBar); |
| +}; |
| + |
| +AppNotifyChannelUIImpl::InfoBar::InfoBar( |
| + AppNotifyChannelUIImpl* creator, |
| + InfoBarTabHelper* helper, |
| + const std::string& app_name) |
| + : ConfirmInfoBarDelegate(helper), creator_(creator), app_name_(app_name) { |
| +} |
| + |
| +AppNotifyChannelUIImpl::InfoBar::~InfoBar() {} |
| + |
| +string16 AppNotifyChannelUIImpl::InfoBar::GetMessageText() const { |
| + return l10n_util::GetStringFUTF16(IDS_APP_NOTIFICATION_NEED_SIGNIN, |
| + UTF8ToUTF16(app_name_)); |
| +} |
| + |
| +string16 AppNotifyChannelUIImpl::InfoBar::GetButtonLabel( |
| + InfoBarButton button) const { |
| + if (button == BUTTON_OK) { |
| + return l10n_util::GetStringUTF16(IDS_APP_NOTIFICATION_NEED_SIGNIN_ACCEPT); |
| + } else if (button == BUTTON_CANCEL) { |
| + return l10n_util::GetStringUTF16(IDS_APP_NOTIFICATION_NEED_SIGNIN_CANCEL); |
| + } else { |
| + NOTREACHED(); |
| + } |
| + return string16(); |
| +} |
| + |
| +bool AppNotifyChannelUIImpl::InfoBar::Accept() { |
| + creator_->OnInfoBarResult(true); |
| + return true; |
| +} |
| + |
| +bool AppNotifyChannelUIImpl::InfoBar::Cancel() { |
| + creator_->OnInfoBarResult(false); |
| + return true; |
| +} |
| + |
| +void AppNotifyChannelUIImpl::InfoBar::InfoBarDismissed() { |
| + Cancel(); |
| +} |
| + |
| + |
| +AppNotifyChannelUIImpl::AppNotifyChannelUIImpl(Browser* browser, |
| + TabContentsWrapper* wrapper, |
| + const std::string& app_name) |
| + : browser_(browser), wrapper_(wrapper), app_name_(app_name), |
| + delegate_(NULL), observing_sync_(false), got_first_sync_callback_(false) { |
| +} |
| + |
| +AppNotifyChannelUIImpl::~AppNotifyChannelUIImpl() { |
| + if (observing_sync_) |
|
Mihai Parparita -not on Chrome
2011/10/27 22:21:05
Can this happen? In AppNotifyChannelSetup::Start y
asargent_no_longer_on_chrome
2011/10/28 17:09:22
Good point. Done.
|
| + StopObservingSync(); |
| +} |
| + |
| +void AppNotifyChannelUIImpl::PromptSyncSetup( |
| + AppNotifyChannelUI::Delegate* delegate) { |
| + CHECK(delegate_ == NULL); |
| + delegate_ = delegate; |
| + |
| + if (!browser_->profile()->HasProfileSyncService()) { |
| + delegate_->OnSyncSetupResult(false); |
| + return; |
| + } |
| + |
| + InfoBarTabHelper* helper = wrapper_->infobar_tab_helper(); |
| + helper->AddInfoBar(new AppNotifyChannelUIImpl::InfoBar( |
| + this, helper, app_name_)); |
| +} |
| + |
| +void AppNotifyChannelUIImpl::OnInfoBarResult(bool accepted) { |
| + if (accepted) { |
| + StartObservingSync(); |
| + browser_->ShowSyncSetup(); |
| + } else { |
| + delegate_->OnSyncSetupResult(false); |
| + } |
| +} |
| + |
| +void AppNotifyChannelUIImpl::OnStateChanged() { |
| + ProfileSyncService* sync_service = |
| + browser_->profile()->GetProfileSyncService(); |
| + bool finished = got_first_sync_callback_ && !sync_service->SetupInProgress(); |
| + got_first_sync_callback_ = true; |
| + |
| + if (finished) { |
| + StopObservingSync(); |
| + delegate_->OnSyncSetupResult(sync_service->HasSyncSetupCompleted()); |
| + } |
| +} |
| + |
| +void AppNotifyChannelUIImpl::StartObservingSync() { |
| + CHECK(!observing_sync_); |
| + observing_sync_ = true; |
| + browser_->profile()->GetProfileSyncService()->AddObserver(this); |
| +} |
| + |
| +void AppNotifyChannelUIImpl::StopObservingSync() { |
| + CHECK(observing_sync_); |
| + observing_sync_ = false; |
| + browser_->profile()->GetProfileSyncService()->RemoveObserver(this); |
| +} |