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_
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
;
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
{
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
);
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.
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
,
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
,
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
,
136 ChangeReason change_reason
) override
;
138 // Overridden from TabRestoreServiceObserver:
139 void TabRestoreServiceChanged(TabRestoreService
* service
) override
;
140 void TabRestoreServiceDestroyed(TabRestoreService
* service
) override
;
142 // Overridden from views::DesktopWindowTreeHostObserverX11:
143 void OnWindowMapped(unsigned long xid
) override
;
144 void OnWindowUnmapped(unsigned long xid
) override
;
146 CHROMEG_CALLBACK_1(GlobalMenuBarX11
, void, OnItemActivated
, DbusmenuMenuitem
*,
148 CHROMEG_CALLBACK_1(GlobalMenuBarX11
, void, OnHistoryItemActivated
,
149 DbusmenuMenuitem
*, unsigned int);
150 CHROMEG_CALLBACK_0(GlobalMenuBarX11
, void, OnHistoryMenuAboutToShow
,
152 CHROMEG_CALLBACK_1(GlobalMenuBarX11
, void, OnProfileItemActivated
,
153 DbusmenuMenuitem
*, unsigned int);
154 CHROMEG_CALLBACK_1(GlobalMenuBarX11
, void, OnEditProfileItemActivated
,
155 DbusmenuMenuitem
*, unsigned int);
156 CHROMEG_CALLBACK_1(GlobalMenuBarX11
, void, OnCreateProfileItemActivated
,
157 DbusmenuMenuitem
*, unsigned int);
161 BrowserView
* browser_view_
;
162 BrowserDesktopWindowTreeHostX11
* host_
;
164 // Maps command ids to DbusmenuMenuitems so we can modify their
165 // enabled/checked state in response to state change notifications.
166 CommandIDMenuItemMap id_to_menu_item_
;
168 DbusmenuServer
* server_
;
169 DbusmenuMenuitem
* root_item_
;
170 DbusmenuMenuitem
* history_menu_
;
171 DbusmenuMenuitem
* profiles_menu_
;
173 // Tracks value of the kShowBookmarkBar preference.
174 PrefChangeRegistrar pref_change_registrar_
;
176 scoped_refptr
<history::TopSites
> top_sites_
;
178 TabRestoreService
* tab_restore_service_
; // weak
180 scoped_ptr
<AvatarMenu
> avatar_menu_
;
182 ScopedObserver
<history::TopSites
, history::TopSitesObserver
> scoped_observer_
;
184 // For callbacks may be run after destruction.
185 base::WeakPtrFactory
<GlobalMenuBarX11
> weak_ptr_factory_
;
187 DISALLOW_COPY_AND_ASSIGN(GlobalMenuBarX11
);
190 #endif // CHROME_BROWSER_UI_VIEWS_FRAME_GLOBAL_MENU_BAR_X11_H_