1 // Copyright 2014 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 COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_OBSERVER_H_
6 #define COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_OBSERVER_H_
9 struct ContextMenuParams
;
12 // The interface used for implementing context-menu items. The following
13 // instruction describe how to implement a context-menu item with this
16 // 1. Add command IDs for the context-menu items to 'chrome_command_ids.h'.
18 // #define IDC_MY_COMMAND 99999
20 // 2. Add strings for the context-menu items to 'generated_sources.grd'.
22 // <message name="IDS_MY_COMMAND" desc="...">
26 // 3. Create a class that implements this interface. (It is a good idea to use
27 // the RenderViewContextMenuProxy interface to avoid accessing the
28 // RenderViewContextMenu class directly.)
30 // class MyMenuObserver : public RenderViewContextMenuObserver {
32 // MyMenuObserver(RenderViewContextMenuProxy* p);
35 // virtual void InitMenu(const content::ContextMenuParams& params) override;
36 // virtual bool IsCommandIdSupported(int command_id) override;
37 // virtual bool IsCommandIdEnabled(int command_id) override;
38 // virtual void ExecuteCommand(int command_id) override;
41 // RenderViewContextMenuProxy* proxy_;
44 // void MyMenuObserver::InitMenu(const content::ContextMenuParams& params) {
45 // proxy_->AddMenuItem(IDC_MY_COMMAND,...);
48 // bool MyMenuObserver::IsCommandIdSupported(int command_id) {
49 // return command_id == IDC_MY_COMMAND;
52 // bool MyMenuObserver::IsCommandIdEnabled(int command_id) {
53 // DCHECK(command_id == IDC_MY_COMMAND);
57 // void MyMenuObserver::ExecuteCommand(int command_id) {
58 // DCHECK(command_id == IDC_MY_COMMAND);
61 // 4. Add this observer class to the RenderViewContextMenu class. (It is good
62 // to use scoped_ptr<> so Chrome can create its instances only when it needs.)
64 // class RenderViewContextMenu {
67 // scoped_ptr<MyMenuObserver> my_menu_observer_;
70 // 5. Create its instance in InitMenu() and add it to the observer list of the
71 // RenderViewContextMenu class.
73 // void RenderViewContextMenu::InitMenu() {
75 // my_menu_observer_.reset(new MyMenuObserver(this));
76 // observers_.AddObserver(my_menu_observer_.get());
80 class RenderViewContextMenuObserver
{
82 virtual ~RenderViewContextMenuObserver() {}
84 // Called when the RenderViewContextMenu class initializes a context menu. We
85 // usually call RenderViewContextMenuProxy::AddMenuItem() to add menu items
87 virtual void InitMenu(const content::ContextMenuParams
& params
) {}
89 // Called when the RenderViewContextMenu class asks whether an observer
90 // listens for the specified command ID. If this function returns true, the
91 // RenderViewContextMenu class calls IsCommandIdEnabled() or ExecuteCommand().
92 virtual bool IsCommandIdSupported(int command_id
);
94 // Called when the RenderViewContextMenu class sets the initial status of the
95 // specified context-menu item. If we need to enable or disable a context-menu
96 // item while showing, use RenderViewContextMenuProxy::UpdateMenuItem().
97 virtual bool IsCommandIdChecked(int command_id
);
98 virtual bool IsCommandIdEnabled(int command_id
);
100 // Called when a user selects the specified context-menu item.
101 virtual void ExecuteCommand(int command_id
) {}
103 // Called when a user closes the context menu without selecting any items.
104 virtual void OnMenuCancel() {}
107 #endif // COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_OBSERVER_H_