[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / content / shell / shell_web_contents_view_delegate_win.cc
blobaf173d278b405514f05668bff58c69da4c6ae8a1
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.
5 #include "content/shell/shell_web_contents_view_delegate.h"
7 #include "base/command_line.h"
8 #include "content/public/browser/devtools_http_handler.h"
9 #include "content/public/browser/render_process_host.h"
10 #include "content/public/browser/render_view_host.h"
11 #include "content/public/browser/render_widget_host_view.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/browser/web_contents_view.h"
14 #include "content/public/common/context_menu_params.h"
15 #include "content/shell/shell.h"
16 #include "content/shell/shell_browser_context.h"
17 #include "content/shell/shell_browser_main_parts.h"
18 #include "content/shell/shell_content_browser_client.h"
19 #include "content/shell/shell_devtools_delegate.h"
20 #include "content/shell/shell_switches.h"
21 #include "content/shell/shell_web_contents_view_delegate_creator.h"
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h"
24 using WebKit::WebContextMenuData;
26 namespace {
28 enum {
29 ShellContextMenuItemCutId = 10001,
30 ShellContextMenuItemCopyId,
31 ShellContextMenuItemPasteId,
32 ShellContextMenuItemDeleteId,
33 ShellContextMenuItemOpenLinkId,
34 ShellContextMenuItemBackId,
35 ShellContextMenuItemForwardId,
36 ShellContextMenuItemReloadId,
37 ShellContextMenuItemInspectId
40 void MakeContextMenuItem(HMENU menu,
41 int menu_index,
42 LPTSTR text,
43 UINT id,
44 bool enabled) {
45 MENUITEMINFO mii = {0};
46 mii.cbSize = sizeof(mii);
47 mii.fMask = MIIM_FTYPE | MIIM_ID | MIIM_DATA | MIIM_STRING | MIIM_STATE;
48 mii.fState = enabled ? MFS_ENABLED : (MF_DISABLED | MFS_GRAYED);
49 mii.fType = MFT_STRING;
50 mii.wID = id;
51 mii.dwTypeData = text;
53 InsertMenuItem(menu, menu_index, TRUE, &mii);
56 } // namespace
58 namespace content {
60 WebContentsViewDelegate* CreateShellWebContentsViewDelegate(
61 WebContents* web_contents) {
62 return new ShellWebContentsViewDelegate(web_contents);
65 ShellWebContentsViewDelegate::ShellWebContentsViewDelegate(
66 WebContents* web_contents)
67 : web_contents_(web_contents) {
70 ShellWebContentsViewDelegate::~ShellWebContentsViewDelegate() {
73 void ShellWebContentsViewDelegate::ShowContextMenu(
74 const ContextMenuParams& params,
75 ContextMenuSourceType type) {
76 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree))
77 return;
79 params_ = params;
80 bool has_link = !params_.unfiltered_link_url.is_empty();
81 bool has_selection = !params_.selection_text.empty();
83 HMENU menu = CreateMenu();
84 HMENU sub_menu = CreatePopupMenu();
85 AppendMenu(menu, MF_STRING | MF_POPUP, (UINT)sub_menu, L"");
87 int index = 0;
88 if (params_.media_type == WebContextMenuData::MediaTypeNone &&
89 !has_link &&
90 !has_selection &&
91 !params_.is_editable) {
92 MakeContextMenuItem(sub_menu,
93 index++,
94 L"Back",
95 ShellContextMenuItemBackId,
96 web_contents_->GetController().CanGoBack());
98 MakeContextMenuItem(sub_menu,
99 index++,
100 L"Forward",
101 ShellContextMenuItemForwardId,
102 web_contents_->GetController().CanGoForward());
104 MakeContextMenuItem(sub_menu,
105 index++,
106 L"Reload",
107 ShellContextMenuItemReloadId,
108 true);
110 AppendMenu(sub_menu, MF_SEPARATOR, 0, NULL);
111 index++;
114 if (has_link) {
115 MakeContextMenuItem(sub_menu,
116 index++,
117 L"Open in New Window",
118 ShellContextMenuItemOpenLinkId,
119 true);
120 AppendMenu(sub_menu, MF_SEPARATOR, 0, NULL);
121 index++;
124 if (params_.is_editable) {
125 bool cut_enabled = ((params_.edit_flags & WebContextMenuData::CanCut) != 0);
126 MakeContextMenuItem(sub_menu,
127 index++,
128 L"Cut",
129 ShellContextMenuItemCutId,
130 cut_enabled);
132 bool copy_enabled =
133 ((params_.edit_flags & WebContextMenuData::CanCopy) != 0);
134 MakeContextMenuItem(sub_menu,
135 index++,
136 L"Copy",
137 ShellContextMenuItemCopyId,
138 copy_enabled);
140 bool paste_enabled =
141 ((params_.edit_flags & WebContextMenuData::CanPaste) != 0);
142 MakeContextMenuItem(sub_menu,
143 index++,
144 L"Paste",
145 ShellContextMenuItemPasteId,
146 paste_enabled);
147 bool delete_enabled =
148 ((params_.edit_flags & WebContextMenuData::CanDelete) != 0);
149 MakeContextMenuItem(sub_menu,
150 index++,
151 L"Delete",
152 ShellContextMenuItemDeleteId,
153 delete_enabled);
155 AppendMenu(sub_menu, MF_SEPARATOR, 0, NULL);
156 index++;
157 } else if (has_selection) {
158 MakeContextMenuItem(sub_menu,
159 index++,
160 L"Copy",
161 ShellContextMenuItemCopyId,
162 true);
164 AppendMenu(sub_menu, MF_SEPARATOR, 0, NULL);
165 index++;
168 MakeContextMenuItem(sub_menu,
169 index++,
170 L"Inspect...",
171 ShellContextMenuItemInspectId,
172 true);
173 #if defined(USE_AURA)
174 NOTIMPLEMENTED();
175 #else
176 gfx::Point screen_point(params.x, params.y);
177 POINT point = screen_point.ToPOINT();
178 ClientToScreen(web_contents_->GetView()->GetNativeView(), &point);
180 int selection =
181 TrackPopupMenu(sub_menu,
182 TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD,
183 point.x, point.y,
185 web_contents_->GetView()->GetContentNativeView(),
186 NULL);
188 MenuItemSelected(selection);
189 #endif
190 DestroyMenu(menu);
193 void ShellWebContentsViewDelegate::MenuItemSelected(int selection) {
194 switch (selection) {
195 case ShellContextMenuItemCutId:
196 web_contents_->GetRenderViewHost()->Cut();
197 break;
198 case ShellContextMenuItemCopyId:
199 web_contents_->GetRenderViewHost()->Copy();
200 break;
201 case ShellContextMenuItemPasteId:
202 web_contents_->GetRenderViewHost()->Paste();
203 break;
204 case ShellContextMenuItemDeleteId:
205 web_contents_->GetRenderViewHost()->Delete();
206 break;
207 case ShellContextMenuItemOpenLinkId: {
208 ShellBrowserContext* browser_context =
209 static_cast<ShellContentBrowserClient*>(
210 GetContentClient()->browser())->browser_context();
211 Shell::CreateNewWindow(browser_context,
212 params_.link_url,
213 NULL,
214 MSG_ROUTING_NONE,
215 NULL);
216 break;
218 case ShellContextMenuItemBackId:
219 web_contents_->GetController().GoToOffset(-1);
220 web_contents_->Focus();
221 break;
222 case ShellContextMenuItemForwardId:
223 web_contents_->GetController().GoToOffset(1);
224 web_contents_->Focus();
225 break;
226 case ShellContextMenuItemReloadId:
227 web_contents_->GetController().Reload(false);
228 web_contents_->Focus();
229 break;
230 case ShellContextMenuItemInspectId: {
231 ShellContentBrowserClient* browser_client =
232 static_cast<ShellContentBrowserClient*>(
233 GetContentClient()->browser());
234 ShellDevToolsDelegate* delegate =
235 browser_client->shell_browser_main_parts()->devtools_delegate();
236 GURL url = delegate->devtools_http_handler()->GetFrontendURL(
237 web_contents_->GetRenderViewHost());
238 Shell::CreateNewWindow(web_contents_->GetBrowserContext(),
239 url,
240 NULL,
241 MSG_ROUTING_NONE,
242 NULL);
243 break;
248 WebDragDestDelegate* ShellWebContentsViewDelegate::GetDragDestDelegate() {
249 return NULL;
252 void ShellWebContentsViewDelegate::StoreFocus() {
255 void ShellWebContentsViewDelegate::RestoreFocus() {
258 bool ShellWebContentsViewDelegate::Focus() {
259 return false;
262 void ShellWebContentsViewDelegate::TakeFocus(bool reverse) {
265 void ShellWebContentsViewDelegate::SizeChanged(const gfx::Size& size) {
268 } // namespace content