Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / views / frame / global_menu_bar_x11.h
blob9dbe90cb80c059e6336c82dd8931d05af5629ca0
1 // Copyright 2013 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.
5 #ifndef CHROME_BROWSER_UI_VIEWS_FRAME_GLOBAL_MENU_BAR_X11_H_
6 #define CHROME_BROWSER_UI_VIEWS_FRAME_GLOBAL_MENU_BAR_X11_H_
8 #include <map>
9 #include <string>
11 #include "base/compiler_specific.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/prefs/pref_change_registrar.h"
14 #include "base/scoped_observer.h"
15 #include "chrome/browser/command_observer.h"
16 #include "chrome/browser/profiles/avatar_menu.h"
17 #include "chrome/browser/profiles/avatar_menu_observer.h"
18 #include "chrome/browser/sessions/tab_restore_service.h"
19 #include "chrome/browser/sessions/tab_restore_service_observer.h"
20 #include "chrome/browser/ui/browser_list_observer.h"
21 #include "components/history/core/browser/history_types.h"
22 #include "components/history/core/browser/top_sites_observer.h"
23 #include "ui/base/glib/glib_signal.h"
24 #include "ui/views/widget/desktop_aura/desktop_window_tree_host_observer_x11.h"
26 typedef struct _DbusmenuMenuitem DbusmenuMenuitem;
27 typedef struct _DbusmenuServer DbusmenuServer;
29 namespace history {
30 class TopSites;
33 namespace ui {
34 class Accelerator;
37 class Browser;
38 class BrowserView;
39 class Profile;
41 class BrowserDesktopWindowTreeHostX11;
42 struct GlobalMenuBarCommand;
44 // Controls the Mac style menu bar on Unity.
46 // Unity has an Apple-like menu bar at the top of the screen that changes
47 // depending on the active window. In the GTK port, we had a hidden GtkMenuBar
48 // object in each GtkWindow which existed only to be scrapped by the
49 // libdbusmenu-gtk code. Since we don't have GtkWindows anymore, we need to
50 // interface directly with the lower level libdbusmenu-glib, which we
51 // opportunistically dlopen() since not everyone is running Ubuntu.
52 class GlobalMenuBarX11 : public AvatarMenuObserver,
53 public chrome::BrowserListObserver,
54 public CommandObserver,
55 public history::TopSitesObserver,
56 public TabRestoreServiceObserver,
57 public views::DesktopWindowTreeHostObserverX11 {
58 public:
59 GlobalMenuBarX11(BrowserView* browser_view,
60 BrowserDesktopWindowTreeHostX11* host);
61 ~GlobalMenuBarX11() override;
63 // Creates the object path for DbusemenuServer which is attached to |xid|.
64 static std::string GetPathForWindow(unsigned long xid);
66 private:
67 struct HistoryItem;
68 typedef std::map<int, DbusmenuMenuitem*> CommandIDMenuItemMap;
70 // Builds a separator.
71 DbusmenuMenuitem* BuildSeparator();
73 // Creates an individual menu item from a title and command, and subscribes
74 // to the activation signal.
75 DbusmenuMenuitem* BuildMenuItem(const std::string& label, int tag_id);
77 // Creates a DbusmenuServer, and attaches all the menu items.
78 void InitServer(unsigned long xid);
80 // Stops listening to enable state changed events.
81 void Disable();
83 // Creates a whole menu defined with |commands| and titled with the string
84 // |menu_str_id|. Then appends it to |parent|.
85 DbusmenuMenuitem* BuildStaticMenu(DbusmenuMenuitem* parent,
86 int menu_str_id,
87 GlobalMenuBarCommand* commands);
89 // Sets the accelerator for |item|.
90 void RegisterAccelerator(DbusmenuMenuitem* item,
91 const ui::Accelerator& accelerator);
93 // Creates a HistoryItem from the data in |entry|.
94 HistoryItem* HistoryItemForTab(const TabRestoreService::Tab& entry);
96 // Creates a menu item form |item| and inserts it in |menu| at |index|.
97 void AddHistoryItemToMenu(HistoryItem* item,
98 DbusmenuMenuitem* menu,
99 int tag,
100 int index);
102 // Sends a message off to History for data.
103 void GetTopSitesData();
105 // Callback to receive data requested from GetTopSitesData().
106 void OnTopSitesReceived(const history::MostVisitedURLList& visited_list);
108 // Updates the visibility of the bookmark bar on pref changes.
109 void OnBookmarkBarVisibilityChanged();
111 void RebuildProfilesMenu();
113 // Find the first index of the item in |menu| with the tag |tag_id|.
114 int GetIndexOfMenuItemWithTag(DbusmenuMenuitem* menu, int tag_id);
116 // This will remove all menu items in |menu| with |tag| as their tag. This
117 // clears state about HistoryItems* that we keep to prevent that data from
118 // going stale. That's why this method recurses into its child menus.
119 void ClearMenuSection(DbusmenuMenuitem* menu, int tag_id);
121 // Deleter function for HistoryItem implementation detail.
122 static void DeleteHistoryItem(void* void_item);
124 // Overridden from AvatarMenuObserver:
125 void OnAvatarMenuChanged(AvatarMenu* avatar_menu) override;
127 // Overridden from chrome::BrowserListObserver:
128 void OnBrowserSetLastActive(Browser* browser) override;
130 // Overridden from CommandObserver:
131 void EnabledStateChangedForCommand(int id, bool enabled) override;
133 // Overridden from history::TopSitesObserver:
134 void TopSitesLoaded(history::TopSites* top_sites) override;
135 void TopSitesChanged(history::TopSites* top_sites) override;
137 // Overridden from TabRestoreServiceObserver:
138 void TabRestoreServiceChanged(TabRestoreService* service) override;
139 void TabRestoreServiceDestroyed(TabRestoreService* service) override;
141 // Overridden from views::DesktopWindowTreeHostObserverX11:
142 void OnWindowMapped(unsigned long xid) override;
143 void OnWindowUnmapped(unsigned long xid) override;
145 CHROMEG_CALLBACK_1(GlobalMenuBarX11, void, OnItemActivated, DbusmenuMenuitem*,
146 unsigned int);
147 CHROMEG_CALLBACK_1(GlobalMenuBarX11, void, OnHistoryItemActivated,
148 DbusmenuMenuitem*, unsigned int);
149 CHROMEG_CALLBACK_0(GlobalMenuBarX11, void, OnHistoryMenuAboutToShow,
150 DbusmenuMenuitem*);
151 CHROMEG_CALLBACK_1(GlobalMenuBarX11, void, OnProfileItemActivated,
152 DbusmenuMenuitem*, unsigned int);
153 CHROMEG_CALLBACK_1(GlobalMenuBarX11, void, OnEditProfileItemActivated,
154 DbusmenuMenuitem*, unsigned int);
155 CHROMEG_CALLBACK_1(GlobalMenuBarX11, void, OnCreateProfileItemActivated,
156 DbusmenuMenuitem*, unsigned int);
158 Browser* browser_;
159 Profile* profile_;
160 BrowserView* browser_view_;
161 BrowserDesktopWindowTreeHostX11* host_;
163 // Maps command ids to DbusmenuMenuitems so we can modify their
164 // enabled/checked state in response to state change notifications.
165 CommandIDMenuItemMap id_to_menu_item_;
167 DbusmenuServer* server_;
168 DbusmenuMenuitem* root_item_;
169 DbusmenuMenuitem* history_menu_;
170 DbusmenuMenuitem* profiles_menu_;
172 // Tracks value of the kShowBookmarkBar preference.
173 PrefChangeRegistrar pref_change_registrar_;
175 scoped_refptr<history::TopSites> top_sites_;
177 TabRestoreService* tab_restore_service_; // weak
179 scoped_ptr<AvatarMenu> avatar_menu_;
181 ScopedObserver<history::TopSites, history::TopSitesObserver> scoped_observer_;
183 // For callbacks may be run after destruction.
184 base::WeakPtrFactory<GlobalMenuBarX11> weak_ptr_factory_;
186 DISALLOW_COPY_AND_ASSIGN(GlobalMenuBarX11);
189 #endif // CHROME_BROWSER_UI_VIEWS_FRAME_GLOBAL_MENU_BAR_X11_H_