render_view_context_menu_proxy.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_PROXY_H_
  5. #define COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_PROXY_H_
  6. #include <string>
  7. namespace content {
  8. class BrowserContext;
  9. class RenderViewHost;
  10. class WebContents;
  11. }
  12. namespace ui {
  13. class ImageModel;
  14. class MenuModel;
  15. }
  16. // An interface that controls a RenderViewContextMenu instance from observers.
  17. // This interface is designed mainly for controlling the instance while showing
  18. // so we can add a context-menu item that takes long time to create its text,
  19. // such as retrieving the item text from a server. The simplest usage is:
  20. // 1. Adding an item with temporary text;
  21. // 2. Posting a background task that creates the item text, and;
  22. // 3. Calling UpdateMenuItem() in the callback function.
  23. // The following snippet describes the simple usage that updates a context-menu
  24. // item with this interface.
  25. //
  26. // class MyTask : public net::URLFetcherDelegate {
  27. // public:
  28. // MyTask(RenderViewContextMenuProxy* proxy, int id)
  29. // : proxy_(proxy),
  30. // id_(id) {
  31. // }
  32. // virtual ~MyTask() {
  33. // }
  34. // virtual void OnURLFetchComplete(const net::URLFetcher* source,
  35. // const GURL& url,
  36. // const net::URLRequestStatus& status,
  37. // int response,
  38. // const net::ResponseCookies& cookies,
  39. // const std::string& data) {
  40. // bool enabled = response == 200;
  41. // const char* text = enabled ? "OK" : "ERROR";
  42. // proxy_->UpdateMenuItem(id_, enabled, base::ASCIIToUTF16(text));
  43. // }
  44. // void Start(const GURL* url, net::URLRequestContextGetter* context) {
  45. // fetcher_.reset(new URLFetcher(url, URLFetcher::GET, this));
  46. // fetcher_->SetRequestContext(context);
  47. // content::AssociateURLFetcherWithRenderView(
  48. // fetcher_.get(),
  49. // proxy_->GetRenderViewHost()->GetSiteInstance()->GetSite(),
  50. // proxy_->GetRenderViewHost()->GetProcess()->GetID(),
  51. // proxy_->GetRenderViewHost()->GetRoutingID());
  52. // fetcher_->Start();
  53. // }
  54. //
  55. // private:
  56. // URLFetcher fetcher_;
  57. // RenderViewContextMenuProxy* proxy_;
  58. // int id_;
  59. // };
  60. //
  61. // void RenderViewContextMenu::AppendEditableItems() {
  62. // // Add a menu item with temporary text shown while we create the final
  63. // // text.
  64. // menu_model_.AddItemWithStringId(IDC_MY_ITEM, IDC_MY_TEXT);
  65. //
  66. // // Start a task that creates the final text.
  67. // my_task_ = new MyTask(this, IDC_MY_ITEM);
  68. // my_task_->Start(...);
  69. // }
  70. //
  71. class RenderViewContextMenuProxy {
  72. public:
  73. // Add a menu item to a context menu.
  74. virtual void AddMenuItem(int command_id, const std::u16string& title) = 0;
  75. virtual void AddMenuItemWithIcon(int command_id,
  76. const std::u16string& title,
  77. const ui::ImageModel& icon) = 0;
  78. virtual void AddCheckItem(int command_id, const std::u16string& title) = 0;
  79. virtual void AddSeparator() = 0;
  80. // Add a submenu item to a context menu.
  81. virtual void AddSubMenu(int command_id,
  82. const std::u16string& label,
  83. ui::MenuModel* model) = 0;
  84. virtual void AddSubMenuWithStringIdAndIcon(int command_id,
  85. int message_id,
  86. ui::MenuModel* model,
  87. const ui::ImageModel& icon) = 0;
  88. // Update the status and text of the specified context-menu item.
  89. virtual void UpdateMenuItem(int command_id,
  90. bool enabled,
  91. bool hidden,
  92. const std::u16string& title) = 0;
  93. // Update the icon of the specified context-menu item.
  94. virtual void UpdateMenuIcon(int command_id, const ui::ImageModel& icon) = 0;
  95. // Remove the specified context-menu item.
  96. virtual void RemoveMenuItem(int command_id) = 0;
  97. // Removes separators so that any adjacent duplicates are reduced to only 1.
  98. virtual void RemoveAdjacentSeparators() = 0;
  99. // Removes separator (if any) before the specified context menu item.
  100. virtual void RemoveSeparatorBeforeMenuItem(int command_id) = 0;
  101. // Add spell check service item to the context menu.
  102. virtual void AddSpellCheckServiceItem(bool is_checked) = 0;
  103. // Add accessibility labels service item to the context menu.
  104. virtual void AddAccessibilityLabelsServiceItem(bool is_checked) = 0;
  105. // Retrieve the given associated objects with a context menu.
  106. virtual content::RenderViewHost* GetRenderViewHost() const = 0;
  107. virtual content::WebContents* GetWebContents() const = 0;
  108. virtual content::BrowserContext* GetBrowserContext() const = 0;
  109. };
  110. #endif // COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_PROXY_H_