summaryrefslogtreecommitdiffstats
path: root/ios/chrome/browser/infobars/infobar.mm
blob: 00ae46b66429829e43f7ac4e7f724fb30f038b27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ios/chrome/browser/infobars/infobar.h"

#include <utility>

#include "base/logging.h"
#include "components/infobars/core/confirm_infobar_delegate.h"
#include "components/translate/core/browser/translate_infobar_delegate.h"
#import "ios/chrome/browser/infobars/confirm_infobar_controller.h"
#include "ios/chrome/browser/infobars/infobar_controller.h"
#include "ios/chrome/browser/translate/translate_infobar_tags.h"

using infobars::InfoBar;
using infobars::InfoBarDelegate;

InfoBarIOS::InfoBarIOS(scoped_ptr<InfoBarDelegate> delegate)
    : InfoBar(std::move(delegate)) {}

InfoBarIOS::~InfoBarIOS() {
  DCHECK(controller_);
  [controller_ detachView];
  controller_.reset();
}

void InfoBarIOS::SetController(InfoBarController* controller) {
  controller_.reset([controller retain]);
}

void InfoBarIOS::Layout(CGRect container_bounds) {
  DCHECK(controller_);
  if ([controller_ view]) {
    [[controller_ view] setFrame:container_bounds];
  } else {
    [controller_ layoutForDelegate:delegate() frame:container_bounds];
  }
  SetBarTargetHeight([controller_ barHeight]);
}

UIView* InfoBarIOS::view() {
  DCHECK(controller_);
  return [controller_ view];
}

void InfoBarIOS::RemoveView() {
  DCHECK(controller_);
  [controller_ removeView];
}

void InfoBarIOS::PlatformSpecificOnHeightsRecalculated() {
  DCHECK(controller_);
  [controller_ onHeightsRecalculated:bar_height()];
}

#pragma mark - InfoBarViewDelegate

void InfoBarIOS::SetInfoBarTargetHeight(int height) {
  SetBarTargetHeight(height);
}

// Some infobar button was pressed.
void InfoBarIOS::InfoBarButtonDidPress(NSUInteger button_id) {
  // Do not add new logic for specific info bar delegates.
  // TODO(droger): Move the logic elsewhere, http://crbug.com/307552.
  // If not owned, the infobar has already been removed.
  if (!owner())
    return;
  if (delegate()->AsConfirmInfoBarDelegate()) {
    ConfirmInfoBarDelegate* confirmDelegate =
        delegate()->AsConfirmInfoBarDelegate();
    if ((button_id == ConfirmInfoBarDelegate::BUTTON_OK &&
         confirmDelegate->Accept()) ||
        (button_id == ConfirmInfoBarDelegate::BUTTON_CANCEL &&
         delegate()->AsConfirmInfoBarDelegate()->Cancel())) {
      RemoveSelf();
    }
  } else if (delegate()->AsTranslateInfoBarDelegate()) {
    translate::TranslateInfoBarDelegate* translateDelegate =
        delegate()->AsTranslateInfoBarDelegate();
    switch (button_id) {
      case TranslateInfoBarIOSTag::AFTER_DONE:
        InfoBarDidCancel();
        break;
      case TranslateInfoBarIOSTag::AFTER_REVERT:
        translateDelegate->RevertTranslation();
        break;
      case TranslateInfoBarIOSTag::BEFORE_ACCEPT:
        translateDelegate->Translate();
        break;
      case TranslateInfoBarIOSTag::BEFORE_DENY:
        translateDelegate->TranslationDeclined();
        if (translateDelegate->ShouldShowNeverTranslateShortcut())
          translateDelegate->ShowNeverTranslateInfobar();
        else
          RemoveSelf();
        break;
      case TranslateInfoBarIOSTag::DENY_LANGUAGE:
        translateDelegate->NeverTranslatePageLanguage();
        RemoveSelf();
        break;
      case TranslateInfoBarIOSTag::DENY_WEBSITE:
        if (!translateDelegate->IsSiteBlacklisted())
          translateDelegate->ToggleSiteBlacklist();
        RemoveSelf();
        break;
      case TranslateInfoBarIOSTag::MESSAGE:
        translateDelegate->MessageInfoBarButtonPressed();
        break;
      default:
        NOTREACHED() << "Unexpected Translate button label";
        break;
    }
  }
}

void InfoBarIOS::InfoBarDidCancel() {
  // If not owned, the infobar has already been removed.
  if (!owner())
    return;
  delegate()->InfoBarDismissed();
  RemoveSelf();
}