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.
5 #include "ios/chrome/browser/infobars/infobar.h"
7 #include "base/logging.h"
8 #include "components/infobars/core/confirm_infobar_delegate.h"
9 #include "components/translate/core/browser/translate_infobar_delegate.h"
10 #import "ios/chrome/browser/infobars/confirm_infobar_controller.h"
11 #include "ios/chrome/browser/infobars/infobar_controller.h"
12 #include "ios/chrome/browser/translate/translate_infobar_tags.h"
14 using infobars::InfoBar;
15 using infobars::InfoBarDelegate;
17 InfoBarIOS::InfoBarIOS(scoped_ptr<InfoBarDelegate> delegate)
18 : InfoBar(delegate.Pass()) {
21 InfoBarIOS::~InfoBarIOS() {
23 [controller_ detachView];
27 void InfoBarIOS::SetController(InfoBarController* controller) {
28 controller_.reset([controller retain]);
31 void InfoBarIOS::Layout(CGRect container_bounds) {
33 if ([controller_ view]) {
34 [[controller_ view] setFrame:container_bounds];
36 [controller_ layoutForDelegate:delegate() frame:container_bounds];
38 SetBarTargetHeight([controller_ barHeight]);
41 UIView* InfoBarIOS::view() {
43 return [controller_ view];
46 void InfoBarIOS::RemoveView() {
48 [controller_ removeView];
51 void InfoBarIOS::PlatformSpecificOnHeightsRecalculated() {
53 [controller_ onHeightsRecalculated:bar_height()];
56 #pragma mark - InfoBarViewDelegate
58 void InfoBarIOS::SetInfoBarTargetHeight(int height) {
59 SetBarTargetHeight(height);
62 // Some infobar button was pressed.
63 void InfoBarIOS::InfoBarButtonDidPress(NSUInteger button_id) {
64 // Do not add new logic for specific info bar delegates.
65 // TODO(droger): Move the logic elsewhere, http://crbug.com/307552.
66 // If not owned, the infobar has already been removed.
69 if (delegate()->AsConfirmInfoBarDelegate()) {
70 ConfirmInfoBarDelegate* confirmDelegate =
71 delegate()->AsConfirmInfoBarDelegate();
72 if ((button_id == ConfirmInfoBarDelegate::BUTTON_OK &&
73 confirmDelegate->Accept()) ||
74 (button_id == ConfirmInfoBarDelegate::BUTTON_CANCEL &&
75 delegate()->AsConfirmInfoBarDelegate()->Cancel())) {
78 } else if (delegate()->AsTranslateInfoBarDelegate()) {
79 translate::TranslateInfoBarDelegate* translateDelegate =
80 delegate()->AsTranslateInfoBarDelegate();
82 case TranslateInfoBarIOSTag::AFTER_DONE:
85 case TranslateInfoBarIOSTag::AFTER_REVERT:
86 translateDelegate->RevertTranslation();
88 case TranslateInfoBarIOSTag::BEFORE_ACCEPT:
89 translateDelegate->Translate();
91 case TranslateInfoBarIOSTag::BEFORE_DENY:
92 translateDelegate->TranslationDeclined();
93 if (translateDelegate->ShouldShowNeverTranslateShortcut())
94 translateDelegate->ShowNeverTranslateInfobar();
98 case TranslateInfoBarIOSTag::DENY_LANGUAGE:
99 translateDelegate->NeverTranslatePageLanguage();
102 case TranslateInfoBarIOSTag::DENY_WEBSITE:
103 if (!translateDelegate->IsSiteBlacklisted())
104 translateDelegate->ToggleSiteBlacklist();
107 case TranslateInfoBarIOSTag::MESSAGE:
108 translateDelegate->MessageInfoBarButtonPressed();
111 NOTREACHED() << "Unexpected Translate button label";
117 void InfoBarIOS::InfoBarDidCancel() {
118 // If not owned, the infobar has already been removed.
121 delegate()->InfoBarDismissed();