blob: fb17822073979d3d2050a0cf35bfbb758b9b6721 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commit09911bf2008-07-26 23:55:294
5#ifndef CHROME_BROWSER_BACK_FORWARD_MENU_MODEL_H__
6#define CHROME_BROWSER_BACK_FORWARD_MENU_MODEL_H__
7
8#include "chrome/views/menu.h"
9
10class Browser;
11class TabContents;
12class SkBitmap;
13class NavigationEntry;
14
15///////////////////////////////////////////////////////////////////////////////
16//
17// BackForwardMenuModel
18//
19// Implements the showing of the dropdown menu for the Back/Forward buttons.
20///////////////////////////////////////////////////////////////////////////////
21class BackForwardMenuModel : public Menu::Delegate {
22 public:
23 // These are IDs used to identify individual UI elements within the
24 // browser window using View::GetViewByID.
25 typedef enum ModelType {
26 FORWARD_MENU_DELEGATE = 1,
27 BACKWARD_MENU_DELEGATE = 2
28 };
29
30 BackForwardMenuModel(Browser* browser, ModelType model_type);
31 virtual ~BackForwardMenuModel();
32
33 // Menu::Delegate
34 virtual std::wstring GetLabel(int menu_id) const;
35 virtual const SkBitmap& GetIcon(int menu_id) const;
36 virtual bool SupportsCommand(int menu_id) const;
37 virtual bool IsCommandEnabled(int menu_id) const;
38 virtual bool IsItemSeparator(int menu_id) const;
39 virtual bool HasIcon(int menu_id) const;
40 virtual void ExecuteCommand(int menu_id);
41 virtual void MenuWillShow();
42 // Returns how many items the menu should show, including history items,
43 // chapter-stops, separators and the Show Full History link. This function
44 // uses GetHistoryItemCount() and GetChapterStopCount() internally to figure
45 // out the total number of items to show.
46 virtual int GetItemCount() const;
47
48 // Returns how many history items the menu should show. For example, if the
49 // navigation controller of the current tab has a current entry index of 5 and
50 // forward_direction_ is false (we are the back button delegate) then this
51 // function will return 5 (representing 0-4). If forward_direction_ is
52 // true (we are the forward button delegate), then this function will return
53 // the number of entries after 5. Note, though, that in either case it will
54 // not report more than kMaxHistoryItems. The number returned also does not
55 // include the separator line after the history items (nor the separator for
56 // the "Show Full History" link).
57 int GetHistoryItemCount() const;
58
59 // Returns how many chapter-stop items the menu should show. For the
60 // definition of a chapter-stop, see GetIndexOfNextChapterStop(). The number
61 // returned does not include the separator lines before and after the
62 // chapter-stops.
63 int GetChapterStopCount(int history_items) const;
64
65 // Finds the next chapter-stop in the NavigationEntryList starting from
66 // the index specified in |start_from| and continuing in the direction
67 // specified (|forward|) until either a chapter-stop is found or we reach the
68 // end, in which case -1 is returned. If |start_from| is out of bounds, -1
69 // will also be returned. A chapter-stop is defined as the last page the user
70 // browsed to within the same domain. For example, if the user's homepage is
71 // Google and she navigates to Google pages G1, G2 and G3 before heading over
72 // to WikiPedia for pages W1 and W2 and then back to Google for pages G4 and
73 // G5 then G3, W2 and G5 are considered chapter-stops. The return value from
74 // this function is an index into the NavigationEntryList vector.
75 int GetIndexOfNextChapterStop(int start_from, bool forward) const;
76
77 // Finds a given chapter-stop starting at the currently active entry in the
78 // NavigationEntryList vector advancing first forward or backward by |offset|
79 // (depending on the direction specified in parameter |forward|). It also
80 // allows you to skip chapter-stops by specifying a positive value for |skip|.
81 // Example: FindChapterStop(5, false, 3) starts with the currently active
82 // index, subtracts 5 from it and then finds the fourth chapter-stop before
83 // that index (skipping the first 3 it finds).
84 // Example: FindChapterStop(0, true, 0) is functionally equivalent to
85 // calling GetIndexOfNextChapterStop(GetCurrentEntryIndex(), true).
86 //
87 // NOTE: Both |offset| and |skip| must be non-negative. The return value from
88 // this function is an index into the NavigationEntryList vector. If |offset|
89 // is out of bounds or if we skip too far (run out of chapter-stops) this
90 // function returns -1.
91 int FindChapterStop(int offset, bool forward, int skip) const;
92
93 // Allows the unit test to use its own dummy tab contents.
94 void set_test_tab_contents(TabContents* test_tab_contents) {
95 test_tab_contents_ = test_tab_contents;
96 }
97
98 // Allow the unit test to use the "Show Full History" label.
99 std::wstring GetShowFullHistoryLabel() const;
100
101 // Retrieves the TabContents pointer to use, which is either the one that
102 // the unit test sets (using SetTabContentsForUnitTest) or the one from
103 // the browser window.
104 TabContents* GetTabContents() const;
105
106 // How many items (max) to show in the back/forward history menu dropdown.
107 static const int kMaxHistoryItems;
108
109 // How many chapter-stops (max) to show in the back/forward dropdown list.
110 static const int kMaxChapterStops;
111
112 private:
113 // Converts a menu item id, as passed in through one of the menu delegate
114 // functions and converts it into an absolute index into the
115 // NavigationEntryList vector. |menu_id| can point to a separator, or the
116 // "Show Full History" link in which case this function returns -1.
117 int MenuIdToNavEntryIndex(int menu_id) const;
118
119 // Looks up a NavigationEntry by menu id.
120 NavigationEntry* GetNavigationEntry(int menu_id) const;
121
122 // Build a string version of a user action on this menu, used as an
123 // identifier for logging user behavior.
124 // E.g. BuildActionName("Click", 2) returns "BackMenu_Click2".
125 // An index of -1 means no index.
126 std::wstring BuildActionName(const std::wstring& name, int index) const;
127
128 Browser* browser_;
129
130 // The unit tests will provide their own TabContents to use.
131 TabContents* test_tab_contents_;
132
133 // Represents whether this is the delegate for the forward button or the
134 // back button.
135 ModelType model_type_;
136
137 DISALLOW_EVIL_CONSTRUCTORS(BackForwardMenuModel);
138};
139
140#endif // CHROME_BROWSER_BACK_FORWARD_MENU_MODEL_H__
141
license.botbf09a502008-08-24 00:55:55142