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