Chromium Code Reviews| 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 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.
| |
| 95 StopObservingSync(); | |
| 96 } | |
| 97 | |
| 98 void AppNotifyChannelUIImpl::PromptSyncSetup( | |
| 99 AppNotifyChannelUI::Delegate* delegate) { | |
| 100 CHECK(delegate_ == NULL); | |
| 101 delegate_ = delegate; | |
| 102 | |
| 103 if (!browser_->profile()->HasProfileSyncService()) { | |
| 104 delegate_->OnSyncSetupResult(false); | |
| 105 return; | |
| 106 } | |
| 107 | |
| 108 InfoBarTabHelper* helper = wrapper_->infobar_tab_helper(); | |
| 109 helper->AddInfoBar(new AppNotifyChannelUIImpl::InfoBar( | |
| 110 this, helper, app_name_)); | |
| 111 } | |
| 112 | |
| 113 void AppNotifyChannelUIImpl::OnInfoBarResult(bool accepted) { | |
| 114 if (accepted) { | |
| 115 StartObservingSync(); | |
| 116 browser_->ShowSyncSetup(); | |
| 117 } else { | |
| 118 delegate_->OnSyncSetupResult(false); | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 void AppNotifyChannelUIImpl::OnStateChanged() { | |
| 123 ProfileSyncService* sync_service = | |
| 124 browser_->profile()->GetProfileSyncService(); | |
| 125 bool finished = got_first_sync_callback_ && !sync_service->SetupInProgress(); | |
| 126 got_first_sync_callback_ = true; | |
| 127 | |
| 128 if (finished) { | |
| 129 StopObservingSync(); | |
| 130 delegate_->OnSyncSetupResult(sync_service->HasSyncSetupCompleted()); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 void AppNotifyChannelUIImpl::StartObservingSync() { | |
| 135 CHECK(!observing_sync_); | |
| 136 observing_sync_ = true; | |
| 137 browser_->profile()->GetProfileSyncService()->AddObserver(this); | |
| 138 } | |
| 139 | |
| 140 void AppNotifyChannelUIImpl::StopObservingSync() { | |
| 141 CHECK(observing_sync_); | |
| 142 observing_sync_ = false; | |
| 143 browser_->profile()->GetProfileSyncService()->RemoveObserver(this); | |
| 144 } | |
| OLD | NEW |