5452f1e608e9cc56700f208c7032a6f7ec3777c3
[platform/framework/web/crosswalk.git] / src / components / renderer_context_menu / render_view_context_menu_base.h
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
5 #ifndef COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_BASE_H_
6 #define COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_BASE_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 "components/renderer_context_menu/context_menu_content_type.h"
15 #include "components/renderer_context_menu/render_view_context_menu_observer.h"
16 #include "components/renderer_context_menu/render_view_context_menu_proxy.h"
17 #include "content/public/common/context_menu_params.h"
18 #include "content/public/common/page_transition_types.h"
19 #include "ui/base/models/simple_menu_model.h"
20 #include "ui/base/window_open_disposition.h"
21
22 namespace content {
23 class RenderFrameHost;
24 class WebContents;
25 }
26
27 namespace gfx {
28 class Point;
29 }
30
31 namespace blink {
32 struct WebMediaPlayerAction;
33 struct WebPluginAction;
34 }
35
36 class RenderViewContextMenuBase : public ui::SimpleMenuModel::Delegate,
37                                   public RenderViewContextMenuProxy {
38  public:
39   // A delegate interface to communicate with the toolkit used by
40   // the embedder.
41   class ToolkitDelegate {
42    public:
43     virtual ~ToolkitDelegate() {}
44     // Initialize the toolkit's menu.
45     virtual void Init(ui::SimpleMenuModel* menu_model) = 0;
46
47     virtual void Cancel() = 0;
48
49     // Updates the actual menu items controlled by the toolkit.
50     virtual void UpdateMenuItem(int command_id,
51                                 bool enabled,
52                                 bool hidden,
53                                 const base::string16& title) = 0;
54   };
55
56   static const size_t kMaxSelectionTextLength;
57
58   static void SetContentCustomCommandIdRange(int first, int last);
59
60   // Convert a command ID so that it fits within the range for
61   // content context menu.
62   static int ConvertToContentCustomCommandId(int id);
63
64   // True if the given id is the one generated for content context menu.
65   static bool IsContentCustomCommandId(int id);
66
67   RenderViewContextMenuBase(content::RenderFrameHost* render_frame_host,
68                             const content::ContextMenuParams& params);
69
70   virtual ~RenderViewContextMenuBase();
71
72   // Initializes the context menu.
73   void Init();
74
75   // Programmatically closes the context menu.
76   void Cancel();
77
78   const ui::SimpleMenuModel& menu_model() const { return menu_model_; }
79   const content::ContextMenuParams& params() const { return params_; }
80
81   // Returns true if the specified command id is known and valid for
82   // this menu. If the command is known |enabled| is set to indicate
83   // if the command is enabled.
84   bool IsCommandIdKnown(int command_id, bool* enabled) const;
85
86   // SimpleMenuModel::Delegate implementation.
87   virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
88   virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
89   virtual void MenuWillShow(ui::SimpleMenuModel* source) OVERRIDE;
90   virtual void MenuClosed(ui::SimpleMenuModel* source) OVERRIDE;
91
92   // RenderViewContextMenuProxy implementation.
93   virtual void AddMenuItem(int command_id,
94                            const base::string16& title) OVERRIDE;
95   virtual void AddCheckItem(int command_id,
96                             const base::string16& title) OVERRIDE;
97   virtual void AddSeparator() OVERRIDE;
98   virtual void AddSubMenu(int command_id,
99                           const base::string16& label,
100                           ui::MenuModel* model) OVERRIDE;
101   virtual void UpdateMenuItem(int command_id,
102                               bool enabled,
103                               bool hidden,
104                               const base::string16& title) OVERRIDE;
105   virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE;
106   virtual content::WebContents* GetWebContents() const OVERRIDE;
107   virtual content::BrowserContext* GetBrowserContext() const OVERRIDE;
108
109  protected:
110   friend class RenderViewContextMenuTest;
111   friend class RenderViewContextMenuPrefsTest;
112
113   void set_content_type(ContextMenuContentType* content_type) {
114     content_type_.reset(content_type);
115   }
116
117   void set_toolkit_delegate(scoped_ptr<ToolkitDelegate> delegate) {
118     toolkit_delegate_ = delegate.Pass();
119   }
120
121   ToolkitDelegate* toolkit_delegate() {
122     return toolkit_delegate_.get();
123   }
124
125   // TODO(oshima): Make these methods delegate.
126
127   // Menu Construction.
128   virtual void InitMenu();
129
130   // Increments histogram value for used items specified by |id|.
131   virtual void RecordUsedItem(int id) = 0;
132
133   // Increments histogram value for visible context menu item specified by |id|.
134   virtual void RecordShownItem(int id) = 0;
135
136 #if defined(ENABLE_PLUGINS)
137   virtual void HandleAuthorizeAllPlugins() = 0;
138 #endif
139
140   // Returns the accelerator for given |command_id|.
141   virtual bool GetAcceleratorForCommandId(
142       int command_id,
143       ui::Accelerator* accelerator) = 0;
144
145   // Subclasses should send notification.
146   virtual void NotifyMenuShown() = 0;
147   virtual void NotifyURLOpened(const GURL& url,
148                                content::WebContents* new_contents) = 0;
149
150   // TODO(oshima): Remove this.
151   virtual void AppendPlatformEditableItems() {}
152
153   content::RenderFrameHost* GetRenderFrameHost();
154
155   bool IsCustomItemChecked(int id) const;
156   bool IsCustomItemEnabled(int id) const;
157
158   // Opens the specified URL string in a new tab.
159   void OpenURL(const GURL& url, const GURL& referrer,
160                WindowOpenDisposition disposition,
161                content::PageTransition transition);
162
163   content::ContextMenuParams params_;
164   content::WebContents* source_web_contents_;
165   content::BrowserContext* browser_context_;
166
167   ui::SimpleMenuModel menu_model_;
168
169   // Our observers.
170   mutable ObserverList<RenderViewContextMenuObserver> observers_;
171
172   // Whether a command has been executed. Used to track whether menu observers
173   // should be notified of menu closing without execution.
174   bool command_executed_;
175
176   scoped_ptr<ContextMenuContentType> content_type_;
177
178  private:
179   bool AppendCustomItems();
180
181   // The RenderFrameHost's IDs.
182   int render_process_id_;
183   int render_frame_id_;
184
185   scoped_ptr<ToolkitDelegate> toolkit_delegate_;
186
187   DISALLOW_COPY_AND_ASSIGN(RenderViewContextMenuBase);
188 };
189
190 #endif  // COMPONENTS_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_BASE_H_