Commit 6eb57fb7 authored by mohsen's avatar mohsen Committed by Commit bot

Move content shell context menu to ShellWebContentsViewDelegate in Views

Allows content shell to run context menu handling code in
WebContentsViewAura, including touch selection code. This is needed to
be able to properly test long press touch selection behavior.

BUG=594101

Review-Url: https://codereview.chromium.org/2132983002
Cr-Commit-Position: refs/heads/master@{#407008}
parent a4710216
...@@ -196,9 +196,10 @@ ...@@ -196,9 +196,10 @@
'shell/browser/shell_views.cc', 'shell/browser/shell_views.cc',
'shell/browser/shell_web_contents_view_delegate.h', 'shell/browser/shell_web_contents_view_delegate.h',
'shell/browser/shell_web_contents_view_delegate_android.cc', 'shell/browser/shell_web_contents_view_delegate_android.cc',
'shell/browser/shell_web_contents_view_delegate_aura.cc',
'shell/browser/shell_web_contents_view_delegate_creator.h', 'shell/browser/shell_web_contents_view_delegate_creator.h',
'shell/browser/shell_web_contents_view_delegate_mac.mm', 'shell/browser/shell_web_contents_view_delegate_mac.mm',
'shell/browser/shell_web_contents_view_delegate_win.cc', 'shell/browser/shell_web_contents_view_delegate_views.cc',
'shell/common/layout_test/layout_test_content_client.cc', 'shell/common/layout_test/layout_test_content_client.cc',
'shell/common/layout_test/layout_test_content_client.h', 'shell/common/layout_test/layout_test_content_client.h',
'shell/common/layout_test/layout_test_messages.cc', 'shell/common/layout_test/layout_test_messages.cc',
...@@ -313,10 +314,12 @@ ...@@ -313,10 +314,12 @@
], ],
'sources/': [ 'sources/': [
['exclude', 'shell/browser/shell_aura.cc'], ['exclude', 'shell/browser/shell_aura.cc'],
['exclude', 'shell/browser/shell_web_contents_view_delegate_aura.cc'],
], ],
}, { }, {
'sources/': [ 'sources/': [
['exclude', 'shell/browser/shell_views.cc'], ['exclude', 'shell/browser/shell_views.cc'],
['exclude', 'shell/browser/shell_web_contents_view_delegate_views.cc'],
], ],
}], }],
], ],
...@@ -324,6 +327,8 @@ ...@@ -324,6 +327,8 @@
'sources/': [ 'sources/': [
['exclude', 'shell/browser/shell_aura.cc'], ['exclude', 'shell/browser/shell_aura.cc'],
['exclude', 'shell/browser/shell_views.cc'], ['exclude', 'shell/browser/shell_views.cc'],
['exclude', 'shell/browser/shell_web_contents_view_delegate_aura.cc'],
['exclude', 'shell/browser/shell_web_contents_view_delegate_views.cc'],
], ],
}], # use_aura==1 }], # use_aura==1
['chromeos==1', { ['chromeos==1', {
......
...@@ -151,7 +151,6 @@ static_library("content_shell_lib") { ...@@ -151,7 +151,6 @@ static_library("content_shell_lib") {
"browser/shell_web_contents_view_delegate_android.cc", "browser/shell_web_contents_view_delegate_android.cc",
"browser/shell_web_contents_view_delegate_creator.h", "browser/shell_web_contents_view_delegate_creator.h",
"browser/shell_web_contents_view_delegate_mac.mm", "browser/shell_web_contents_view_delegate_mac.mm",
"browser/shell_web_contents_view_delegate_win.cc",
"common/layout_test/layout_test_content_client.cc", "common/layout_test/layout_test_content_client.cc",
"common/layout_test/layout_test_content_client.h", "common/layout_test/layout_test_content_client.h",
"common/layout_test/layout_test_messages.cc", "common/layout_test/layout_test_messages.cc",
...@@ -328,7 +327,10 @@ static_library("content_shell_lib") { ...@@ -328,7 +327,10 @@ static_library("content_shell_lib") {
] ]
if (toolkit_views) { if (toolkit_views) {
sources += [ "browser/shell_views.cc" ] sources += [
"browser/shell_views.cc",
"browser/shell_web_contents_view_delegate_views.cc",
]
deps += [ deps += [
"//ui/resources", "//ui/resources",
"//ui/views", "//ui/views",
...@@ -337,7 +339,10 @@ static_library("content_shell_lib") { ...@@ -337,7 +339,10 @@ static_library("content_shell_lib") {
"//ui/wm:test_support", "//ui/wm:test_support",
] ]
} else { } else {
sources += [ "browser/shell_aura.cc" ] sources += [
"browser/shell_aura.cc",
"browser/shell_web_contents_view_delegate_aura.cc",
]
} }
} else { } else {
sources -= [ sources -= [
......
...@@ -286,12 +286,14 @@ void Shell::UpdateNavigationControls(bool to_different_document) { ...@@ -286,12 +286,14 @@ void Shell::UpdateNavigationControls(bool to_different_document) {
} }
void Shell::ShowDevTools() { void Shell::ShowDevTools() {
InnerShowDevTools(); if (!devtools_frontend_) {
} devtools_frontend_ = ShellDevToolsFrontend::Show(web_contents());
devtools_observer_.reset(new DevToolsWebContentsObserver(
this, devtools_frontend_->frontend_shell()->web_contents()));
}
void Shell::ShowDevToolsForElementAt(int x, int y) { devtools_frontend_->Activate();
InnerShowDevTools(); devtools_frontend_->Focus();
devtools_frontend_->InspectElementAt(x, y);
} }
void Shell::CloseDevTools() { void Shell::CloseDevTools() {
...@@ -438,10 +440,6 @@ void Shell::ActivateContents(WebContents* contents) { ...@@ -438,10 +440,6 @@ void Shell::ActivateContents(WebContents* contents) {
contents->GetRenderViewHost()->GetWidget()->Focus(); contents->GetRenderViewHost()->GetWidget()->Focus();
} }
bool Shell::HandleContextMenu(const content::ContextMenuParams& params) {
return PlatformHandleContextMenu(params);
}
gfx::Size Shell::GetShellDefaultSize() { gfx::Size Shell::GetShellDefaultSize() {
static gfx::Size default_shell_size; static gfx::Size default_shell_size;
if (!default_shell_size.IsEmpty()) if (!default_shell_size.IsEmpty())
...@@ -465,17 +463,6 @@ void Shell::TitleWasSet(NavigationEntry* entry, bool explicit_set) { ...@@ -465,17 +463,6 @@ void Shell::TitleWasSet(NavigationEntry* entry, bool explicit_set) {
PlatformSetTitle(entry->GetTitle()); PlatformSetTitle(entry->GetTitle());
} }
void Shell::InnerShowDevTools() {
if (!devtools_frontend_) {
devtools_frontend_ = ShellDevToolsFrontend::Show(web_contents());
devtools_observer_.reset(new DevToolsWebContentsObserver(
this, devtools_frontend_->frontend_shell()->web_contents()));
}
devtools_frontend_->Activate();
devtools_frontend_->Focus();
}
void Shell::OnDevToolsWebContentsDestroyed() { void Shell::OnDevToolsWebContentsDestroyed() {
devtools_observer_.reset(); devtools_observer_.reset();
devtools_frontend_ = NULL; devtools_frontend_ = NULL;
......
...@@ -76,7 +76,6 @@ class Shell : public WebContentsDelegate, ...@@ -76,7 +76,6 @@ class Shell : public WebContentsDelegate,
void UpdateNavigationControls(bool to_different_document); void UpdateNavigationControls(bool to_different_document);
void Close(); void Close();
void ShowDevTools(); void ShowDevTools();
void ShowDevToolsForElementAt(int x, int y);
void CloseDevTools(); void CloseDevTools();
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
// Resizes the web content view to the given dimensions. // Resizes the web content view to the given dimensions.
...@@ -159,7 +158,6 @@ class Shell : public WebContentsDelegate, ...@@ -159,7 +158,6 @@ class Shell : public WebContentsDelegate,
const base::string16& source_id) override; const base::string16& source_id) override;
void RendererUnresponsive(WebContents* source) override; void RendererUnresponsive(WebContents* source) override;
void ActivateContents(WebContents* contents) override; void ActivateContents(WebContents* contents) override;
bool HandleContextMenu(const content::ContextMenuParams& params) override;
static gfx::Size GetShellDefaultSize(); static gfx::Size GetShellDefaultSize();
...@@ -205,8 +203,6 @@ class Shell : public WebContentsDelegate, ...@@ -205,8 +203,6 @@ class Shell : public WebContentsDelegate,
void PlatformSetIsLoading(bool loading); void PlatformSetIsLoading(bool loading);
// Set the title of shell window // Set the title of shell window
void PlatformSetTitle(const base::string16& title); void PlatformSetTitle(const base::string16& title);
// User right-clicked on the web view
bool PlatformHandleContextMenu(const content::ContextMenuParams& params);
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
void PlatformToggleFullscreenModeForTab(WebContents* web_contents, void PlatformToggleFullscreenModeForTab(WebContents* web_contents,
bool enter_fullscreen); bool enter_fullscreen);
...@@ -227,7 +223,6 @@ class Shell : public WebContentsDelegate, ...@@ -227,7 +223,6 @@ class Shell : public WebContentsDelegate,
// WebContentsObserver // WebContentsObserver
void TitleWasSet(NavigationEntry* entry, bool explicit_set) override; void TitleWasSet(NavigationEntry* entry, bool explicit_set) override;
void InnerShowDevTools();
void OnDevToolsWebContentsDestroyed(); void OnDevToolsWebContentsDestroyed();
std::unique_ptr<ShellJavaScriptDialogManager> dialog_manager_; std::unique_ptr<ShellJavaScriptDialogManager> dialog_manager_;
......
...@@ -88,11 +88,6 @@ bool Shell::PlatformIsFullscreenForTabOrPending( ...@@ -88,11 +88,6 @@ bool Shell::PlatformIsFullscreenForTabOrPending(
return Java_Shell_isFullscreenForTabOrPending(env, java_object_.obj()); return Java_Shell_isFullscreenForTabOrPending(env, java_object_.obj());
} }
bool Shell::PlatformHandleContextMenu(
const content::ContextMenuParams& params) {
return false;
}
void Shell::Close() { void Shell::Close() {
RemoveShellView(java_object_.obj()); RemoveShellView(java_object_.obj());
delete this; delete this;
......
...@@ -66,9 +66,4 @@ void Shell::Close() { ...@@ -66,9 +66,4 @@ void Shell::Close() {
void Shell::PlatformSetTitle(const base::string16& title) { void Shell::PlatformSetTitle(const base::string16& title) {
} }
bool Shell::PlatformHandleContextMenu(
const content::ContextMenuParams& params) {
return false;
}
} // namespace content } // namespace content
...@@ -256,11 +256,7 @@ std::string ShellContentBrowserClient::GetDefaultDownloadName() { ...@@ -256,11 +256,7 @@ std::string ShellContentBrowserClient::GetDefaultDownloadName() {
WebContentsViewDelegate* ShellContentBrowserClient::GetWebContentsViewDelegate( WebContentsViewDelegate* ShellContentBrowserClient::GetWebContentsViewDelegate(
WebContents* web_contents) { WebContents* web_contents) {
#if !defined(USE_AURA)
return CreateShellWebContentsViewDelegate(web_contents); return CreateShellWebContentsViewDelegate(web_contents);
#else
return NULL;
#endif
} }
QuotaPermissionContext* QuotaPermissionContext*
......
...@@ -274,11 +274,6 @@ void Shell::PlatformSetTitle(const base::string16& title) { ...@@ -274,11 +274,6 @@ void Shell::PlatformSetTitle(const base::string16& title) {
[window_ setTitle:title_string]; [window_ setTitle:title_string];
} }
bool Shell::PlatformHandleContextMenu(
const content::ContextMenuParams& params) {
return false;
}
void Shell::Close() { void Shell::Close() {
if (headless_) if (headless_)
delete this; delete this;
......
...@@ -13,22 +13,16 @@ ...@@ -13,22 +13,16 @@
#include "content/public/browser/context_factory.h" #include "content/public/browser/context_factory.h"
#include "content/public/browser/render_widget_host_view.h" #include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/common/context_menu_params.h"
#include "content/shell/browser/shell_platform_data_aura.h" #include "content/shell/browser/shell_platform_data_aura.h"
#include "ui/aura/client/screen_position_client.h"
#include "ui/aura/env.h" #include "ui/aura/env.h"
#include "ui/aura/window.h" #include "ui/aura/window.h"
#include "ui/aura/window_event_dispatcher.h" #include "ui/aura/window_event_dispatcher.h"
#include "ui/base/clipboard/clipboard.h" #include "ui/base/clipboard/clipboard.h"
#include "ui/base/models/simple_menu_model.h"
#include "ui/base/resource/resource_bundle.h" #include "ui/base/resource/resource_bundle.h"
#include "ui/display/screen.h" #include "ui/display/screen.h"
#include "ui/events/event.h" #include "ui/events/event.h"
#include "ui/views/background.h" #include "ui/views/background.h"
#include "ui/views/controls/button/label_button.h" #include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/button/menu_button.h"
#include "ui/views/controls/button/menu_button_listener.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/controls/textfield/textfield.h" #include "ui/views/controls/textfield/textfield.h"
#include "ui/views/controls/textfield/textfield_controller.h" #include "ui/views/controls/textfield/textfield_controller.h"
#include "ui/views/controls/webview/webview.h" #include "ui/views/controls/webview/webview.h"
...@@ -55,44 +49,6 @@ namespace content { ...@@ -55,44 +49,6 @@ namespace content {
namespace { namespace {
// Model for the "Debug" menu
class ContextMenuModel : public ui::SimpleMenuModel,
public ui::SimpleMenuModel::Delegate {
public:
explicit ContextMenuModel(
Shell* shell, const content::ContextMenuParams& params)
: ui::SimpleMenuModel(this),
shell_(shell),
params_(params) {
AddItem(COMMAND_OPEN_DEVTOOLS, base::ASCIIToUTF16("Inspect Element"));
}
// ui::SimpleMenuModel::Delegate:
bool IsCommandIdChecked(int command_id) const override { return false; }
bool IsCommandIdEnabled(int command_id) const override { return true; }
bool GetAcceleratorForCommandId(int command_id,
ui::Accelerator* accelerator) const override {
return false;
}
void ExecuteCommand(int command_id, int event_flags) override {
switch (command_id) {
case COMMAND_OPEN_DEVTOOLS:
shell_->ShowDevToolsForElementAt(params_.x, params_.y);
break;
};
}
private:
enum CommandID {
COMMAND_OPEN_DEVTOOLS
};
Shell* shell_;
content::ContextMenuParams params_;
DISALLOW_COPY_AND_ASSIGN(ContextMenuModel);
};
// Maintain the UI controls and web view for content shell // Maintain the UI controls and web view for content shell
class ShellWindowDelegateView : public views::WidgetDelegateView, class ShellWindowDelegateView : public views::WidgetDelegateView,
public views::TextfieldController, public views::TextfieldController,
...@@ -150,35 +106,6 @@ class ShellWindowDelegateView : public views::WidgetDelegateView, ...@@ -150,35 +106,6 @@ class ShellWindowDelegateView : public views::WidgetDelegateView,
} }
} }
void ShowWebViewContextMenu(const content::ContextMenuParams& params) {
gfx::Point screen_point(params.x, params.y);
// Convert from content coordinates to window coordinates.
// This code copied from chrome_web_contents_view_delegate_views.cc
aura::Window* web_contents_window =
shell_->web_contents()->GetNativeView();
aura::Window* root_window = web_contents_window->GetRootWindow();
aura::client::ScreenPositionClient* screen_position_client =
aura::client::GetScreenPositionClient(root_window);
if (screen_position_client) {
screen_position_client->ConvertPointToScreen(web_contents_window,
&screen_point);
}
context_menu_model_.reset(new ContextMenuModel(shell_, params));
context_menu_runner_.reset(new views::MenuRunner(
context_menu_model_.get(), views::MenuRunner::CONTEXT_MENU));
if (context_menu_runner_->RunMenuAt(web_view_->GetWidget(),
NULL,
gfx::Rect(screen_point, gfx::Size()),
views::MENU_ANCHOR_TOPRIGHT,
ui::MENU_SOURCE_NONE) ==
views::MenuRunner::MENU_DELETED) {
return;
}
}
private: private:
// Initialize the UI control contained in shell window // Initialize the UI control contained in shell window
void InitShellWindow() { void InitShellWindow() {
...@@ -372,8 +299,6 @@ class ShellWindowDelegateView : public views::WidgetDelegateView, ...@@ -372,8 +299,6 @@ class ShellWindowDelegateView : public views::WidgetDelegateView,
views::LabelButton* refresh_button_; views::LabelButton* refresh_button_;
views::LabelButton* stop_button_; views::LabelButton* stop_button_;
views::Textfield* url_entry_; views::Textfield* url_entry_;
std::unique_ptr<ContextMenuModel> context_menu_model_;
std::unique_ptr<views::MenuRunner> context_menu_runner_;
// Contents view contains the web contents view // Contents view contains the web contents view
View* contents_view_; View* contents_view_;
...@@ -525,14 +450,4 @@ void Shell::PlatformSetTitle(const base::string16& title) { ...@@ -525,14 +450,4 @@ void Shell::PlatformSetTitle(const base::string16& title) {
window_widget_->UpdateWindowTitle(); window_widget_->UpdateWindowTitle();
} }
bool Shell::PlatformHandleContextMenu(
const content::ContextMenuParams& params) {
if (headless_)
return true;
ShellWindowDelegateView* delegate_view =
static_cast<ShellWindowDelegateView*>(window_widget_->widget_delegate());
delegate_view->ShowWebViewContextMenu(params);
return true;
}
} // namespace content } // namespace content
...@@ -5,12 +5,22 @@ ...@@ -5,12 +5,22 @@
#ifndef CONTENT_SHELL_BROWSER_SHELL_WEB_CONTENTS_VIEW_DELEGATE_H_ #ifndef CONTENT_SHELL_BROWSER_SHELL_WEB_CONTENTS_VIEW_DELEGATE_H_
#define CONTENT_SHELL_BROWSER_SHELL_WEB_CONTENTS_VIEW_DELEGATE_H_ #define CONTENT_SHELL_BROWSER_SHELL_WEB_CONTENTS_VIEW_DELEGATE_H_
#include <memory>
#include "base/macros.h" #include "base/macros.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view_delegate.h" #include "content/public/browser/web_contents_view_delegate.h"
#include "content/public/common/context_menu_params.h" #include "content/public/common/context_menu_params.h"
namespace ui {
class SimpleMenuModel;
}
namespace views {
class MenuRunner;
}
namespace content { namespace content {
class ShellWebContentsViewDelegate : public WebContentsViewDelegate { class ShellWebContentsViewDelegate : public WebContentsViewDelegate {
...@@ -24,13 +34,18 @@ class ShellWebContentsViewDelegate : public WebContentsViewDelegate { ...@@ -24,13 +34,18 @@ class ShellWebContentsViewDelegate : public WebContentsViewDelegate {
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
void ActionPerformed(int id); void ActionPerformed(int id);
#elif defined(OS_WIN)
void MenuItemSelected(int selection);
#endif #endif
private: private:
WebContents* web_contents_; WebContents* web_contents_;
#if defined(OS_MACOSX)
ContextMenuParams params_; ContextMenuParams params_;
#endif
#if defined(TOOLKIT_VIEWS)
std::unique_ptr<ui::SimpleMenuModel> context_menu_model_;
std::unique_ptr<views::MenuRunner> context_menu_runner_;
#endif
DISALLOW_COPY_AND_ASSIGN(ShellWebContentsViewDelegate); DISALLOW_COPY_AND_ASSIGN(ShellWebContentsViewDelegate);
}; };
......
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
namespace content {
class WebContents;
class WebContentsViewDelegate;
WebContentsViewDelegate* CreateShellWebContentsViewDelegate(
WebContents* web_contents) {
return nullptr;
}
} // namespace content
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/shell/browser/shell_web_contents_view_delegate.h"
#include "base/strings/utf_string_conversions.h"
#include "content/public/browser/web_contents.h"
#include "content/shell/browser/shell_devtools_frontend.h"
#include "content/shell/common/shell_switches.h"
#include "ui/aura/client/screen_position_client.h"
#include "ui/aura/window.h"
#include "ui/base/models/simple_menu_model.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/widget/widget.h"
namespace content {
namespace {
// Model for the "Debug" menu
class ContextMenuModel : public ui::SimpleMenuModel,
public ui::SimpleMenuModel::Delegate {
public:
ContextMenuModel(WebContents* web_contents, const ContextMenuParams& params)
: ui::SimpleMenuModel(this),
web_contents_(web_contents),
params_(params) {
AddItem(COMMAND_OPEN_DEVTOOLS, base::ASCIIToUTF16("Inspect Element"));
}
~ContextMenuModel() override {}
// ui::SimpleMenuModel::Delegate:
bool IsCommandIdChecked(int command_id) const override { return false; }
bool IsCommandIdEnabled(int command_id) const override { return true; }
bool GetAcceleratorForCommandId(int command_id,
ui::Accelerator* accelerator) const override {
return false;
}
void ExecuteCommand(int command_id, int event_flags) override {
switch (command_id) {
case COMMAND_OPEN_DEVTOOLS:
ShellDevToolsFrontend* devtools_frontend =
ShellDevToolsFrontend::Show(web_contents_);
devtools_frontend->Activate();
devtools_frontend->Focus();
devtools_frontend->InspectElementAt(params_.x, params_.y);
break;
};
}
private:
enum CommandID { COMMAND_OPEN_DEVTOOLS };
WebContents* web_contents_;
ContextMenuParams params_;
DISALLOW_COPY_AND_ASSIGN(ContextMenuModel);
};
} // namespace
WebContentsViewDelegate* CreateShellWebContentsViewDelegate(
WebContents* web_contents) {
return new ShellWebContentsViewDelegate(web_contents);
}
ShellWebContentsViewDelegate::ShellWebContentsViewDelegate(
WebContents* web_contents)
: web_contents_(web_contents) {}
ShellWebContentsViewDelegate::~ShellWebContentsViewDelegate() {}
void ShellWebContentsViewDelegate::ShowContextMenu(
RenderFrameHost* render_frame_host,
const ContextMenuParams& params) {
if (switches::IsRunLayoutTestSwitchPresent())
return;
gfx::Point screen_point(params.x, params.y);
// Convert from content coordinates to window coordinates.
// This code copied from chrome_web_contents_view_delegate_views.cc
aura::Window* web_contents_window = web_contents_->GetNativeView();
aura::Window* root_window = web_contents_window->GetRootWindow();
aura::client::ScreenPositionClient* screen_position_client =
aura::client::GetScreenPositionClient(root_window);
if (screen_position_client) {
screen_position_client->ConvertPointToScreen(web_contents_window,
&screen_point);
}
context_menu_model_.reset(new ContextMenuModel(web_contents_, params));
context_menu_runner_.reset(new views::MenuRunner(
context_menu_model_.get(), views::MenuRunner::CONTEXT_MENU));
views::Widget* widget = views::Widget::GetWidgetForNativeView(
web_contents_->GetTopLevelNativeWindow());
if (context_menu_runner_->RunMenuAt(
widget, nullptr, gfx::Rect(screen_point, gfx::Size()),
views::MENU_ANCHOR_TOPRIGHT,
ui::MENU_SOURCE_NONE) == views::MenuRunner::MENU_DELETED) {
return;
}
}
} // namespace content
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/shell/browser/shell_web_contents_view_delegate.h"
#include "base/command_line.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/context_menu_params.h"
#include "content/shell/browser/shell.h"
#include "content/shell/browser/shell_browser_context.h"
#include "content/shell/browser/shell_browser_main_parts.h"
#include "content/shell/browser/shell_content_browser_client.h"
#include "content/shell/browser/shell_devtools_frontend.h"
#include "content/shell/browser/shell_web_contents_view_delegate_creator.h"
#include "content/shell/common/shell_switches.h"
#include "third_party/WebKit/public/web/WebContextMenuData.h"
using blink::WebContextMenuData;
namespace {
enum {
ShellContextMenuItemCutId = 10001,
ShellContextMenuItemCopyId,
ShellContextMenuItemPasteId,
ShellContextMenuItemDeleteId,
ShellContextMenuItemOpenLinkId,
ShellContextMenuItemBackId,
ShellContextMenuItemForwardId,
ShellContextMenuItemReloadId,
ShellContextMenuItemInspectId
};
void MakeContextMenuItem(HMENU menu,
int menu_index,
LPCTSTR text,
UINT id,
bool enabled) {
MENUITEMINFO mii = {0};
mii.cbSize = sizeof(mii);
mii.fMask = MIIM_FTYPE | MIIM_ID | MIIM_DATA | MIIM_STRING | MIIM_STATE;
mii.fState = enabled ? MFS_ENABLED : (MF_DISABLED | MFS_GRAYED);
mii.fType = MFT_STRING;
mii.wID = id;
mii.dwTypeData = const_cast<LPTSTR>(text);
InsertMenuItem(menu, menu_index, TRUE, &mii);
}
} // namespace
namespace content {
WebContentsViewDelegate* CreateShellWebContentsViewDelegate(
WebContents* web_contents) {
return new ShellWebContentsViewDelegate(web_contents);
}
ShellWebContentsViewDelegate::ShellWebContentsViewDelegate(
WebContents* web_contents)
: web_contents_(web_contents) {
}
ShellWebContentsViewDelegate::~ShellWebContentsViewDelegate() {
}
void ShellWebContentsViewDelegate::ShowContextMenu(
RenderFrameHost* render_frame_host,
const ContextMenuParams& params) {
if (switches::IsRunLayoutTestSwitchPresent())
return;
params_ = params;
bool has_link = !params_.unfiltered_link_url.is_empty();
bool has_selection = !params_.selection_text.empty();
HMENU menu = CreateMenu();
HMENU sub_menu = CreatePopupMenu();
AppendMenu(menu, MF_STRING | MF_POPUP, reinterpret_cast<UINT_PTR>(sub_menu),
L"");
int index = 0;
if (params_.media_type == WebContextMenuData::MediaTypeNone &&
!has_link &&
!has_selection &&
!params_.is_editable) {
MakeContextMenuItem(sub_menu,
index++,
L"Back",
ShellContextMenuItemBackId,
web_contents_->GetController().CanGoBack());
MakeContextMenuItem(sub_menu,
index++,
L"Forward",
ShellContextMenuItemForwardId,
web_contents_->GetController().CanGoForward());
MakeContextMenuItem(sub_menu,
index++,
L"Reload",
ShellContextMenuItemReloadId,
true);
AppendMenu(sub_menu, MF_SEPARATOR, 0, NULL);
index++;
}
if (has_link) {
MakeContextMenuItem(sub_menu,
index++,
L"Open in New Window",
ShellContextMenuItemOpenLinkId,
true);
AppendMenu(sub_menu, MF_SEPARATOR, 0, NULL);
index++;
}
if (params_.is_editable) {
bool cut_enabled = ((params_.edit_flags & WebContextMenuData::CanCut) != 0);
MakeContextMenuItem(sub_menu,
index++,
L"Cut",
ShellContextMenuItemCutId,
cut_enabled);
bool copy_enabled =
((params_.edit_flags & WebContextMenuData::CanCopy) != 0);
MakeContextMenuItem(sub_menu,
index++,
L"Copy",
ShellContextMenuItemCopyId,
copy_enabled);
bool paste_enabled =
((params_.edit_flags & WebContextMenuData::CanPaste) != 0);
MakeContextMenuItem(sub_menu,
index++,
L"Paste",
ShellContextMenuItemPasteId,
paste_enabled);
bool delete_enabled =
((params_.edit_flags & WebContextMenuData::CanDelete) != 0);
MakeContextMenuItem(sub_menu,
index++,
L"Delete",
ShellContextMenuItemDeleteId,
delete_enabled);
AppendMenu(sub_menu, MF_SEPARATOR, 0, NULL);
index++;
} else if (has_selection) {
MakeContextMenuItem(sub_menu,
index++,
L"Copy",
ShellContextMenuItemCopyId,
true);
AppendMenu(sub_menu, MF_SEPARATOR, 0, NULL);
index++;
}
MakeContextMenuItem(sub_menu,
index++,
L"Inspect...",
ShellContextMenuItemInspectId,
true);
NOTIMPLEMENTED();
DestroyMenu(menu);
}
void ShellWebContentsViewDelegate::MenuItemSelected(int selection) {
switch (selection) {
case ShellContextMenuItemCutId:
web_contents_->Cut();
break;
case ShellContextMenuItemCopyId:
web_contents_->Copy();
break;
case ShellContextMenuItemPasteId:
web_contents_->Paste();
break;
case ShellContextMenuItemDeleteId:
web_contents_->Delete();
break;
case ShellContextMenuItemOpenLinkId: {
ShellBrowserContext* browser_context =
ShellContentBrowserClient::Get()->browser_context();
Shell::CreateNewWindow(browser_context,
params_.link_url,
NULL,
gfx::Size());
break;
}
case ShellContextMenuItemBackId:
web_contents_->GetController().GoToOffset(-1);
web_contents_->Focus();
break;
case ShellContextMenuItemForwardId:
web_contents_->GetController().GoToOffset(1);
web_contents_->Focus();
break;
case ShellContextMenuItemReloadId:
web_contents_->GetController().Reload(false);
web_contents_->Focus();
break;
case ShellContextMenuItemInspectId: {
ShellDevToolsFrontend::Show(web_contents_);
break;
}
}
}
} // namespace content
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment