OLD | NEW |
(Empty) | |
| 1 // Copyright 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 "ios/chrome/browser/ui/sync/sync_error_infobar_delegate.h" |
| 6 |
| 7 #import <UIKit/UIKit.h> |
| 8 |
| 9 #include <utility> |
| 10 |
| 11 #include "base/logging.h" |
| 12 #include "base/mac/scoped_nsobject.h" |
| 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/strings/sys_string_conversions.h" |
| 15 #include "components/browser_sync/profile_sync_service.h" |
| 16 #include "components/infobars/core/infobar.h" |
| 17 #include "components/infobars/core/infobar_delegate.h" |
| 18 #include "components/infobars/core/infobar_manager.h" |
| 19 #include "components/sync/driver/sync_service.h" |
| 20 #import "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 21 #include "ios/chrome/browser/sync/ios_chrome_profile_sync_service_factory.h" |
| 22 #include "ios/chrome/browser/sync/sync_setup_service.h" |
| 23 #include "ios/chrome/browser/sync/sync_setup_service_factory.h" |
| 24 #import "ios/chrome/browser/ui/commands/UIKit+ChromeExecuteCommand.h" |
| 25 #import "ios/chrome/browser/ui/commands/generic_chrome_command.h" |
| 26 #import "ios/chrome/browser/ui/sync/sync_util.h" |
| 27 #include "ui/base/l10n/l10n_util.h" |
| 28 |
| 29 // static |
| 30 bool SyncErrorInfoBarDelegate::Create(infobars::InfoBarManager* infobar_manager, |
| 31 ios::ChromeBrowserState* browser_state) { |
| 32 DCHECK(infobar_manager); |
| 33 std::unique_ptr<ConfirmInfoBarDelegate> delegate( |
| 34 new SyncErrorInfoBarDelegate(browser_state)); |
| 35 return !!infobar_manager->AddInfoBar( |
| 36 infobar_manager->CreateConfirmInfoBar(std::move(delegate))); |
| 37 } |
| 38 |
| 39 SyncErrorInfoBarDelegate::SyncErrorInfoBarDelegate( |
| 40 ios::ChromeBrowserState* browser_state) |
| 41 : browser_state_(browser_state) { |
| 42 DCHECK(!browser_state->IsOffTheRecord()); |
| 43 icon_ = gfx::Image([UIImage imageNamed:@"infobar_warning"]); |
| 44 SyncSetupService* sync_setup_service = |
| 45 SyncSetupServiceFactory::GetForBrowserState(browser_state); |
| 46 DCHECK(sync_setup_service); |
| 47 // Set all of the UI based on the sync state at the same time to ensure |
| 48 // they all correspond to the same sync error. |
| 49 error_state_ = sync_setup_service->GetSyncServiceState(); |
| 50 message_ = base::SysNSStringToUTF16( |
| 51 ios_internal::sync::GetSyncErrorMessageForBrowserState(browser_state_)); |
| 52 button_text_ = base::SysNSStringToUTF16( |
| 53 ios_internal::sync::GetSyncErrorButtonTitleForBrowserState( |
| 54 browser_state_)); |
| 55 command_.reset([ios_internal::sync::GetSyncCommandForBrowserState( |
| 56 browser_state_) retain]); |
| 57 |
| 58 // Register for sync status changes. |
| 59 syncer::SyncService* sync_service = |
| 60 IOSChromeProfileSyncServiceFactory::GetForBrowserState(browser_state_); |
| 61 sync_service->AddObserver(this); |
| 62 } |
| 63 |
| 64 SyncErrorInfoBarDelegate::~SyncErrorInfoBarDelegate() { |
| 65 syncer::SyncService* sync_service = |
| 66 IOSChromeProfileSyncServiceFactory::GetForBrowserState(browser_state_); |
| 67 sync_service->RemoveObserver(this); |
| 68 } |
| 69 |
| 70 infobars::InfoBarDelegate::InfoBarIdentifier |
| 71 SyncErrorInfoBarDelegate::GetIdentifier() const { |
| 72 return SYNC_ERROR_INFOBAR_DELEGATE; |
| 73 } |
| 74 |
| 75 base::string16 SyncErrorInfoBarDelegate::GetMessageText() const { |
| 76 return message_; |
| 77 } |
| 78 |
| 79 int SyncErrorInfoBarDelegate::GetButtons() const { |
| 80 return button_text_.empty() ? BUTTON_NONE : BUTTON_OK; |
| 81 } |
| 82 |
| 83 base::string16 SyncErrorInfoBarDelegate::GetButtonLabel( |
| 84 InfoBarButton button) const { |
| 85 DCHECK(button == BUTTON_OK); |
| 86 return button_text_; |
| 87 } |
| 88 |
| 89 gfx::Image SyncErrorInfoBarDelegate::GetIcon() const { |
| 90 return icon_; |
| 91 } |
| 92 |
| 93 bool SyncErrorInfoBarDelegate::Accept() { |
| 94 DCHECK(command_); |
| 95 UIWindow* main_window = [[UIApplication sharedApplication] keyWindow]; |
| 96 DCHECK(main_window); |
| 97 [main_window chromeExecuteCommand:command_]; |
| 98 return false; |
| 99 } |
| 100 |
| 101 void SyncErrorInfoBarDelegate::OnStateChanged() { |
| 102 // If the inforbar is in the process of being removed, nothing must be done. |
| 103 infobars::InfoBar* infobar = this->infobar(); |
| 104 if (!infobar) |
| 105 return; |
| 106 SyncSetupService* sync_setup_service = |
| 107 SyncSetupServiceFactory::GetForBrowserState(browser_state_); |
| 108 SyncSetupService::SyncServiceState new_error_state = |
| 109 sync_setup_service->GetSyncServiceState(); |
| 110 if (error_state_ == new_error_state) |
| 111 return; |
| 112 error_state_ = new_error_state; |
| 113 if (ios_internal::sync::IsTransientSyncError(new_error_state)) { |
| 114 infobar->RemoveSelf(); |
| 115 } else { |
| 116 infobars::InfoBarManager* infobar_manager = infobar->owner(); |
| 117 if (infobar_manager) { |
| 118 std::unique_ptr<ConfirmInfoBarDelegate> new_infobar_delegate( |
| 119 new SyncErrorInfoBarDelegate(browser_state_)); |
| 120 infobar_manager->ReplaceInfoBar(infobar, |
| 121 infobar_manager->CreateConfirmInfoBar( |
| 122 std::move(new_infobar_delegate))); |
| 123 } |
| 124 } |
| 125 } |
OLD | NEW |