• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_TAB_CONTENTS_RENDER_VIEW_CONTEXT_MENU_H_
6 #define CHROME_BROWSER_TAB_CONTENTS_RENDER_VIEW_CONTEXT_MENU_H_
7 #pragma once
8 
9 #include <map>
10 #include <string>
11 #include <vector>
12 
13 #include "base/memory/scoped_vector.h"
14 #include "base/string16.h"
15 #include "chrome/browser/extensions/extension_menu_manager.h"
16 #include "content/common/page_transition_types.h"
17 #include "ui/base/models/simple_menu_model.h"
18 #include "webkit/glue/context_menu.h"
19 #include "webkit/glue/window_open_disposition.h"
20 
21 class ExtensionMenuItem;
22 class Profile;
23 class TabContents;
24 
25 namespace gfx {
26 class Point;
27 }
28 
29 namespace WebKit {
30 struct WebMediaPlayerAction;
31 }
32 
33 class RenderViewContextMenu : public ui::SimpleMenuModel::Delegate {
34  public:
35   static const size_t kMaxExtensionItemTitleLength;
36   static const size_t kMaxSelectionTextLength;
37 
38   RenderViewContextMenu(TabContents* tab_contents,
39                         const ContextMenuParams& params);
40 
41   virtual ~RenderViewContextMenu();
42 
43   // Initializes the context menu.
44   void Init();
45 
46   // SimpleMenuModel::Delegate implementation.
47   virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
48   virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
49   virtual void ExecuteCommand(int command_id) OVERRIDE;
50   virtual void MenuWillShow() OVERRIDE;
51   virtual void MenuClosed() OVERRIDE;
52 
53  protected:
54   void InitMenu();
55 
56   // Platform specific functions.
57   virtual void PlatformInit() = 0;
58   virtual bool GetAcceleratorForCommandId(
59       int command_id,
60       ui::Accelerator* accelerator) = 0;
61   virtual void LookUpInDictionary();
62 
63   // Attempts to get an ExtensionMenuItem given the id of a context menu item.
64   ExtensionMenuItem* GetExtensionMenuItem(int id) const;
65 
66   ContextMenuParams params_;
67   TabContents* source_tab_contents_;
68   Profile* profile_;
69 
70   ui::SimpleMenuModel menu_model_;
71 
72   // True if we are showing for an external tab contents. The default is false.
73   bool external_;
74 
75 
76   // Maps the id from a context menu item to the ExtensionMenuItem's internal
77   // id.
78   std::map<int, ExtensionMenuItem::Id> extension_item_map_;
79 
80  private:
81   static bool IsDevToolsURL(const GURL& url);
82   static bool IsInternalResourcesURL(const GURL& url);
83   bool AppendCustomItems();
84   void AppendDeveloperItems();
85   void AppendLinkItems();
86   void AppendImageItems();
87   void AppendAudioItems();
88   void AppendVideoItems();
89   void AppendMediaItems();
90   void AppendPluginItems();
91   void AppendPageItems();
92   void AppendFrameItems();
93   void AppendCopyItem();
94   void AppendEditableItems();
95   void AppendSearchProvider();
96   void AppendAllExtensionItems();
97   void AppendSpellcheckOptionsSubMenu();
98   // Add writing direction sub menu (only used on Mac).
99   void AppendBidiSubMenu();
100 
101   // This is a helper function to append items for one particular extension.
102   // The |index| parameter is used for assigning id's, and is incremented for
103   // each item actually added.
104   void AppendExtensionItems(const std::string& extension_id, int* index);
105 
106   // Used for recursively adding submenus of extension items.
107   void RecursivelyAppendExtensionItems(
108       const std::vector<ExtensionMenuItem*>& items,
109       bool can_cross_incognito,
110       ui::SimpleMenuModel* menu_model,
111       int *index);
112   // This will set the icon on the most recently-added item in the menu_model_.
113   void SetExtensionIcon(const std::string& extension_id);
114 
115   // Opens the specified URL string in a new tab.  If |in_current_window| is
116   // false, a new window is created to hold the new tab.
117   void OpenURL(const GURL& url,
118                WindowOpenDisposition disposition,
119                PageTransition::Type transition);
120 
121   // Copy to the clipboard an image located at a point in the RenderView
122   void CopyImageAt(int x, int y);
123 
124   // Launch the inspector targeting a point in the RenderView
125   void Inspect(int x, int y);
126 
127   // Writes the specified text/url to the system clipboard
128   void WriteURLToClipboard(const GURL& url);
129 
130   void MediaPlayerActionAt(const gfx::Point& location,
131                            const WebKit::WebMediaPlayerAction& action);
132 
133   bool IsDevCommandEnabled(int id) const;
134 
135   // Returns a (possibly truncated) version of the current selection text
136   // suitable or putting in the title of a menu item.
137   string16 PrintableSelectionText();
138 
139   // The destination URL to use if the user tries to search for or navigate to
140   // a text selection.
141   GURL selection_navigation_url_;
142 
143   ui::SimpleMenuModel spellcheck_submenu_model_;
144   ui::SimpleMenuModel bidi_submenu_model_;
145   ScopedVector<ui::SimpleMenuModel> extension_menu_models_;
146 
147   DISALLOW_COPY_AND_ASSIGN(RenderViewContextMenu);
148 };
149 
150 #endif  // CHROME_BROWSER_TAB_CONTENTS_RENDER_VIEW_CONTEXT_MENU_H_
151