Skip to content

Commit

Permalink
1st step to C14N RendererContextMenu
Browse files Browse the repository at this point in the history
* Move context_menu_delegate/renderer_view_context_menu_observer
* RenderViewContextMenuProxy has been extracted from render_view_context_menu.h and moved to component directory.
* Inline empty observer methods.
* Reduce the use of Profile and replaced them with content::BrowserContext

BUG=397320
TBR=sky@chromium.org

Review URL: https://codereview.chromium.org/425493002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@286149 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
oshima@chromium.org committed Jul 29, 2014
1 parent 108d19a commit 0a83f14
Show file tree
Hide file tree
Showing 25 changed files with 283 additions and 217 deletions.
1 change: 1 addition & 0 deletions chrome/browser/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ include_rules = [
"+components/pref_registry",
"+components/query_parser",
"+components/rappor",
"+components/renderer_context_menu",
"+components/search",
"+components/search_engines",
"+components/search_provider_logos",
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/guest_view/app_view/app_view_guest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/guest_view/app_view/app_view_constants.h"
#include "chrome/browser/guest_view/guest_view_manager.h"
#include "chrome/browser/renderer_context_menu/context_menu_delegate.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
#include "chrome/common/chrome_switches.h"
#include "components/renderer_context_menu/context_menu_delegate.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/common/renderer_preferences.h"
#include "extensions/browser/api/app_runtime/app_runtime_api.h"
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/guest_view/web_view/web_view_guest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
#include "chrome/browser/guest_view/web_view/web_view_permission_helper.h"
#include "chrome/browser/guest_view/web_view/web_view_permission_types.h"
#include "chrome/browser/guest_view/web_view/web_view_renderer_state.h"
#include "chrome/browser/renderer_context_menu/context_menu_delegate.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
#include "chrome/browser/ui/pdf/pdf_tab_helper.h"
#include "chrome/browser/ui/zoom/zoom_controller.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/extensions/chrome_extension_messages.h"
#include "chrome/common/render_messages.h"
#include "components/renderer_context_menu/context_menu_delegate.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/child_process_security_policy.h"
#include "content/public/browser/native_web_keyboard_event.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu_observer.h"
#include "components/renderer_context_menu/render_view_context_menu_observer.h"

namespace content {
class WebContents;
Expand Down
102 changes: 61 additions & 41 deletions chrome/browser/renderer_context_menu/render_view_context_menu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
#include "components/translate/core/browser/translate_download_manager.h"
#include "components/translate/core/browser/translate_manager.h"
#include "components/translate/core/browser/translate_prefs.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/child_process_security_policy.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/download_save_info.h"
Expand Down Expand Up @@ -383,6 +384,11 @@ void EscapeAmpersands(base::string16* text) {
text);
}

// Returns the preference of the profile represented by the |context|.
PrefService* GetPrefs(content::BrowserContext* context) {
return user_prefs::UserPrefs::Get(context);
}

} // namespace

