Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(295)

Side by Side Diff: chrome/browser/infobars/infobar.h

Issue 22694006: Infobar system refactor. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_INFOBARS_INFOBAR_H_ 5 #ifndef CHROME_BROWSER_INFOBARS_INFOBAR_H_
6 #define CHROME_BROWSER_INFOBARS_INFOBAR_H_ 6 #define CHROME_BROWSER_INFOBARS_INFOBAR_H_
7 7
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/basictypes.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/compiler_specific.h"
12 #include "build/build_config.h"
13 #include "chrome/browser/infobars/infobar_delegate.h" 11 #include "chrome/browser/infobars/infobar_delegate.h"
14 #include "third_party/skia/include/core/SkColor.h" 12 #include "third_party/skia/include/core/SkColor.h"
15 #include "ui/gfx/animation/animation_delegate.h" 13 #include "ui/gfx/animation/animation_delegate.h"
16 #include "ui/gfx/animation/slide_animation.h" 14 #include "ui/gfx/animation/slide_animation.h"
17 #include "ui/gfx/size.h" 15 #include "ui/gfx/size.h"
18 16
19 // TODO(sail): These functions should be static methods in the InfoBar class
20 // below once all platforms use that class.
21 SkColor GetInfoBarTopColor(InfoBarDelegate::Type infobar_type);
22 SkColor GetInfoBarBottomColor(InfoBarDelegate::Type infobar_type);
23
24 // TODO(pkasting): Same with these notification-related typedefs.
25 typedef InfoBarDelegate InfoBarAddedDetails;
26 typedef std::pair<InfoBarDelegate*, bool> InfoBarRemovedDetails;
27 typedef std::pair<InfoBarDelegate*, InfoBarDelegate*> InfoBarReplacedDetails;
28
29 class InfoBarContainer; 17 class InfoBarContainer;
30 class InfoBarService; 18 class InfoBarService;
31 19
20 // InfoBar is a cross-platform base class for an infobar "view" (in the MVC
21 // sense), which owns a corresponding InfoBarDelegate "model". Typically,
22 // a caller will call XYZInfoBarDelegate::Create() and pass in the
23 // InfoBarService for the relevant tab. This will create an XYZInfoBarDelegate,
24 // create a platform-specific subclass of InfoBar to own it, and then call
25 // InfoBarService::AddInfoBar() to give it ownership of the infobar.
26 // During its life, the InfoBar may be shown and hidden as the owning tab is
27 // switched between the foreground and background. Eventually, InfoBarService
28 // will instruct the InfoBar to close itself. At this point, the InfoBar will
29 // optionally animate closed; once it's no longer visible, it deletes itself,
30 // destroying the InfoBarDelegate in the process.
31 //
32 // Thus, InfoBarDelegate and InfoBar implementations can assume they share
33 // lifetimes, and not NULL-check each other; but if one needs to reach back into
34 // the owning InfoBarService, it must check whether that's still possible.
32 class InfoBar : public gfx::AnimationDelegate { 35 class InfoBar : public gfx::AnimationDelegate {
33 public: 36 public:
34 InfoBar(InfoBarService* owner, InfoBarDelegate* delegate); 37 // These are the types passed as Details for infobar-related notifications.
38 typedef InfoBar AddedDetails;
39 typedef std::pair<InfoBar*, bool> RemovedDetails;
40 typedef std::pair<InfoBar*, InfoBar*> ReplacedDetails;
41
42 explicit InfoBar(scoped_ptr<InfoBarDelegate> delegate);
35 virtual ~InfoBar(); 43 virtual ~InfoBar();
36 44
37 // Platforms must define these. 45 // Platforms must define these.
38 static const int kDefaultBarTargetHeight; 46 static const int kDefaultBarTargetHeight;
39 static const int kSeparatorLineHeight; 47 static const int kSeparatorLineHeight;
40 static const int kDefaultArrowTargetHeight; 48 static const int kDefaultArrowTargetHeight;
41 static const int kMaximumArrowTargetHeight; 49 static const int kMaximumArrowTargetHeight;
42 // The half-width (see comments on |arrow_half_width_| below) scales to its 50 // The half-width (see comments on |arrow_half_width_| below) scales to its
43 // default and maximum values proportionally to how the height scales to its. 51 // default and maximum values proportionally to how the height scales to its.
44 static const int kDefaultArrowTargetHalfWidth; 52 static const int kDefaultArrowTargetHalfWidth;
45 static const int kMaximumArrowTargetHalfWidth; 53 static const int kMaximumArrowTargetHalfWidth;
46 54
47 InfoBarDelegate* delegate() { return delegate_; } 55 static SkColor GetTopColor(InfoBarDelegate::Type infobar_type);
56 static SkColor GetBottomColor(InfoBarDelegate::Type infobar_type);
57
58 InfoBarService* owner() { return owner_; }
59 InfoBarDelegate* delegate() { return delegate_.get(); }
60 const InfoBarDelegate* delegate() const { return delegate_.get(); }
48 void set_container(InfoBarContainer* container) { container_ = container; } 61 void set_container(InfoBarContainer* container) { container_ = container; }
49 62
63 // Sets |owner_|. This also calls StoreActiveEntryUniqueID() on |delegate_|.
64 // This must only be called once as there's no way to extract an infobar from
65 // its owner without deleting it, for reparenting in another tab.
66 void SetOwner(InfoBarService* owner);
67
50 // Makes the infobar visible. If |animate| is true, the infobar is then 68 // Makes the infobar visible. If |animate| is true, the infobar is then
51 // animated to full size. 69 // animated to full size.
52 void Show(bool animate); 70 void Show(bool animate);
53 71
54 // Makes the infobar hidden. If |animate| is true, the infobar is first 72 // Makes the infobar hidden. If |animate| is false, the infobar is
55 // animated to zero size. Once the infobar is hidden, it is removed from its 73 // immediately removed from the container, and, if now unowned, deleted. If
56 // container (triggering its deletion), and its delegate is closed. 74 // |animate| is true, the infobar is animated to zero size, ultimately
75 // triggering a call to AnimationEnded().
57 void Hide(bool animate); 76 void Hide(bool animate);
58 77
59 // Changes the target height of the arrow portion of the infobar. This has no 78 // Changes the target height of the arrow portion of the infobar. This has no
60 // effect once the infobar is animating closed. 79 // effect once the infobar is animating closed.
61 void SetArrowTargetHeight(int height); 80 void SetArrowTargetHeight(int height);
62 81
63 // Notifies the infobar that it is no longer owned and should close its 82 // Notifies the infobar that it is no longer owned and should delete itself
64 // delegate once it is invisible. 83 // once it is invisible.
65 void CloseSoon(); 84 void CloseSoon();
66 85
86 // Forwards a close request to our owner. This is a no-op if we're already
87 // unowned.
88 void RemoveSelf();
89
67 // Changes the target height of the main ("bar") portion of the infobar. 90 // Changes the target height of the main ("bar") portion of the infobar.
68 void SetBarTargetHeight(int height); 91 void SetBarTargetHeight(int height);
69 92
70 const gfx::SlideAnimation& animation() const { return animation_; } 93 const gfx::SlideAnimation& animation() const { return animation_; }
71 int arrow_height() const { return arrow_height_; } 94 int arrow_height() const { return arrow_height_; }
72 int arrow_target_height() const { return arrow_target_height_; } 95 int arrow_target_height() const { return arrow_target_height_; }
73 int arrow_half_width() const { return arrow_half_width_; } 96 int arrow_half_width() const { return arrow_half_width_; }
74 int total_height() const { return arrow_height_ + bar_height_; } 97 int total_height() const { return arrow_height_ + bar_height_; }
75 98
76 protected: 99 protected:
77 // gfx::AnimationDelegate: 100 // gfx::AnimationDelegate:
78 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE; 101 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
79 102
80 // Forwards a close request to our owner.
81 // NOTE: Subclasses should not call this if we're already unowned.
82 void RemoveSelf();
83
84 // Given a control with size |prefsize|, returns the centered y position 103 // Given a control with size |prefsize|, returns the centered y position
85 // within us, taking into account animation so the control "slides in" (or 104 // within us, taking into account animation so the control "slides in" (or
86 // out) as we animate open and closed. 105 // out) as we animate open and closed.
87 int OffsetY(const gfx::Size& prefsize) const; 106 int OffsetY(const gfx::Size& prefsize) const;
88 107
89 InfoBarService* owner() const { return owner_; }
90 const InfoBarContainer* container() const { return container_; } 108 const InfoBarContainer* container() const { return container_; }
91 InfoBarContainer* container() { return container_; } 109 InfoBarContainer* container() { return container_; }
92 gfx::SlideAnimation* animation() { return &animation_; } 110 gfx::SlideAnimation* animation() { return &animation_; }
93 int bar_height() const { return bar_height_; } 111 int bar_height() const { return bar_height_; }
94 int bar_target_height() const { return bar_target_height_; } 112 int bar_target_height() const { return bar_target_height_; }
95 113
96 // Platforms may optionally override these if they need to do work during 114 // Platforms may optionally override these if they need to do work during
97 // processing of the given calls. 115 // processing of the given calls.
116 virtual void PlatformSpecificSetOwner() {}
98 virtual void PlatformSpecificShow(bool animate) {} 117 virtual void PlatformSpecificShow(bool animate) {}
99 virtual void PlatformSpecificHide(bool animate) {} 118 virtual void PlatformSpecificHide(bool animate) {}
100 virtual void PlatformSpecificOnCloseSoon() {} 119 virtual void PlatformSpecificOnCloseSoon() {}
101 virtual void PlatformSpecificOnHeightsRecalculated() {} 120 virtual void PlatformSpecificOnHeightsRecalculated() {}
102 121
103 private: 122 private:
104 // gfx::AnimationDelegate: 123 // gfx::AnimationDelegate:
105 virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE; 124 virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE;
106 125
107 // Finds the new desired arrow and bar heights, and if they differ from the 126 // Finds the new desired arrow and bar heights, and if they differ from the
108 // current ones, calls PlatformSpecificOnHeightRecalculated(). Informs our 127 // current ones, calls PlatformSpecificOnHeightRecalculated(). Informs our
109 // container our state has changed if either the heights have changed or 128 // container our state has changed if either the heights have changed or
110 // |force_notify| is set. 129 // |force_notify| is set.
111 void RecalculateHeights(bool force_notify); 130 void RecalculateHeights(bool force_notify);
112 131
113 // Checks whether we're closed. If so, notifies the container that it should 132 // Checks whether the infobar is unowned and done with all animations. If so,
114 // remove us (which will cause the platform-specific code to asynchronously 133 // notifies the container that it should remove this infobar, and deletes
115 // delete us) and closes the delegate. 134 // itself.
116 void MaybeDelete(); 135 void MaybeDelete();
117 136
118 InfoBarService* owner_; 137 InfoBarService* owner_;
119 InfoBarDelegate* delegate_; 138 scoped_ptr<InfoBarDelegate> delegate_;
120 InfoBarContainer* container_; 139 InfoBarContainer* container_;
121 gfx::SlideAnimation animation_; 140 gfx::SlideAnimation animation_;
122 141
123 // The current and target heights of the arrow and bar portions, and half the 142 // The current and target heights of the arrow and bar portions, and half the
124 // current arrow width. (It's easier to work in half-widths as we draw the 143 // current arrow width. (It's easier to work in half-widths as we draw the
125 // arrow as two halves on either side of a center point.) 144 // arrow as two halves on either side of a center point.)
126 int arrow_height_; // Includes both fill and top stroke. 145 int arrow_height_; // Includes both fill and top stroke.
127 int arrow_target_height_; 146 int arrow_target_height_;
128 int arrow_half_width_; // Includes only fill. 147 int arrow_half_width_; // Includes only fill.
129 int bar_height_; // Includes both fill and bottom separator. 148 int bar_height_; // Includes both fill and bottom separator.
130 int bar_target_height_; 149 int bar_target_height_;
131 150
132 DISALLOW_COPY_AND_ASSIGN(InfoBar); 151 DISALLOW_COPY_AND_ASSIGN(InfoBar);
133 }; 152 };
134 153
135 #endif // CHROME_BROWSER_INFOBARS_INFOBAR_H_ 154 #endif // CHROME_BROWSER_INFOBARS_INFOBAR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698