172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
24a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
34a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// found in the LICENSE file.
44a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
54a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#ifndef CHROME_BROWSER_UI_VIEWS_INFOBARS_INFOBAR_CONTAINER_H_
64a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#define CHROME_BROWSER_UI_VIEWS_INFOBARS_INFOBAR_CONTAINER_H_
74a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#pragma once
84a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
9ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include <vector>
10dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
11ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/compiler_specific.h"
12ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "content/common/notification_observer.h"
13ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "content/common/notification_registrar.h"
14ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "third_party/skia/include/core/SkColor.h"
154a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
16dc0f95d653279beabeb9817299e2902918ba123eKristian Monsenclass InfoBar;
174a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdochclass InfoBarDelegate;
184a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdochclass TabContents;
194a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
20ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// InfoBarContainer is a cross-platform base class to handle the visibility-
21ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// related aspects of InfoBars.  While InfoBars own themselves, the
22ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// InfoBarContainer is responsible for telling particular InfoBars that they
23ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// should be hidden or visible.
24ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//
25ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Platforms need to subclass this to implement a few platform-specific
26ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// functions, which are pure virtual here.
27ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass InfoBarContainer : public NotificationObserver {
284a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch public:
294a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  class Delegate {
304a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch   public:
31ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    // The separator color may vary depending on where the container is hosted.
32ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    virtual SkColor GetInfoBarSeparatorColor() const = 0;
33ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
34ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    // The delegate is notified each time the infobar container changes height,
35ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    // as well as when it stops animating.
36ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    virtual void InfoBarContainerStateChanged(bool is_animating) = 0;
37ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
38ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    // The delegate needs to tell us whether "unspoofable" arrows should be
39ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    // drawn, and if so, at what |x| coordinate.  |x| may be NULL.
40ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    virtual bool DrawInfoBarArrows(int* x) const = 0;
41dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
42dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen   protected:
43dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    virtual ~Delegate();
444a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  };
454a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
464a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  explicit InfoBarContainer(Delegate* delegate);
474a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  virtual ~InfoBarContainer();
484a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
49dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // Changes the TabContents for which this container is showing infobars.  This
50dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // will remove all current infobars from the container, add the infobars from
51dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // |contents|, and show them all.  |contents| may be NULL.
524a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  void ChangeTabContents(TabContents* contents);
534a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
54ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Returns the amount by which to overlap the toolbar above, and, when
55ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // |total_height| is non-NULL, set it to the height of the InfoBarContainer
56ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // (including overlap).
57ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  int GetVerticalOverlap(int* total_height);
58ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
59ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Called by the delegate when the distance between what the top infobar's
60ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // "unspoofable" arrow would point to and the top infobar itself changes.
61ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // This enables the top infobar to show a longer arrow (e.g. because of a
62ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // visible bookmark bar) or shorter (e.g. due to being in a popup window) if
63ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // desired.
64ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  //
65ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // IMPORTANT: This MUST NOT result in a call back to
66ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Delegate::InfoBarContainerStateChanged() unless it causes an actual
67ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // change, lest we infinitely recurse.
68ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void SetMaxTopArrowHeight(int height);
69ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
70ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Called when a contained infobar has animated or by some other means changed
71ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // its height, or when it stops animating.  The container is expected to do
72ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // anything necessary to respond, e.g. re-layout.
73ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void OnInfoBarStateChanged(bool is_animating);
74ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
75ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Removes the specified InfoBarDelegate from the selected TabContents.  This
76dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // will notify us back and cause us to close the InfoBar.  This is called from
774a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // the InfoBar's close button handler.
784a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  void RemoveDelegate(InfoBarDelegate* delegate);
794a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
80dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // Called by |infobar| to request that it be removed from the container, as it
81dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // is about to delete itself.  At this point, |infobar| should already be
82dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // hidden.
83ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void RemoveInfoBar(InfoBar* infobar);
84dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
85ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  const Delegate* delegate() const { return delegate_; }
8672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
87ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen protected:
88ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Subclasses must call this during destruction, so that we can remove
89ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // infobars (which will call the pure virtual functions below) while the
90ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // subclass portion of |this| has not yet been destroyed.
91ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void RemoveAllInfoBarsForDestruction();
92dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
93ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // These must be implemented on each platform to e.g. adjust the visible
94ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // object hierarchy.
95ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  virtual void PlatformSpecificAddInfoBar(InfoBar* infobar) = 0;
96ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  virtual void PlatformSpecificRemoveInfoBar(InfoBar* infobar) = 0;
97ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
98ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen private:
99ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  typedef std::vector<InfoBar*> InfoBars;
1004a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
10172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // NotificationObserver:
1024a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  virtual void Observe(NotificationType type,
1034a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch                       const NotificationSource& source,
104ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                       const NotificationDetails& details) OVERRIDE;
1054a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
1064a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // Removes an InfoBar for the specified delegate, in response to a
1074a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // notification from the selected TabContents. The InfoBar's disappearance
1084a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // will be animated if |use_animation| is true.
1094a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  void RemoveInfoBar(InfoBarDelegate* delegate, bool use_animation);
1104a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
111dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // Adds |infobar| to this container and calls Show() on it.  |animate| is
112dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // passed along to infobar->Show().  Depending on the value of
113dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // |callback_status|, this calls infobar->set_container(this) either before or
114dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // after the call to Show() so that OnInfoBarAnimated() either will or won't
115dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // be called as a result.
116dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  enum CallbackStatus { NO_CALLBACK, WANT_CALLBACK };
117dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  void AddInfoBar(InfoBar* infobar,
118dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen                  bool animate,
119dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen                  CallbackStatus callback_status);
1204a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
121ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void UpdateInfoBarArrowTargetHeights();
122ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  int ArrowTargetHeightForInfoBar(size_t infobar_index) const;
123ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
1244a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  NotificationRegistrar registrar_;
1254a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  Delegate* delegate_;
1264a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  TabContents* tab_contents_;
127dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  InfoBars infobars_;
1284a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
129ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Calculated in SetMaxTopArrowHeight().
130ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  int top_arrow_target_height_;
131ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
1324a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  DISALLOW_COPY_AND_ASSIGN(InfoBarContainer);
1334a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch};
1344a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
1354a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#endif  // CHROME_BROWSER_UI_VIEWS_INFOBARS_INFOBAR_CONTAINER_H_
136