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/translate/translate_infobar_tags.h"
13 using infobars::InfoBar;
14 using infobars::InfoBarDelegate;
16 InfoBarIOS::InfoBarIOS(scoped_ptr<InfoBarDelegate> delegate)
17 : InfoBar(delegate.Pass()) {
20 InfoBarIOS::~InfoBarIOS() {
22 [controller_ detachView];
26 void InfoBarIOS::SetController(InfoBarController* controller) {
27 controller_.reset([controller retain]);
30 void InfoBarIOS::Layout(CGRect container_bounds) {
32 if ([controller_ view]) {
33 [[controller_ view] setFrame:container_bounds];
35 [controller_ layoutForDelegate:delegate() frame:container_bounds];
37 SetBarTargetHeight([controller_ barHeight]);
40 UIView* InfoBarIOS::view() {
42 return [controller_ view];
45 void InfoBarIOS::RemoveView() {
47 [controller_ removeView];
50 void InfoBarIOS::PlatformSpecificOnHeightsRecalculated() {
52 [controller_ onHeightsRecalculated:bar_height()];
55 #pragma mark - InfoBarViewDelegate
57 void InfoBarIOS::SetInfoBarTargetHeight(int height) {
58 SetBarTargetHeight(height);
61 // Some infobar button was pressed.
62 void InfoBarIOS::InfoBarButtonDidPress(NSUInteger button_id) {
63 // Do not add new logic for specific info bar delegates.
64 // TODO(droger): Move the logic elsewhere, http://crbug.com/307552.
65 // If not owned, the infobar has already been removed.
68 if (delegate()->AsConfirmInfoBarDelegate()) {
69 ConfirmInfoBarDelegate* confirmDelegate =
70 delegate()->AsConfirmInfoBarDelegate();
71 if ((button_id == ConfirmInfoBarDelegate::BUTTON_OK &&
72 confirmDelegate->Accept()) ||
73 (button_id == ConfirmInfoBarDelegate::BUTTON_CANCEL &&
74 delegate()->AsConfirmInfoBarDelegate()->Cancel())) {
77 } else if (delegate()->AsTranslateInfoBarDelegate()) {
78 translate::TranslateInfoBarDelegate* translateDelegate =
79 delegate()->AsTranslateInfoBarDelegate();
81 case TranslateInfoBarIOSTag::AFTER_DONE:
84 case TranslateInfoBarIOSTag::AFTER_REVERT:
85 translateDelegate->RevertTranslation();
87 case TranslateInfoBarIOSTag::BEFORE_ACCEPT:
88 translateDelegate->Translate();
90 case TranslateInfoBarIOSTag::BEFORE_DENY:
91 translateDelegate->TranslationDeclined();
92 if (translateDelegate->ShouldShowNeverTranslateShortcut())
93 translateDelegate->ShowNeverTranslateInfobar();
97 case TranslateInfoBarIOSTag::DENY_LANGUAGE:
98 translateDelegate->NeverTranslatePageLanguage();
101 case TranslateInfoBarIOSTag::DENY_WEBSITE:
102 if (!translateDelegate->IsSiteBlacklisted())
103 translateDelegate->ToggleSiteBlacklist();
106 case TranslateInfoBarIOSTag::MESSAGE:
107 translateDelegate->MessageInfoBarButtonPressed();
110 NOTREACHED() << "Unexpected Translate button label";
116 void InfoBarIOS::InfoBarDidCancel() {
117 // If not owned, the infobar has already been removed.
120 delegate()->InfoBarDismissed();