OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/sync/profile_sync_service.h" |
| 11 #include "chrome/browser/sync/sync_setup_wizard.h" |
| 12 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" |
| 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 15 #include "content/public/browser/notification_details.h" |
| 16 #include "content/public/browser/notification_observer.h" |
| 17 #include "content/public/browser/notification_registrar.h" |
| 18 #include "content/public/browser/notification_source.h" |
| 19 #include "content/public/browser/notification_service.h" |
| 20 #include "content/public/browser/notification_types.h" |
| 21 #include "grit/generated_resources.h" |
| 22 #include "ui/base/l10n/l10n_util.h" |
| 23 |
| 24 class AppNotifyChannelUIImpl::InfoBar : public ConfirmInfoBarDelegate { |
| 25 public: |
| 26 InfoBar(AppNotifyChannelUIImpl* creator, |
| 27 InfoBarTabHelper* helper, |
| 28 const std::string& app_name); |
| 29 virtual ~InfoBar(); |
| 30 |
| 31 // ConfirmInfoBarDelegate. |
| 32 virtual string16 GetMessageText() const OVERRIDE; |
| 33 virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; |
| 34 virtual bool Accept() OVERRIDE; |
| 35 virtual bool Cancel() OVERRIDE; |
| 36 virtual void InfoBarDismissed() OVERRIDE; |
| 37 |
| 38 private: |
| 39 AppNotifyChannelUIImpl* creator_; |
| 40 std::string app_name_; |
| 41 |
| 42 DISALLOW_COPY_AND_ASSIGN(InfoBar); |
| 43 }; |
| 44 |
| 45 AppNotifyChannelUIImpl::InfoBar::InfoBar( |
| 46 AppNotifyChannelUIImpl* creator, |
| 47 InfoBarTabHelper* helper, |
| 48 const std::string& app_name) |
| 49 : ConfirmInfoBarDelegate(helper), creator_(creator), app_name_(app_name) { |
| 50 } |
| 51 |
| 52 AppNotifyChannelUIImpl::InfoBar::~InfoBar() {} |
| 53 |
| 54 string16 AppNotifyChannelUIImpl::InfoBar::GetMessageText() const { |
| 55 return l10n_util::GetStringFUTF16(IDS_APP_NOTIFICATION_NEED_SIGNIN, |
| 56 UTF8ToUTF16(app_name_)); |
| 57 } |
| 58 |
| 59 string16 AppNotifyChannelUIImpl::InfoBar::GetButtonLabel( |
| 60 InfoBarButton button) const { |
| 61 if (button == BUTTON_OK) { |
| 62 return l10n_util::GetStringUTF16(IDS_APP_NOTIFICATION_NEED_SIGNIN_ACCEPT); |
| 63 } else if (button == BUTTON_CANCEL) { |
| 64 return l10n_util::GetStringUTF16(IDS_APP_NOTIFICATION_NEED_SIGNIN_CANCEL); |
| 65 } else { |
| 66 NOTREACHED(); |
| 67 } |
| 68 return string16(); |
| 69 } |
| 70 |
| 71 bool AppNotifyChannelUIImpl::InfoBar::Accept() { |
| 72 creator_->OnInfoBarResult(true); |
| 73 return true; |
| 74 } |
| 75 |
| 76 bool AppNotifyChannelUIImpl::InfoBar::Cancel() { |
| 77 creator_->OnInfoBarResult(false); |
| 78 return true; |
| 79 } |
| 80 |
| 81 void AppNotifyChannelUIImpl::InfoBar::InfoBarDismissed() { |
| 82 Cancel(); |
| 83 } |
| 84 |
| 85 |
| 86 AppNotifyChannelUIImpl::AppNotifyChannelUIImpl(Browser* browser, |
| 87 TabContentsWrapper* wrapper, |
| 88 const std::string& app_name) |
| 89 : browser_(browser), wrapper_(wrapper), app_name_(app_name), |
| 90 delegate_(NULL), observing_sync_(false), got_first_sync_callback_(false) { |
| 91 } |
| 92 |
| 93 AppNotifyChannelUIImpl::~AppNotifyChannelUIImpl() { |
| 94 // We should have either not started observing sync, or already called |
| 95 // StopObservingSync by this point. |
| 96 CHECK(!observing_sync_); |
| 97 } |
| 98 |
| 99 void AppNotifyChannelUIImpl::PromptSyncSetup( |
| 100 AppNotifyChannelUI::Delegate* delegate) { |
| 101 CHECK(delegate_ == NULL); |
| 102 delegate_ = delegate; |
| 103 |
| 104 if (!browser_->profile()->HasProfileSyncService()) { |
| 105 delegate_->OnSyncSetupResult(false); |
| 106 return; |
| 107 } |
| 108 |
| 109 InfoBarTabHelper* helper = wrapper_->infobar_tab_helper(); |
| 110 helper->AddInfoBar(new AppNotifyChannelUIImpl::InfoBar( |
| 111 this, helper, app_name_)); |
| 112 } |
| 113 |
| 114 void AppNotifyChannelUIImpl::OnInfoBarResult(bool accepted) { |
| 115 if (accepted) { |
| 116 StartObservingSync(); |
| 117 browser_->ShowSyncSetup(); |
| 118 } else { |
| 119 delegate_->OnSyncSetupResult(false); |
| 120 } |
| 121 } |
| 122 |
| 123 void AppNotifyChannelUIImpl::OnStateChanged() { |
| 124 ProfileSyncService* sync_service = |
| 125 browser_->profile()->GetProfileSyncService(); |
| 126 bool finished = got_first_sync_callback_ && !sync_service->SetupInProgress(); |
| 127 got_first_sync_callback_ = true; |
| 128 |
| 129 if (finished) { |
| 130 StopObservingSync(); |
| 131 delegate_->OnSyncSetupResult(sync_service->HasSyncSetupCompleted()); |
| 132 } |
| 133 } |
| 134 |
| 135 void AppNotifyChannelUIImpl::StartObservingSync() { |
| 136 CHECK(!observing_sync_); |
| 137 observing_sync_ = true; |
| 138 browser_->profile()->GetProfileSyncService()->AddObserver(this); |
| 139 } |
| 140 |
| 141 void AppNotifyChannelUIImpl::StopObservingSync() { |
| 142 CHECK(observing_sync_); |
| 143 observing_sync_ = false; |
| 144 browser_->profile()->GetProfileSyncService()->RemoveObserver(this); |
| 145 } |
OLD | NEW |