Commit 6038eab2 authored by n.bansal@samsung.com's avatar n.bansal@samsung.com

Use suggested filename for "Save Link As"

Use filename mentioned in download attribute of anchor element while saving file using "Save Link As" from context menu.

BUG=366370

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@281983 0039d316-1c4b-4281-b951-d872f2087c98
parent 93877762
...@@ -1511,6 +1511,7 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) { ...@@ -1511,6 +1511,7 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) {
dl_params->set_referrer( dl_params->set_referrer(
content::Referrer(referrer, params_.referrer_policy)); content::Referrer(referrer, params_.referrer_policy));
dl_params->set_referrer_encoding(params_.frame_charset); dl_params->set_referrer_encoding(params_.frame_charset);
dl_params->set_suggested_name(params_.suggested_filename);
dl_params->set_prompt(true); dl_params->set_prompt(true);
dlm->DownloadUrl(dl_params.Pass()); dlm->DownloadUrl(dl_params.Pass());
break; break;
......
...@@ -230,6 +230,39 @@ IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, OpenIncognitoNoneReferrer) { ...@@ -230,6 +230,39 @@ IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, OpenIncognitoNoneReferrer) {
ASSERT_EQ(kEmptyReferrer, page_referrer); ASSERT_EQ(kEmptyReferrer, page_referrer);
} }
// Check filename on clicking "Save Link As" via a "real" context menu.
IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, SuggestedFileName) {
// Register observer.
SaveLinkAsContextMenuObserver menu_observer(
content::NotificationService::AllSources());
// Go to a page with a link having download attribute.
const std::string kSuggestedFilename("test_filename.png");
ui_test_utils::NavigateToURL(
browser(),
GURL("data:text/html,<a href='about:blank' download='" +
kSuggestedFilename + "'>link</a>"));
// Open a context menu.
blink::WebMouseEvent mouse_event;
mouse_event.type = blink::WebInputEvent::MouseDown;
mouse_event.button = blink::WebMouseEvent::ButtonRight;
mouse_event.x = 15;
mouse_event.y = 15;
content::WebContents* tab =
browser()->tab_strip_model()->GetActiveWebContents();
tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
mouse_event.type = blink::WebInputEvent::MouseUp;
tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
// Wait for context menu to be visible.
menu_observer.WaitForMenu();
// Compare filename.
base::string16 suggested_filename = menu_observer.GetSuggestedFilename();
ASSERT_EQ(kSuggestedFilename, base::UTF16ToUTF8(suggested_filename).c_str());
}
// Ensure that View Page Info won't crash if there is no visible entry. // Ensure that View Page Info won't crash if there is no visible entry.
// See http://crbug.com/370863. // See http://crbug.com/370863.
IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, ViewPageInfoWithNoEntry) { IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, ViewPageInfoWithNoEntry) {
......
...@@ -6,9 +6,11 @@ ...@@ -6,9 +6,11 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu.h" #include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
#include "content/public/browser/notification_service.h" #include "content/public/browser/notification_service.h"
#include "content/public/test/test_utils.h"
ContextMenuNotificationObserver::ContextMenuNotificationObserver( ContextMenuNotificationObserver::ContextMenuNotificationObserver(
int command_to_execute) int command_to_execute)
...@@ -32,7 +34,8 @@ void ContextMenuNotificationObserver::Observe( ...@@ -32,7 +34,8 @@ void ContextMenuNotificationObserver::Observe(
base::MessageLoop::current()->PostTask( base::MessageLoop::current()->PostTask(
FROM_HERE, FROM_HERE,
base::Bind(&ContextMenuNotificationObserver::ExecuteCommand, base::Bind(&ContextMenuNotificationObserver::ExecuteCommand,
base::Unretained(this), context_menu)); base::Unretained(this),
context_menu));
break; break;
} }
...@@ -46,3 +49,53 @@ void ContextMenuNotificationObserver::ExecuteCommand( ...@@ -46,3 +49,53 @@ void ContextMenuNotificationObserver::ExecuteCommand(
context_menu->ExecuteCommand(command_to_execute_, 0); context_menu->ExecuteCommand(command_to_execute_, 0);
context_menu->Cancel(); context_menu->Cancel();
} }
SaveLinkAsContextMenuObserver::SaveLinkAsContextMenuObserver(
const content::NotificationSource& source)
: ContextMenuNotificationObserver(IDC_CONTENT_CONTEXT_SAVELINKAS),
menu_visible_(false) {
}
SaveLinkAsContextMenuObserver::~SaveLinkAsContextMenuObserver() {
}
void SaveLinkAsContextMenuObserver::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_RENDER_VIEW_CONTEXT_MENU_SHOWN: {
menu_visible_ = true;
RenderViewContextMenu* context_menu =
content::Source<RenderViewContextMenu>(source).ptr();
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&SaveLinkAsContextMenuObserver::Cancel,
base::Unretained(this),
context_menu));
break;
}
default:
NOTREACHED();
}
}
void SaveLinkAsContextMenuObserver::WaitForMenu() {
content::WindowedNotificationObserver menu_observer(
chrome::NOTIFICATION_RENDER_VIEW_CONTEXT_MENU_SHOWN,
content::NotificationService::AllSources());
if (!menu_visible_)
menu_observer.Wait();
menu_visible_ = false;
}
base::string16 SaveLinkAsContextMenuObserver::GetSuggestedFilename() {
return params_.suggested_filename;
}
void SaveLinkAsContextMenuObserver::Cancel(
RenderViewContextMenu* context_menu) {
params_ = context_menu->params();
context_menu->Cancel();
}
...@@ -6,8 +6,10 @@ ...@@ -6,8 +6,10 @@
#define CHROME_BROWSER_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_BROWSERTEST_UTIL_H_ #define CHROME_BROWSER_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_BROWSERTEST_UTIL_H_
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/strings/string16.h"
#include "content/public/browser/notification_observer.h" #include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h" #include "content/public/browser/notification_registrar.h"
#include "content/public/common/context_menu_params.h"
class RenderViewContextMenu; class RenderViewContextMenu;
...@@ -30,4 +32,33 @@ class ContextMenuNotificationObserver : public content::NotificationObserver { ...@@ -30,4 +32,33 @@ class ContextMenuNotificationObserver : public content::NotificationObserver {
DISALLOW_COPY_AND_ASSIGN(ContextMenuNotificationObserver); DISALLOW_COPY_AND_ASSIGN(ContextMenuNotificationObserver);
}; };
class SaveLinkAsContextMenuObserver : public ContextMenuNotificationObserver {
public:
// Register to listen for notifications of
// NOTIFICATION_RENDER_VIEW_CONTEXT_MENU_SHOWN from either
// a specific source, or from all sources if |source| is
// NotificationService::AllSources().
explicit SaveLinkAsContextMenuObserver(
const content::NotificationSource& source);
virtual ~SaveLinkAsContextMenuObserver();
// Suggested filename for file downloaded through "Save Link As" option.
base::string16 GetSuggestedFilename();
// Wait for context menu to be visible.
void WaitForMenu();
private:
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
void Cancel(RenderViewContextMenu* context_menu);
bool menu_visible_;
content::ContextMenuParams params_;
DISALLOW_COPY_AND_ASSIGN(SaveLinkAsContextMenuObserver);
};
#endif // CHROME_BROWSER_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_BROWSERTEST_UTIL_H_ #endif // CHROME_BROWSER_RENDERER_CONTEXT_MENU_RENDER_VIEW_CONTEXT_MENU_BROWSERTEST_UTIL_H_
...@@ -54,6 +54,7 @@ IPC_STRUCT_TRAITS_BEGIN(content::ContextMenuParams) ...@@ -54,6 +54,7 @@ IPC_STRUCT_TRAITS_BEGIN(content::ContextMenuParams)
IPC_STRUCT_TRAITS_MEMBER(frame_page_state) IPC_STRUCT_TRAITS_MEMBER(frame_page_state)
IPC_STRUCT_TRAITS_MEMBER(media_flags) IPC_STRUCT_TRAITS_MEMBER(media_flags)
IPC_STRUCT_TRAITS_MEMBER(selection_text) IPC_STRUCT_TRAITS_MEMBER(selection_text)
IPC_STRUCT_TRAITS_MEMBER(suggested_filename)
IPC_STRUCT_TRAITS_MEMBER(misspelled_word) IPC_STRUCT_TRAITS_MEMBER(misspelled_word)
IPC_STRUCT_TRAITS_MEMBER(misspelling_hash) IPC_STRUCT_TRAITS_MEMBER(misspelling_hash)
IPC_STRUCT_TRAITS_MEMBER(dictionary_suggestions) IPC_STRUCT_TRAITS_MEMBER(dictionary_suggestions)
......
...@@ -98,6 +98,10 @@ struct CONTENT_EXPORT ContextMenuParams { ...@@ -98,6 +98,10 @@ struct CONTENT_EXPORT ContextMenuParams {
// This is the text of the selection that the context menu was invoked on. // This is the text of the selection that the context menu was invoked on.
base::string16 selection_text; base::string16 selection_text;
// This is the suggested filename to be used when saving file through "Save
// Link As" option of context menu.
base::string16 suggested_filename;
// The misspelled word under the cursor, if any. Used to generate the // The misspelled word under the cursor, if any. Used to generate the
// |dictionary_suggestions| list. // |dictionary_suggestions| list.
base::string16 misspelled_word; base::string16 misspelled_word;
......
...@@ -40,6 +40,7 @@ ContextMenuParams ContextMenuParamsBuilder::Build( ...@@ -40,6 +40,7 @@ ContextMenuParams ContextMenuParamsBuilder::Build(
params.edit_flags = data.editFlags; params.edit_flags = data.editFlags;
params.frame_charset = data.frameEncoding.utf8(); params.frame_charset = data.frameEncoding.utf8();
params.referrer_policy = data.referrerPolicy; params.referrer_policy = data.referrerPolicy;
params.suggested_filename = data.suggestedFilename;
for (size_t i = 0; i < data.dictionarySuggestions.size(); ++i) for (size_t i = 0; i < data.dictionarySuggestions.size(); ++i)
params.dictionary_suggestions.push_back(data.dictionarySuggestions[i]); params.dictionary_suggestions.push_back(data.dictionarySuggestions[i]);
......
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