recent_tabs_sub_menu_model.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
1// Copyright 2012 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_UI_TOOLBAR_RECENT_TABS_SUB_MENU_MODEL_H_
6#define CHROME_BROWSER_UI_TOOLBAR_RECENT_TABS_SUB_MENU_MODEL_H_
7
8#include <set>
9
10#include "base/memory/weak_ptr.h"
11#include "chrome/browser/favicon/favicon_service.h"
12#include "chrome/browser/sessions/tab_restore_service.h"
13#include "chrome/browser/sync/glue/synced_session.h"
14#include "chrome/common/cancelable_task_tracker.h"
15#include "ui/base/accelerators/accelerator.h"
16#include "ui/base/models/simple_menu_model.h"
17
18class Browser;
19struct SessionTab;
20
21namespace browser_sync {
22class SessionModelAssociator;
23}
24
25namespace gfx {
26class Image;
27}
28
29namespace ui {
30class AcceleratorProvider;
31}
32
33// A menu model that builds the contents of "Recent tabs" submenu, which include
34// the last closed tab and opened tabs of other devices.
35class RecentTabsSubMenuModel : public ui::SimpleMenuModel,
36                               public ui::SimpleMenuModel::Delegate {
37 public:
38  // If |associator| is NULL, default associator for |browser|'s profile will
39  // be used.  Testing may require a specific |associator|.
40  RecentTabsSubMenuModel(ui::AcceleratorProvider* accelerator_provider,
41                         Browser* browser,
42                         browser_sync::SessionModelAssociator* associator);
43  virtual ~RecentTabsSubMenuModel();
44
45  // Overridden from ui::SimpleMenuModel::Delegate:
46  virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
47  virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
48  virtual bool GetAcceleratorForCommandId(
49      int command_id,
50      ui::Accelerator* accelerator) OVERRIDE;
51  virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
52
53  int GetMaxWidthForItemAtIndex(int item_index) const;
54
55  // Command Id for disabled recently closed header menu item to which we want
56  // to append the accelerator string.
57  static const int kRecentlyClosedHeaderCommandId;
58
59  // Command Id for disabled menu items, e.g. device section header,
60  // "No tabs from other devices", etc.
61  static const int kOtherDeviceHeaderCommandId;
62
63 private:
64  struct TabNavigationItem;
65  typedef std::vector<TabNavigationItem> TabNavigationItems;
66
67  typedef std::vector<SessionID::id_type> WindowItems;
68
69  // Build the menu items by populating the model.
70  void Build();
71  void BuildRecentTabs();
72  void BuildDevices();
73  void BuildLocalTabItem(int seesion_id,
74                         const string16& title,
75                         const GURL& url);
76  void BuildForeignTabItem(const std::string& session_tag,
77                           const SessionTab& tab);
78  void BuildWindowItem(const SessionID::id_type& window_id, int num_tabs);
79  void AddDeviceFavicon(int index_in_menu,
80                        browser_sync::SyncedSession::DeviceType device_type);
81  void AddTabFavicon(int model_index, int command_id, const GURL& url);
82  void OnFaviconDataAvailable(int command_id,
83                              const history::FaviconImageResult& image_result);
84  browser_sync::SessionModelAssociator* GetModelAssociator();
85
86  Browser* browser_;  // Weak.
87
88  browser_sync::SessionModelAssociator* associator_;  // Weak.
89
90  // Accelerator for reopening last closed tab.
91  ui::Accelerator reopen_closed_tab_accelerator_;
92
93  // Navigation items for other devices and recent tabs. The |command_id| for
94  // these is set to kFirstTabCommandId plus the index into the vector. Upon
95  // invocation of the menu, the navigation information is retrieved from
96  // |tab_navigation_items_| and used to navigate to the item specified.
97  TabNavigationItems tab_navigation_items_;
98
99  // Window items for recently closed windows. The |command_id| for
100  // these is set to kFirstWindowCommandId plus the index into the vector. Upon
101  // invocation of the menu, information is retrieved from |window_items_|
102  // and used to create the specified window.
103  WindowItems window_items_;
104
105  gfx::Image default_favicon_;
106
107  CancelableTaskTracker cancelable_task_tracker_;
108
109  base::WeakPtrFactory<RecentTabsSubMenuModel> weak_ptr_factory_;
110
111  DISALLOW_COPY_AND_ASSIGN(RecentTabsSubMenuModel);
112};
113
114#endif  // CHROME_BROWSER_UI_TOOLBAR_RECENT_TABS_SUB_MENU_MODEL_H_
115