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