summaryrefslogtreecommitdiffstats
path: root/ios/chrome/browser/infobars/infobar_container_ios.mm
blob: ecd0ebe83bd45816b5a4577a83cb70247dd89b95 (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
// 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_container_ios.h"

#include <stddef.h>
#import <UIKit/UIKit.h>

#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "ios/chrome/browser/infobars/infobar.h"
#include "ios/chrome/browser/infobars/infobar_container_view.h"
#include "ios/public/provider/chrome/browser/chrome_browser_provider.h"

InfoBarContainerIOS::InfoBarContainerIOS(
    infobars::InfoBarContainer::Delegate* delegate)
    : InfoBarContainer(delegate), delegate_(delegate) {
  DCHECK(delegate);
  container_view_.reset([[InfoBarContainerView alloc] init]);
  [container_view_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth |
                                       UIViewAutoresizingFlexibleTopMargin];
}

InfoBarContainerIOS::~InfoBarContainerIOS() {
  delegate_ = nullptr;
  RemoveAllInfoBarsForDestruction();
}

InfoBarContainerView* InfoBarContainerIOS::view() {
  return container_view_;
}

void InfoBarContainerIOS::PlatformSpecificAddInfoBar(infobars::InfoBar* infobar,
                                                     size_t position) {
  InfoBarIOS* infobar_ios = static_cast<InfoBarIOS*>(infobar);
  [container_view_ addInfoBar:infobar_ios position:position];
}

void InfoBarContainerIOS::PlatformSpecificRemoveInfoBar(
    infobars::InfoBar* infobar) {
  InfoBarIOS* infobar_ios = static_cast<InfoBarIOS*>(infobar);
  infobar_ios->RemoveView();
  // If total_height() is 0, then the infobar was removed after an animation. In
  // this case, signal the delegate that the state changed.
  // Otherwise, the infobar is being replaced by another one. Do not call the
  // delegate in this case, as the delegate will be updated when the new infobar
  // is added.
  if (infobar->total_height() == 0 && delegate_)
    delegate_->InfoBarContainerStateChanged(false);

  // TODO(rohitrao, jif): [Merge 239355] Upstream InfoBarContainer deletes the
  // infobar. Avoid deleting it here.
  // crbug.com/327290
  // base::MessageLoop::current()->DeleteSoon(FROM_HERE, infobar_ios);
}

void InfoBarContainerIOS::PlatformSpecificInfoBarStateChanged(
    bool is_animating) {
  [container_view_ setUserInteractionEnabled:!is_animating];
  [container_view_ setNeedsLayout];
}

void InfoBarContainerIOS::SuspendInfobars() {
  ios::GetChromeBrowserProvider()->SetUIViewAlphaWithAnimation(container_view_,
                                                               0);
}

void InfoBarContainerIOS::RestoreInfobars() {
  ios::GetChromeBrowserProvider()->SetUIViewAlphaWithAnimation(container_view_,
                                                               1);
}