| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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_TAB_CONTENTS_RENDER_VIEW_CONTEXT_MENU_H_ | |
| 6 #define CHROME_BROWSER_TAB_CONTENTS_RENDER_VIEW_CONTEXT_MENU_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/observer_list.h" | |
| 13 #include "base/strings/string16.h" | |
| 14 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" | |
| 15 #include "chrome/browser/extensions/context_menu_matcher.h" | |
| 16 #include "chrome/browser/extensions/menu_manager.h" | |
| 17 #include "chrome/browser/tab_contents/render_view_context_menu_observer.h" | |
| 18 #include "content/public/common/context_menu_params.h" | |
| 19 #include "content/public/common/page_transition_types.h" | |
| 20 #include "ui/base/models/simple_menu_model.h" | |
| 21 #include "ui/base/window_open_disposition.h" | |
| 22 | |
| 23 class PrintPreviewContextMenuObserver; | |
| 24 class Profile; | |
| 25 class SpellingMenuObserver; | |
| 26 class SpellCheckerSubMenuObserver; | |
| 27 | |
| 28 namespace content { | |
| 29 class RenderFrameHost; | |
| 30 class RenderViewHost; | |
| 31 class WebContents; | |
| 32 } | |
| 33 | |
| 34 namespace extensions { | |
| 35 class Extension; | |
| 36 class MenuItem; | |
| 37 } | |
| 38 | |
| 39 namespace gfx { | |
| 40 class Point; | |
| 41 } | |
| 42 | |
| 43 namespace blink { | |
| 44 struct WebMediaPlayerAction; | |
| 45 struct WebPluginAction; | |
| 46 } | |
| 47 | |
| 48 // An interface that controls a RenderViewContextMenu instance from observers. | |
| 49 // This interface is designed mainly for controlling the instance while showing | |
| 50 // so we can add a context-menu item that takes long time to create its text, | |
| 51 // such as retrieving the item text from a server. The simplest usage is: | |
| 52 // 1. Adding an item with temporary text; | |
| 53 // 2. Posting a background task that creates the item text, and; | |
| 54 // 3. Calling UpdateMenuItem() in the callback function. | |
| 55 // The following snippet describes the simple usage that updates a context-menu | |
| 56 // item with this interface. | |
| 57 // | |
| 58 // class MyTask : public net::URLFetcherDelegate { | |
| 59 // public: | |
| 60 // MyTask(RenderViewContextMenuProxy* proxy, int id) | |
| 61 // : proxy_(proxy), | |
| 62 // id_(id) { | |
| 63 // } | |
| 64 // virtual ~MyTask() { | |
| 65 // } | |
| 66 // virtual void OnURLFetchComplete(const net::URLFetcher* source, | |
| 67 // const GURL& url, | |
| 68 // const net::URLRequestStatus& status, | |
| 69 // int response, | |
| 70 // const net::ResponseCookies& cookies, | |
| 71 // const std::string& data) { | |
| 72 // bool enabled = response == 200; | |
| 73 // const char* text = enabled ? "OK" : "ERROR"; | |
| 74 // proxy_->UpdateMenuItem(id_, enabled, base::ASCIIToUTF16(text)); | |
| 75 // } | |
| 76 // void Start(const GURL* url, net::URLRequestContextGetter* context) { | |
| 77 // fetcher_.reset(new URLFetcher(url, URLFetcher::GET, this)); | |
| 78 // fetcher_->SetRequestContext(context); | |
| 79 // content::AssociateURLFetcherWithRenderView( | |
| 80 // fetcher_.get(), | |
| 81 // proxy_->GetRenderViewHost()->GetSiteInstance()->GetSite(), | |
| 82 // proxy_->GetRenderViewHost()->GetProcess()->GetID(), | |
| 83 // proxy_->GetRenderViewHost()->GetRoutingID()); | |
| 84 // fetcher_->Start(); | |
| 85 // } | |
| 86 // | |
| 87 // private: | |
| 88 // URLFetcher fetcher_; | |
| 89 // RenderViewContextMenuProxy* proxy_; | |
| 90 // int id_; | |
| 91 // }; | |
| 92 // | |
| 93 // void RenderViewContextMenu::AppendEditableItems() { | |
| 94 // // Add a menu item with temporary text shown while we create the final | |
| 95 // // text. | |
| 96 // menu_model_.AddItemWithStringId(IDC_MY_ITEM, IDC_MY_TEXT); | |
| 97 // | |
| 98 // // Start a task that creates the final text. | |
| 99 // my_task_ = new MyTask(this, IDC_MY_ITEM); | |
| 100 // my_task_->Start(...); | |
| 101 // } | |
| 102 // | |
| 103 class RenderViewContextMenuProxy { | |
| 104 public: | |
| 105 // Add a menu item to a context menu. | |
| 106 virtual void AddMenuItem(int command_id, const base::string16& title) = 0; | |
| 107 virtual void AddCheckItem(int command_id, const base::string16& title) = 0; | |
| 108 virtual void AddSeparator() = 0; | |
| 109 | |
| 110 // Add a submenu item to a context menu. | |
| 111 virtual void AddSubMenu(int command_id, | |
| 112 const base::string16& label, | |
| 113 ui::MenuModel* model) = 0; | |
| 114 | |
| 115 // Update the status and text of the specified context-menu item. | |
| 116 virtual void UpdateMenuItem(int command_id, | |
| 117 bool enabled, | |
| 118 bool hidden, | |
| 119 const base::string16& title) = 0; | |
| 120 | |
| 121 // Retrieve the given associated objects with a context menu. | |
| 122 virtual content::RenderViewHost* GetRenderViewHost() const = 0; | |
| 123 virtual content::WebContents* GetWebContents() const = 0; | |
| 124 virtual Profile* GetProfile() const = 0; | |
| 125 }; | |
| 126 | |
| 127 class RenderViewContextMenu : public ui::SimpleMenuModel::Delegate, | |
| 128 public RenderViewContextMenuProxy { | |
| 129 public: | |
| 130 static const size_t kMaxSelectionTextLength; | |
| 131 | |
| 132 RenderViewContextMenu(content::RenderFrameHost* render_frame_host, | |
| 133 const content::ContextMenuParams& params); | |
| 134 | |
| 135 virtual ~RenderViewContextMenu(); | |
| 136 | |
| 137 // Initializes the context menu. | |
| 138 void Init(); | |
| 139 | |
| 140 // Programmatically closes the context menu. | |
| 141 void Cancel(); | |
| 142 | |
| 143 const ui::MenuModel& menu_model() const { return menu_model_; } | |
| 144 | |
| 145 // SimpleMenuModel::Delegate implementation. | |
| 146 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; | |
| 147 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; | |
| 148 virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE; | |
| 149 virtual void MenuWillShow(ui::SimpleMenuModel* source) OVERRIDE; | |
| 150 virtual void MenuClosed(ui::SimpleMenuModel* source) OVERRIDE; | |
| 151 | |
| 152 // RenderViewContextMenuDelegate implementation. | |
| 153 virtual void AddMenuItem(int command_id, | |
| 154 const base::string16& title) OVERRIDE; | |
| 155 virtual void AddCheckItem(int command_id, | |
| 156 const base::string16& title) OVERRIDE; | |
| 157 virtual void AddSeparator() OVERRIDE; | |
| 158 virtual void AddSubMenu(int command_id, | |
| 159 const base::string16& label, | |
| 160 ui::MenuModel* model) OVERRIDE; | |
| 161 virtual void UpdateMenuItem(int command_id, | |
| 162 bool enabled, | |
| 163 bool hidden, | |
| 164 const base::string16& title) OVERRIDE; | |
| 165 virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE; | |
| 166 virtual content::WebContents* GetWebContents() const OVERRIDE; | |
| 167 virtual Profile* GetProfile() const OVERRIDE; | |
| 168 | |
| 169 protected: | |
| 170 void InitMenu(); | |
| 171 | |
| 172 // Platform specific functions. | |
| 173 virtual void PlatformInit() = 0; | |
| 174 virtual void PlatformCancel() = 0; | |
| 175 virtual bool GetAcceleratorForCommandId( | |
| 176 int command_id, | |
| 177 ui::Accelerator* accelerator) = 0; | |
| 178 virtual void AppendPlatformEditableItems(); | |
| 179 | |
| 180 content::ContextMenuParams params_; | |
| 181 content::WebContents* source_web_contents_; | |
| 182 // The RenderFrameHost's IDs. | |
| 183 int render_process_id_; | |
| 184 int render_frame_id_; | |
| 185 Profile* profile_; | |
| 186 | |
| 187 ui::SimpleMenuModel menu_model_; | |
| 188 extensions::ContextMenuMatcher extension_items_; | |
| 189 | |
| 190 private: | |
| 191 friend class RenderViewContextMenuTest; | |
| 192 friend class RenderViewContextMenuPrefsTest; | |
| 193 | |
| 194 static bool IsDevToolsURL(const GURL& url); | |
| 195 static bool IsInternalResourcesURL(const GURL& url); | |
| 196 static bool ExtensionContextAndPatternMatch( | |
| 197 const content::ContextMenuParams& params, | |
| 198 extensions::MenuItem::ContextList contexts, | |
| 199 const extensions::URLPatternSet& target_url_patterns); | |
| 200 static bool MenuItemMatchesParams( | |
| 201 const content::ContextMenuParams& params, | |
| 202 const extensions::MenuItem* item); | |
| 203 | |
| 204 // Gets the extension (if any) associated with the WebContents that we're in. | |
| 205 const extensions::Extension* GetExtension() const; | |
| 206 void AppendAppModeItems(); | |
| 207 void AppendPlatformAppItems(); | |
| 208 void AppendPopupExtensionItems(); | |
| 209 void AppendPanelItems(); | |
| 210 bool AppendCustomItems(); | |
| 211 void AppendDeveloperItems(); | |
| 212 void AppendLinkItems(); | |
| 213 void AppendImageItems(); | |
| 214 void AppendAudioItems(); | |
| 215 void AppendVideoItems(); | |
| 216 void AppendMediaItems(); | |
| 217 void AppendPluginItems(); | |
| 218 void AppendPageItems(); | |
| 219 void AppendFrameItems(); | |
| 220 void AppendCopyItem(); | |
| 221 void AppendPrintItem(); | |
| 222 void AppendEditableItems(); | |
| 223 void AppendSearchProvider(); | |
| 224 void AppendAllExtensionItems(); | |
| 225 void AppendSpellingSuggestionsSubMenu(); | |
| 226 void AppendSpellcheckOptionsSubMenu(); | |
| 227 void AppendSpeechInputOptionsSubMenu(); | |
| 228 void AppendProtocolHandlerSubMenu(); | |
| 229 | |
| 230 // Opens the specified URL string in a new tab. The |frame_id| specifies the | |
| 231 // frame in which the context menu was displayed, or 0 if the menu action is | |
| 232 // independent of that frame (e.g. protocol handler settings). | |
| 233 void OpenURL(const GURL& url, const GURL& referrer, int64 frame_id, | |
| 234 WindowOpenDisposition disposition, | |
| 235 content::PageTransition transition); | |
| 236 | |
| 237 // Copy to the clipboard an image located at a point in the RenderView | |
| 238 void CopyImageAt(int x, int y); | |
| 239 | |
| 240 // Get an image located at a point in the RenderView for search. | |
| 241 void GetImageThumbnailForSearch(); | |
| 242 | |
| 243 // Launch the inspector targeting a point in the RenderView | |
| 244 void Inspect(int x, int y); | |
| 245 | |
| 246 // Writes the specified text/url to the system clipboard | |
| 247 void WriteURLToClipboard(const GURL& url); | |
| 248 | |
| 249 void MediaPlayerActionAt(const gfx::Point& location, | |
| 250 const blink::WebMediaPlayerAction& action); | |
| 251 void PluginActionAt(const gfx::Point& location, | |
| 252 const blink::WebPluginAction& action); | |
| 253 | |
| 254 bool IsDevCommandEnabled(int id) const; | |
| 255 | |
| 256 // Returns a list of registered ProtocolHandlers that can handle the clicked | |
| 257 // on URL. | |
| 258 ProtocolHandlerRegistry::ProtocolHandlerList GetHandlersForLinkUrl(); | |
| 259 | |
| 260 // Returns a (possibly truncated) version of the current selection text | |
| 261 // suitable or putting in the title of a menu item. | |
| 262 base::string16 PrintableSelectionText(); | |
| 263 | |
| 264 // The destination URL to use if the user tries to search for or navigate to | |
| 265 // a text selection. | |
| 266 GURL selection_navigation_url_; | |
| 267 | |
| 268 ui::SimpleMenuModel speech_input_submenu_model_; | |
| 269 ui::SimpleMenuModel protocol_handler_submenu_model_; | |
| 270 ProtocolHandlerRegistry* protocol_handler_registry_; | |
| 271 | |
| 272 // An observer that handles spelling-menu items. | |
| 273 scoped_ptr<SpellingMenuObserver> spelling_menu_observer_; | |
| 274 | |
| 275 // An observer that handles a 'spell-checker options' submenu. | |
| 276 scoped_ptr<SpellCheckerSubMenuObserver> spellchecker_submenu_observer_; | |
| 277 | |
| 278 #if defined(ENABLE_FULL_PRINTING) | |
| 279 // An observer that disables menu items when print preview is active. | |
| 280 scoped_ptr<PrintPreviewContextMenuObserver> print_preview_menu_observer_; | |
| 281 #endif | |
| 282 | |
| 283 // Our observers. | |
| 284 mutable ObserverList<RenderViewContextMenuObserver> observers_; | |
| 285 | |
| 286 // Whether a command has been executed. Used to track whether menu observers | |
| 287 // should be notified of menu closing without execution. | |
| 288 bool command_executed_; | |
| 289 | |
| 290 // Whether or not the menu was triggered for a browser plugin guest. | |
| 291 // Guests are rendered inside chrome apps, but have most of the actions | |
| 292 // that a regular web page has. | |
| 293 // Currently actions/items that are suppressed from guests are: searching, | |
| 294 // printing, speech and instant. | |
| 295 bool is_guest_; | |
| 296 | |
| 297 DISALLOW_COPY_AND_ASSIGN(RenderViewContextMenu); | |
| 298 }; | |
| 299 | |
| 300 #endif // CHROME_BROWSER_TAB_CONTENTS_RENDER_VIEW_CONTEXT_MENU_H_ | |
| OLD | NEW |