1 // Copyright (c) 2011 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_BOOKMARKS_BOOKMARK_CONTEXT_MENU_H_ 6 #define CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_CONTEXT_MENU_H_ 7 8 #include "base/compiler_specific.h" 9 #include "base/macros.h" 10 #include "chrome/browser/ui/bookmarks/bookmark_context_menu_controller.h" 11 #include "chrome/browser/ui/bookmarks/bookmark_stats.h" 12 #include "ui/views/controls/menu/menu_delegate.h" 13 14 class Browser; 15 16 namespace views { 17 class MenuRunner; 18 class Widget; 19 } 20 21 // Observer for the BookmarkContextMenu. 22 class BookmarkContextMenuObserver { 23 public: 24 // Invoked before the specified items are removed from the bookmark model. 25 virtual void WillRemoveBookmarks( 26 const std::vector<const bookmarks::BookmarkNode*>& bookmarks) = 0; 27 28 // Invoked after the items have been removed from the model. 29 virtual void DidRemoveBookmarks() = 0; 30 31 // Invoked when the context menu is closed. 32 virtual void OnContextMenuClosed() = 0; 33 34 protected: ~BookmarkContextMenuObserver()35 virtual ~BookmarkContextMenuObserver() {} 36 }; 37 38 class BookmarkContextMenu : public BookmarkContextMenuControllerDelegate, 39 public views::MenuDelegate { 40 public: 41 // |browser| is used to open the bookmark manager, and is NULL in tests. 42 BookmarkContextMenu( 43 views::Widget* parent_widget, 44 Browser* browser, 45 Profile* profile, 46 content::PageNavigator* page_navigator, 47 BookmarkLaunchLocation opened_from, 48 const bookmarks::BookmarkNode* parent, 49 const std::vector<const bookmarks::BookmarkNode*>& selection, 50 bool close_on_remove); 51 ~BookmarkContextMenu() override; 52 53 // Installs a callback to be run before the context menu is run. The callback 54 // runs only once, and only one such callback can be set at any time. Once the 55 // installed callback is run, another callback can be installed. 56 static void InstallPreRunCallback(base::OnceClosure callback); 57 58 // Shows the context menu at the specified point. 59 void RunMenuAt(const gfx::Point& point, 60 ui::MenuSourceType source_type); 61 menu()62 views::MenuItemView* menu() const { return menu_; } 63 set_observer(BookmarkContextMenuObserver * observer)64 void set_observer(BookmarkContextMenuObserver* observer) { 65 observer_ = observer; 66 } 67 68 // Sets the PageNavigator. 69 void SetPageNavigator(content::PageNavigator* navigator); 70 71 // Overridden from views::MenuDelegate: 72 void ExecuteCommand(int command_id, int event_flags) override; 73 bool IsItemChecked(int command_id) const override; 74 bool IsCommandEnabled(int command_id) const override; 75 bool IsCommandVisible(int command_id) const override; 76 bool ShouldCloseAllMenusOnExecute(int id) override; 77 void OnMenuClosed(views::MenuItemView* menu) override; 78 79 // Overridden from BookmarkContextMenuControllerDelegate: 80 void CloseMenu() override; 81 void WillExecuteCommand( 82 int command_id, 83 const std::vector<const bookmarks::BookmarkNode*>& bookmarks) override; 84 void DidExecuteCommand(int command_id) override; 85 86 private: 87 std::unique_ptr<BookmarkContextMenuController> controller_; 88 89 // The parent of dialog boxes opened from the context menu. 90 views::Widget* parent_widget_; 91 92 // The menu itself. This is owned by |menu_runner_|. 93 views::MenuItemView* menu_; 94 95 // Responsible for running the menu. 96 std::unique_ptr<views::MenuRunner> menu_runner_; 97 98 BookmarkContextMenuObserver* observer_; 99 100 // Should the menu close when a node is removed. 101 bool close_on_remove_; 102 103 DISALLOW_COPY_AND_ASSIGN(BookmarkContextMenu); 104 }; 105 106 #endif // CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_CONTEXT_MENU_H_ 107