// static
Expand All @@ -409,16 +415,15 @@ RenderViewContextMenu::RenderViewContextMenu(
source_web_contents_(WebContents::FromRenderFrameHost(render_frame_host)),
render_process_id_(render_frame_host->GetProcess()->GetID()),
render_frame_id_(render_frame_host->GetRoutingID()),
profile_(Profile::FromBrowserContext(
source_web_contents_->GetBrowserContext())),
browser_context_(source_web_contents_->GetBrowserContext()),
menu_model_(this),
extension_items_(profile_,
extension_items_(browser_context_,
this,
&menu_model_,
base::Bind(MenuItemMatchesParams, params_)),
protocol_handler_submenu_model_(this),
protocol_handler_registry_(
ProtocolHandlerRegistryFactory::GetForProfile(profile_)),
ProtocolHandlerRegistryFactory::GetForProfile(GetProfile())),
command_executed_(false) {
content_type_.reset(ContextMenuContentTypeFactory::Create(
source_web_contents_, params));
Expand Down Expand Up @@ -519,11 +524,11 @@ bool RenderViewContextMenu::MenuItemMatchesParams(
void RenderViewContextMenu::AppendAllExtensionItems() {
extension_items_.Clear();
ExtensionService* service =
extensions::ExtensionSystem::Get(profile_)->extension_service();
extensions::ExtensionSystem::Get(browser_context_)->extension_service();
if (!service)
return; // In unit-tests, we may not have an ExtensionService.

MenuManager* menu_manager = MenuManager::Get(profile_);
MenuManager* menu_manager = MenuManager::Get(browser_context_);
if (!menu_manager)
return;

Expand Down Expand Up @@ -691,6 +696,10 @@ void RenderViewContextMenu::InitMenu() {
}
}

Profile* RenderViewContextMenu::GetProfile() {
return Profile::FromBrowserContext(browser_context_);
}

void RenderViewContextMenu::AppendPrintPreviewItems() {
#if defined(ENABLE_FULL_PRINTING)
if (!print_preview_menu_observer_.get()) {
Expand All @@ -704,7 +713,7 @@ void RenderViewContextMenu::AppendPrintPreviewItems() {

const Extension* RenderViewContextMenu::GetExtension() const {
extensions::ExtensionSystem* system =
extensions::ExtensionSystem::Get(profile_);
extensions::ExtensionSystem::Get(browser_context_);
// There is no process manager in some tests.
if (!system->process_manager())
return NULL;
Expand Down Expand Up @@ -749,8 +758,8 @@ WebContents* RenderViewContextMenu::GetWebContents() const {
return source_web_contents_;
}

Profile* RenderViewContextMenu::GetProfile() const {
return profile_;
BrowserContext* RenderViewContextMenu::GetBrowserContext() const {
return browser_context_;
}

bool RenderViewContextMenu::AppendCustomItems() {
Expand Down Expand Up @@ -828,7 +837,7 @@ void RenderViewContextMenu::AppendImageItems() {

void RenderViewContextMenu::AppendSearchWebForImageItems() {
TemplateURLService* service =
TemplateURLServiceFactory::GetForProfile(profile_);
TemplateURLServiceFactory::GetForProfile(GetProfile());
const TemplateURL* const default_provider =
service->GetDefaultSearchProvider();
if (params_.has_image_contents && default_provider &&
Expand Down Expand Up @@ -955,15 +964,15 @@ void RenderViewContextMenu::AppendCopyItem() {
}

void RenderViewContextMenu::AppendPrintItem() {
if (profile_->GetPrefs()->GetBoolean(prefs::kPrintingEnabled) &&
if (GetPrefs(browser_context_)->GetBoolean(prefs::kPrintingEnabled) &&
(params_.media_type == WebContextMenuData::MediaTypeNone ||
params_.media_flags & WebContextMenuData::MediaCanPrint)) {
menu_model_.AddItemWithStringId(IDC_PRINT, IDS_CONTENT_CONTEXT_PRINT);
}
}

void RenderViewContextMenu::AppendSearchProvider() {
DCHECK(profile_);
DCHECK(browser_context_);

base::TrimWhitespace(params_.selection_text, base::TRIM_ALL,
&params_.selection_text);
Expand All @@ -974,9 +983,13 @@ void RenderViewContextMenu::AppendSearchProvider() {
base::ASCIIToUTF16(" "), &params_.selection_text);

AutocompleteMatch match;
AutocompleteClassifierFactory::GetForProfile(profile_)->Classify(
params_.selection_text, false, false,
metrics::OmniboxEventProto::INVALID_SPEC, &match, NULL);
AutocompleteClassifierFactory::GetForProfile(GetProfile())
->Classify(params_.selection_text,
false,
false,
metrics::OmniboxEventProto::INVALID_SPEC,
&match,
NULL);
selection_navigation_url_ = match.destination_url;
if (!selection_navigation_url_.is_valid())
return;
Expand All @@ -986,8 +999,8 @@ void RenderViewContextMenu::AppendSearchProvider() {

if (AutocompleteMatch::IsSearchType(match.type)) {
const TemplateURL* const default_provider =
TemplateURLServiceFactory::GetForProfile(profile_)->
GetDefaultSearchProvider();
TemplateURLServiceFactory::GetForProfile(GetProfile())
->GetDefaultSearchProvider();
if (!default_provider)
return;
menu_model_.AddItem(
Expand Down Expand Up @@ -1112,10 +1125,12 @@ bool RenderViewContextMenu::IsCommandIdEnabled(int id) const {
return false;
}

PrefService* prefs = GetPrefs(browser_context_);

// Allow Spell Check language items on sub menu for text area context menu.
if ((id >= IDC_SPELLCHECK_LANGUAGES_FIRST) &&
(id < IDC_SPELLCHECK_LANGUAGES_LAST)) {
return profile_->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck);
return prefs->GetBoolean(prefs::kEnableContinuousSpellcheck);
}

// Custom items.
Expand All @@ -1136,7 +1151,7 @@ bool RenderViewContextMenu::IsCommandIdEnabled(int id) const {
}

IncognitoModePrefs::Availability incognito_avail =
IncognitoModePrefs::GetAvailability(profile_->GetPrefs());
IncognitoModePrefs::GetAvailability(prefs);
switch (id) {
case IDC_BACK:
return source_web_contents_->GetController().CanGoBack();
Expand Down Expand Up @@ -1340,13 +1355,14 @@ bool RenderViewContextMenu::IsCommandIdEnabled(int id) const {
return !!(params_.edit_flags & WebContextMenuData::CanSelectAll);

case IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD:
return !profile_->IsOffTheRecord() && params_.link_url.is_valid() &&
return !browser_context_->IsOffTheRecord() &&
params_.link_url.is_valid() &&
incognito_avail != IncognitoModePrefs::DISABLED;

case IDC_PRINT:
return profile_->GetPrefs()->GetBoolean(prefs::kPrintingEnabled) &&
(params_.media_type == WebContextMenuData::MediaTypeNone ||
params_.media_flags & WebContextMenuData::MediaCanPrint);
return prefs->GetBoolean(prefs::kPrintingEnabled) &&
(params_.media_type == WebContextMenuData::MediaTypeNone ||
params_.media_flags & WebContextMenuData::MediaCanPrint);

case IDC_CONTENT_CONTEXT_SEARCHWEBFOR:
case IDC_CONTENT_CONTEXT_GOTOURL:
Expand All @@ -1360,8 +1376,7 @@ bool RenderViewContextMenu::IsCommandIdEnabled(int id) const {
return true;

case IDC_CHECK_SPELLING_WHILE_TYPING:
return profile_->GetPrefs()->GetBoolean(
prefs::kEnableContinuousSpellcheck);
return prefs->GetBoolean(prefs::kEnableContinuousSpellcheck);

#if !defined(OS_MACOSX) && defined(OS_POSIX)
// TODO(suzhe): this should not be enabled for password fields.
Expand Down Expand Up @@ -1509,7 +1524,8 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) {
const GURL& referrer =
params_.frame_url.is_empty() ? params_.page_url : params_.frame_url;
const GURL& url = params_.link_url;
DownloadManager* dlm = BrowserContext::GetDownloadManager(profile_);
DownloadManager* dlm =
BrowserContext::GetDownloadManager(browser_context_);
scoped_ptr<DownloadUrlParameters> dl_params(
DownloadUrlParameters::FromWebContents(source_web_contents_, url));
dl_params->set_referrer(
Expand Down Expand Up @@ -1647,8 +1663,9 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) {
DCHECK(platform_app);
DCHECK(platform_app->is_platform_app());

extensions::ExtensionSystem::Get(profile_)->extension_service()->
ReloadExtension(platform_app->id());
extensions::ExtensionSystem::Get(browser_context_)
->extension_service()
->ReloadExtension(platform_app->id());
break;
}

Expand All @@ -1657,8 +1674,8 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) {
DCHECK(platform_app);
DCHECK(platform_app->is_platform_app());

apps::AppLoadService::Get(profile_)->RestartApplication(
platform_app->id());
apps::AppLoadService::Get(GetProfile())
->RestartApplication(platform_app->id());
break;
}

Expand All @@ -1671,7 +1688,8 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) {

if (!print_view_manager)
break;
if (profile_->GetPrefs()->GetBoolean(prefs::kPrintPreviewDisabled)) {
if (GetPrefs(browser_context_)
->GetBoolean(prefs::kPrintPreviewDisabled)) {
print_view_manager->PrintNow();
} else {
print_view_manager->PrintPreviewNow(!params_.selection_text.empty());
Expand Down Expand Up @@ -1706,7 +1724,8 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) {
DCHECK(platform_app);
DCHECK(platform_app->is_platform_app());

extensions::devtools_util::InspectBackgroundPage(platform_app, profile_);
extensions::devtools_util::InspectBackgroundPage(platform_app,
GetProfile());
break;
}

Expand Down Expand Up @@ -1742,7 +1761,8 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) {
// Since the user decided to translate for that language and site, clears
// any preferences for not translating them.
scoped_ptr<translate::TranslatePrefs> prefs(
ChromeTranslateClient::CreateTranslatePrefs(profile_->GetPrefs()));
ChromeTranslateClient::CreateTranslatePrefs(
GetPrefs(browser_context_)));
prefs->UnblockLanguage(original_lang);
prefs->RemoveSiteFromBlacklist(params_.page_url.HostNoBrackets());
translate::TranslateManager* manager =
Expand Down Expand Up @@ -1833,7 +1853,7 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) {
case IDC_CONTENT_CONTEXT_ADDSEARCHENGINE: {
// Make sure the model is loaded.
TemplateURLService* model =
TemplateURLServiceFactory::GetForProfile(profile_);
TemplateURLServiceFactory::GetForProfile(GetProfile());
if (!model)
return;
model->Load();
Expand All @@ -1850,8 +1870,8 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) {
data.favicon_url =
TemplateURL::GenerateFaviconURL(params_.page_url.GetOrigin());
// Takes ownership of the TemplateURL.
search_engine_tab_helper->delegate()->
ConfirmAddSearchProvider(new TemplateURL(data), profile_);
search_engine_tab_helper->delegate()->ConfirmAddSearchProvider(
new TemplateURL(data), GetProfile());
}
break;
}
Expand Down Expand Up @@ -1915,13 +1935,14 @@ bool RenderViewContextMenu::IsDevCommandEnabled(int id) const {
if (id == IDC_CONTENT_CONTEXT_INSPECTELEMENT ||
id == IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE) {
const CommandLine* command_line = CommandLine::ForCurrentProcess();
if (!profile_->GetPrefs()->GetBoolean(prefs::kWebKitJavascriptEnabled) ||
if (!GetPrefs(browser_context_)
->GetBoolean(prefs::kWebKitJavascriptEnabled) ||
command_line->HasSwitch(switches::kDisableJavaScript))
return false;

// Don't enable the web inspector if the developer tools are disabled via
// the preference dev-tools-disabled.
if (profile_->GetPrefs()->GetBoolean(prefs::kDevToolsDisabled))
if (GetPrefs(browser_context_)->GetBoolean(prefs::kDevToolsDisabled))
return false;
}

Expand Down Expand Up @@ -1959,8 +1980,7 @@ void RenderViewContextMenu::OpenURL(
details.not_yet_in_tabstrip = false;
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_RETARGETING,
content::Source<Profile>(Profile::FromBrowserContext(
source_web_contents_->GetBrowserContext())),
content::Source<Profile>(GetProfile()),
content::Details<RetargetingDetails>(&details));
}

Expand Down Expand Up @@ -1992,7 +2012,7 @@ void RenderViewContextMenu::Inspect(int x, int y) {
void RenderViewContextMenu::WriteURLToClipboard(const GURL& url) {
chrome_common_net::WriteURLToClipboard(
url,
profile_->GetPrefs()->GetString(prefs::kAcceptLanguages),
GetPrefs(browser_context_)->GetString(prefs::kAcceptLanguages),
ui::Clipboard::GetForCurrentThread());
}

Expand Down
Loading

0 comments on commit 0a83f14

Please sign in to comment.