Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ios / chrome / browser / infobars / infobar.mm
blobb7f7453caa5ff2f939f0c0605f702e436b5f0be8
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() {
22   DCHECK(controller_);
23   [controller_ detachView];
24   controller_.reset();
27 void InfoBarIOS::SetController(InfoBarController* controller) {
28   controller_.reset([controller retain]);
31 void InfoBarIOS::Layout(CGRect container_bounds) {
32   DCHECK(controller_);
33   if ([controller_ view]) {
34     [[controller_ view] setFrame:container_bounds];
35   } else {
36     [controller_ layoutForDelegate:delegate() frame:container_bounds];
37   }
38   SetBarTargetHeight([controller_ barHeight]);
41 UIView* InfoBarIOS::view() {
42   DCHECK(controller_);
43   return [controller_ view];
46 void InfoBarIOS::RemoveView() {
47   DCHECK(controller_);
48   [controller_ removeView];
51 void InfoBarIOS::PlatformSpecificOnHeightsRecalculated() {
52   DCHECK(controller_);
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.
67   if (!owner())
68     return;
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())) {
76       RemoveSelf();
77     }
78   } else if (delegate()->AsTranslateInfoBarDelegate()) {
79     translate::TranslateInfoBarDelegate* translateDelegate =
80         delegate()->AsTranslateInfoBarDelegate();
81     switch (button_id) {
82       case TranslateInfoBarIOSTag::AFTER_DONE:
83         InfoBarDidCancel();
84         break;
85       case TranslateInfoBarIOSTag::AFTER_REVERT:
86         translateDelegate->RevertTranslation();
87         break;
88       case TranslateInfoBarIOSTag::BEFORE_ACCEPT:
89         translateDelegate->Translate();
90         break;
91       case TranslateInfoBarIOSTag::BEFORE_DENY:
92         translateDelegate->TranslationDeclined();
93         if (translateDelegate->ShouldShowNeverTranslateShortcut())
94           translateDelegate->ShowNeverTranslateInfobar();
95         else
96           RemoveSelf();
97         break;
98       case TranslateInfoBarIOSTag::DENY_LANGUAGE:
99         translateDelegate->NeverTranslatePageLanguage();
100         RemoveSelf();
101         break;
102       case TranslateInfoBarIOSTag::DENY_WEBSITE:
103         if (!translateDelegate->IsSiteBlacklisted())
104           translateDelegate->ToggleSiteBlacklist();
105         RemoveSelf();
106         break;
107       case TranslateInfoBarIOSTag::MESSAGE:
108         translateDelegate->MessageInfoBarButtonPressed();
109         break;
110       default:
111         NOTREACHED() << "Unexpected Translate button label";
112         break;
113     }
114   }
117 void InfoBarIOS::InfoBarDidCancel() {
118   // If not owned, the infobar has already been removed.
119   if (!owner())
120     return;
121   delegate()->InfoBarDismissed();
122   RemoveSelf();