OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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. | |
4 | |
5 #ifndef CHROME_BROWSER_BACK_FORWARD_MENU_MODEL_H_ | |
6 #define CHROME_BROWSER_BACK_FORWARD_MENU_MODEL_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 | |
11 #include "app/menus/menu_model.h" | |
12 #include "base/basictypes.h" | |
13 #include "base/gtest_prod_util.h" | |
14 #include "base/string16.h" | |
15 #include "webkit/glue/window_open_disposition.h" | |
16 | |
17 class Browser; | |
18 class SkBitmap; | |
19 class TabContents; | |
20 class NavigationEntry; | |
21 | |
22 /////////////////////////////////////////////////////////////////////////////// | |
23 // | |
24 // BackForwardMenuModel | |
25 // | |
26 // Interface for the showing of the dropdown menu for the Back/Forward buttons. | |
27 // Actual implementations are platform-specific. | |
28 /////////////////////////////////////////////////////////////////////////////// | |
29 class BackForwardMenuModel : public menus::MenuModel { | |
30 public: | |
31 // These are IDs used to identify individual UI elements within the | |
32 // browser window using View::GetViewByID. | |
33 enum ModelType { | |
34 FORWARD_MENU = 1, | |
35 BACKWARD_MENU = 2 | |
36 }; | |
37 | |
38 BackForwardMenuModel(Browser* browser, ModelType model_type); | |
39 virtual ~BackForwardMenuModel() { } | |
40 | |
41 // MenuModel implementation. | |
42 virtual bool HasIcons() const; | |
43 // Returns how many items the menu should show, including history items, | |
44 // chapter-stops, separators and the Show Full History link. This function | |
45 // uses GetHistoryItemCount() and GetChapterStopCount() internally to figure | |
46 // out the total number of items to show. | |
47 virtual int GetItemCount() const; | |
48 virtual ItemType GetTypeAt(int index) const; | |
49 virtual int GetCommandIdAt(int index) const; | |
50 virtual string16 GetLabelAt(int index) const; | |
51 virtual bool IsLabelDynamicAt(int index) const; | |
52 virtual bool GetAcceleratorAt(int index, | |
53 menus::Accelerator* accelerator) const; | |
54 virtual bool IsItemCheckedAt(int index) const; | |
55 virtual int GetGroupIdAt(int index) const; | |
56 virtual bool GetIconAt(int index, SkBitmap* icon) const; | |
57 virtual menus::ButtonMenuItemModel* GetButtonMenuItemAt(int index) const; | |
58 virtual bool IsEnabledAt(int index) const; | |
59 virtual MenuModel* GetSubmenuModelAt(int index) const; | |
60 virtual void HighlightChangedTo(int index); | |
61 virtual void ActivatedAt(int index); | |
62 virtual void ActivatedAtWithDisposition(int index, int disposition); | |
63 virtual void MenuWillShow(); | |
64 | |
65 // Is the item at |index| a separator? | |
66 bool IsSeparator(int index) const; | |
67 | |
68 private: | |
69 // Allows the unit test to use its own dummy tab contents. | |
70 void set_test_tab_contents(TabContents* test_tab_contents) { | |
71 test_tab_contents_ = test_tab_contents; | |
72 } | |
73 | |
74 // Returns how many history items the menu should show. For example, if the | |
75 // navigation controller of the current tab has a current entry index of 5 and | |
76 // forward_direction_ is false (we are the back button delegate) then this | |
77 // function will return 5 (representing 0-4). If forward_direction_ is | |
78 // true (we are the forward button delegate), then this function will return | |
79 // the number of entries after 5. Note, though, that in either case it will | |
80 // not report more than kMaxHistoryItems. The number returned also does not | |
81 // include the separator line after the history items (nor the separator for | |
82 // the "Show Full History" link). | |
83 int GetHistoryItemCount() const; | |
84 | |
85 // Returns how many chapter-stop items the menu should show. For the | |
86 // definition of a chapter-stop, see GetIndexOfNextChapterStop(). The number | |
87 // returned does not include the separator lines before and after the | |
88 // chapter-stops. | |
89 int GetChapterStopCount(int history_items) const; | |
90 | |
91 // Finds the next chapter-stop in the NavigationEntryList starting from | |
92 // the index specified in |start_from| and continuing in the direction | |
93 // specified (|forward|) until either a chapter-stop is found or we reach the | |
94 // end, in which case -1 is returned. If |start_from| is out of bounds, -1 | |
95 // will also be returned. A chapter-stop is defined as the last page the user | |
96 // browsed to within the same domain. For example, if the user's homepage is | |
97 // Google and she navigates to Google pages G1, G2 and G3 before heading over | |
98 // to WikiPedia for pages W1 and W2 and then back to Google for pages G4 and | |
99 // G5 then G3, W2 and G5 are considered chapter-stops. The return value from | |
100 // this function is an index into the NavigationEntryList vector. | |
101 int GetIndexOfNextChapterStop(int start_from, bool forward) const; | |
102 | |
103 // Finds a given chapter-stop starting at the currently active entry in the | |
104 // NavigationEntryList vector advancing first forward or backward by |offset| | |
105 // (depending on the direction specified in parameter |forward|). It also | |
106 // allows you to skip chapter-stops by specifying a positive value for |skip|. | |
107 // Example: FindChapterStop(5, false, 3) starts with the currently active | |
108 // index, subtracts 5 from it and then finds the fourth chapter-stop before | |
109 // that index (skipping the first 3 it finds). | |
110 // Example: FindChapterStop(0, true, 0) is functionally equivalent to | |
111 // calling GetIndexOfNextChapterStop(GetCurrentEntryIndex(), true). | |
112 // | |
113 // NOTE: Both |offset| and |skip| must be non-negative. The return value from | |
114 // this function is an index into the NavigationEntryList vector. If |offset| | |
115 // is out of bounds or if we skip too far (run out of chapter-stops) this | |
116 // function returns -1. | |
117 int FindChapterStop(int offset, bool forward, int skip) const; | |
118 | |
119 // How many items (max) to show in the back/forward history menu dropdown. | |
120 static const int kMaxHistoryItems; | |
121 | |
122 // How many chapter-stops (max) to show in the back/forward dropdown list. | |
123 static const int kMaxChapterStops; | |
124 | |
125 // Takes a menu item index as passed in through one of the menu delegate | |
126 // functions and converts it into an index into the NavigationEntryList | |
127 // vector. |index| can point to a separator, or the | |
128 // "Show Full History" link in which case this function returns -1. | |
129 int MenuIndexToNavEntryIndex(int index) const; | |
130 | |
131 // Does the item have a command associated with it? | |
132 bool ItemHasCommand(int index) const; | |
133 | |
134 // Returns true if there is an icon for this menu item. | |
135 bool ItemHasIcon(int index) const; | |
136 | |
137 // Allow the unit test to use the "Show Full History" label. | |
138 string16 GetShowFullHistoryLabel() const; | |
139 | |
140 // Looks up a NavigationEntry by menu id. | |
141 NavigationEntry* GetNavigationEntry(int index) const; | |
142 | |
143 // Retrieves the TabContents pointer to use, which is either the one that | |
144 // the unit test sets (using SetTabContentsForUnitTest) or the one from | |
145 // the browser window. | |
146 TabContents* GetTabContents() const; | |
147 | |
148 // Build a string version of a user action on this menu, used as an | |
149 // identifier for logging user behavior. | |
150 // E.g. BuildActionName("Click", 2) returns "BackMenu_Click2". | |
151 // An index of -1 means no index. | |
152 std::string BuildActionName(const std::string& name, int index) const; | |
153 | |
154 Browser* browser_; | |
155 | |
156 // The unit tests will provide their own TabContents to use. | |
157 TabContents* test_tab_contents_; | |
158 | |
159 // Represents whether this is the delegate for the forward button or the | |
160 // back button. | |
161 ModelType model_type_; | |
162 | |
163 friend class BackFwdMenuModelTest; | |
164 FRIEND_TEST_ALL_PREFIXES(BackFwdMenuModelTest, BasicCase); | |
165 FRIEND_TEST_ALL_PREFIXES(BackFwdMenuModelTest, MaxItemsTest); | |
166 FRIEND_TEST_ALL_PREFIXES(BackFwdMenuModelTest, ChapterStops); | |
167 | |
168 DISALLOW_COPY_AND_ASSIGN(BackForwardMenuModel); | |
169 }; | |
170 | |
171 #endif // CHROME_BROWSER_BACK_FORWARD_MENU_MODEL_H_ | |
OLD | NEW |