render_view_context_menu_observer.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.
  4. #ifndef COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_OBSERVER_H_
  5. #define COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_OBSERVER_H_
  6. #include "ui/gfx/geometry/rect.h"
  7. namespace content {
  8. struct ContextMenuParams;
  9. }
  10. // The interface used for implementing context-menu items. The following
  11. // instruction describe how to implement a context-menu item with this
  12. // interface.
  13. //
  14. // 1. Add command IDs for the context-menu items to 'chrome_command_ids.h'.
  15. //
  16. // #define IDC_MY_COMMAND 99999
  17. //
  18. // 2. Add strings for the context-menu items to 'generated_sources.grd'.
  19. //
  20. // <message name="IDS_MY_COMMAND" desc="...">
  21. // My command
  22. // </message>
  23. //
  24. // 3. Create a class that implements this interface. (It is a good idea to use
  25. // the RenderViewContextMenuProxy interface to avoid accessing the
  26. // RenderViewContextMenu class directly.)
  27. //
  28. // class MyMenuObserver : public RenderViewContextMenuObserver {
  29. // public:
  30. // MyMenuObserver(RenderViewContextMenuProxy* p);
  31. // ~MyMenuObserver() override;
  32. //
  33. // void InitMenu(const content::ContextMenuParams& params) override;
  34. // bool IsCommandIdSupported(int command_id) override;
  35. // bool IsCommandIdEnabled(int command_id) override;
  36. // void ExecuteCommand(int command_id) override;
  37. //
  38. // private:
  39. // RenderViewContextMenuProxy* proxy_;
  40. // }
  41. //
  42. // void MyMenuObserver::InitMenu(const content::ContextMenuParams& params) {
  43. // proxy_->AddMenuItem(IDC_MY_COMMAND,...);
  44. // }
  45. //
  46. // bool MyMenuObserver::IsCommandIdSupported(int command_id) {
  47. // return command_id == IDC_MY_COMMAND;
  48. // }
  49. //
  50. // bool MyMenuObserver::IsCommandIdEnabled(int command_id) {
  51. // DCHECK(command_id == IDC_MY_COMMAND);
  52. // return true;
  53. // }
  54. //
  55. // void MyMenuObserver::ExecuteCommand(int command_id) {
  56. // DCHECK(command_id == IDC_MY_COMMAND);
  57. // }
  58. //
  59. // 4. Add this observer class to the RenderViewContextMenu class. (It is good
  60. // to use std::unique_ptr<> so Chrome can create its instances only when it
  61. // needs.)
  62. //
  63. // class RenderViewContextMenu {
  64. // ...
  65. // private:
  66. // std::unique_ptr<MyMenuObserver> my_menu_observer_;
  67. // };
  68. //
  69. // 5. Create its instance in InitMenu() and add it to the observer list of the
  70. // RenderViewContextMenu class.
  71. //
  72. // void RenderViewContextMenu::InitMenu() {
  73. // ...
  74. // my_menu_observer_.reset(new MyMenuObserver(this));
  75. // observers_.AddObserver(my_menu_observer_.get());
  76. // }
  77. //
  78. //
  79. class RenderViewContextMenuObserver {
  80. public:
  81. virtual ~RenderViewContextMenuObserver() {}
  82. // Called when the RenderViewContextMenu class initializes a context menu. We
  83. // usually call RenderViewContextMenuProxy::AddMenuItem() to add menu items
  84. // in this function.
  85. virtual void InitMenu(const content::ContextMenuParams& params) {}
  86. // Called when the RenderViewContextMenu class asks whether an observer
  87. // listens for the specified command ID. If this function returns true, the
  88. // RenderViewContextMenu class calls IsCommandIdEnabled() or ExecuteCommand().
  89. virtual bool IsCommandIdSupported(int command_id);
  90. // Called when the RenderViewContextMenu class sets the initial status of the
  91. // specified context-menu item. If we need to enable or disable a context-menu
  92. // item while showing, use RenderViewContextMenuProxy::UpdateMenuItem().
  93. virtual bool IsCommandIdChecked(int command_id);
  94. virtual bool IsCommandIdEnabled(int command_id);
  95. // Called when a user selects the specified context-menu item. This is
  96. // only called when the observer returns true for IsCommandIdSupported()
  97. // for that |command_id|.
  98. virtual void ExecuteCommand(int command_id) {}
  99. // Called when a user selects the specified context-menu item, including
  100. // command that is supported by other observers.
  101. virtual void CommandWillBeExecuted(int command_id) {}
  102. virtual void OnMenuClosed() {}
  103. virtual void OnContextMenuShown(const content::ContextMenuParams& params,
  104. const gfx::Rect& bounds_in_screen) {}
  105. //
  106. virtual void OnContextMenuViewBoundsChanged(
  107. const gfx::Rect& bounds_in_screen) {}
  108. };
  109. #endif // COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_OBSERVER_H_