Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / frame / global_menu_bar_x11.h
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.
4
5 #ifndef CHROME_BROWSER_UI_VIEWS_FRAME_GLOBAL_MENU_BAR_X11_H_
6 #define CHROME_BROWSER_UI_VIEWS_FRAME_GLOBAL_MENU_BAR_X11_H_
7
8 #include <map>
9 #include <string>
10
11 #include "base/compiler_specific.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/prefs/pref_change_registrar.h"
14 #include "chrome/browser/command_observer.h"
15 #include "chrome/browser/profiles/avatar_menu.h"
16 #include "chrome/browser/profiles/avatar_menu_observer.h"
17 #include "chrome/browser/sessions/tab_restore_service.h"
18 #include "chrome/browser/sessions/tab_restore_service_observer.h"
19 #include "chrome/browser/ui/browser_list_observer.h"
20 #include "components/history/core/browser/history_types.h"
21 #include "content/public/browser/notification_observer.h"
22 #include "content/public/browser/notification_registrar.h"
23 #include "ui/base/glib/glib_signal.h"
24 #include "ui/views/widget/desktop_aura/desktop_window_tree_host_observer_x11.h"
25
26 typedef struct _DbusmenuMenuitem DbusmenuMenuitem;
27 typedef struct _DbusmenuServer   DbusmenuServer;
28
29 namespace history {
30 class TopSites;
31 }
32
33 namespace ui {
34 class Accelerator;
35 }
36
37 class Browser;
38 class BrowserView;
39 class Profile;
40
41 class BrowserDesktopWindowTreeHostX11;
42 struct GlobalMenuBarCommand;
43
44 // Controls the Mac style menu bar on Unity.
45 //
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 content::NotificationObserver,
56                          public TabRestoreServiceObserver,
57                          public views::DesktopWindowTreeHostObserverX11 {
58  public:
59   GlobalMenuBarX11(BrowserView* browser_view,
60                    BrowserDesktopWindowTreeHostX11* host);
61   virtual ~GlobalMenuBarX11();
62
63   // Creates the object path for DbusemenuServer which is attached to |xid|.
64   static std::string GetPathForWindow(unsigned long xid);
65
66  private:
67   struct HistoryItem;
68   typedef std::map<int, DbusmenuMenuitem*> CommandIDMenuItemMap;
69
70   // Builds a separator.
71   DbusmenuMenuitem* BuildSeparator();
72
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);
76
77   // Creates a DbusmenuServer, and attaches all the menu items.
78   void InitServer(unsigned long xid);
79
80   // Stops listening to enable state changed events.
81   void Disable();
82
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);
88
89   // Sets the accelerator for |item|.
90   void RegisterAccelerator(DbusmenuMenuitem* item,
91                            const ui::Accelerator& accelerator);
92
93   // Creates a HistoryItem from the data in |entry|.
94   HistoryItem* HistoryItemForTab(const TabRestoreService::Tab& entry);
95
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);
101
102   // Sends a message off to History for data.
103   void GetTopSitesData();
104
105   // Callback to receive data requested from GetTopSitesData().
106   void OnTopSitesReceived(const history::MostVisitedURLList& visited_list);
107
108   // Updates the visibility of the bookmark bar on pref changes.
109   void OnBookmarkBarVisibilityChanged();
110
111   void RebuildProfilesMenu();
112
113   // Find the first index of the item in |menu| with the tag |tag_id|.
114   int GetIndexOfMenuItemWithTag(DbusmenuMenuitem* menu, int tag_id);
115
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);
120
121   // Deleter function for HistoryItem implementation detail.
122   static void DeleteHistoryItem(void* void_item);
123
124   // Overridden from AvatarMenuObserver:
125   virtual void OnAvatarMenuChanged(AvatarMenu* avatar_menu) OVERRIDE;
126
127   // Overridden from chrome::BrowserListObserver:
128   virtual void OnBrowserSetLastActive(Browser* browser) OVERRIDE;
129
130   // Overridden from CommandObserver:
131   virtual void EnabledStateChangedForCommand(int id, bool enabled) OVERRIDE;
132
133   // Overridden from content::NotificationObserver:
134   virtual void Observe(int type,
135                        const content::NotificationSource& source,
136                        const content::NotificationDetails& details) OVERRIDE;
137
138   // Overridden from TabRestoreServiceObserver:
139   virtual void TabRestoreServiceChanged(TabRestoreService* service) OVERRIDE;
140   virtual void TabRestoreServiceDestroyed(TabRestoreService* service) OVERRIDE;
141
142   // Overridden from views::DesktopWindowTreeHostObserverX11:
143   virtual void OnWindowMapped(unsigned long xid) OVERRIDE;
144   virtual void OnWindowUnmapped(unsigned long xid) OVERRIDE;
145
146   CHROMEG_CALLBACK_1(GlobalMenuBarX11, void, OnItemActivated, DbusmenuMenuitem*,
147                      unsigned int);
148   CHROMEG_CALLBACK_1(GlobalMenuBarX11, void, OnHistoryItemActivated,
149                      DbusmenuMenuitem*, unsigned int);
150   CHROMEG_CALLBACK_0(GlobalMenuBarX11, void, OnHistoryMenuAboutToShow,
151                      DbusmenuMenuitem*);
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);
158
159   Browser* browser_;
160   Profile* profile_;
161   BrowserView* browser_view_;
162   BrowserDesktopWindowTreeHostX11* host_;
163
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_;
167
168   DbusmenuServer* server_;
169   DbusmenuMenuitem* root_item_;
170   DbusmenuMenuitem* history_menu_;
171   DbusmenuMenuitem* profiles_menu_;
172
173   // Tracks value of the kShowBookmarkBar preference.
174   PrefChangeRegistrar pref_change_registrar_;
175
176   history::TopSites* top_sites_;
177
178   TabRestoreService* tab_restore_service_;  // weak
179
180   scoped_ptr<AvatarMenu> avatar_menu_;
181
182   content::NotificationRegistrar registrar_;
183
184   // For callbacks may be run after destruction.
185   base::WeakPtrFactory<GlobalMenuBarX11> weak_ptr_factory_;
186
187   DISALLOW_COPY_AND_ASSIGN(GlobalMenuBarX11);
188 };
189
190 #endif  // CHROME_BROWSER_UI_VIEWS_FRAME_GLOBAL_MENU_BAR_X11_H_