Skip to content

Commit

Permalink
Show download item origin URL.
Browse files Browse the repository at this point in the history
Fixes brave/brave-browser#1269

* Subclasses DownloadItemView to draw the origin URL and the lock icon,
updates accessible name with origin URL;
* Adds the lock icon to vector icons;
* Extends DownloadItemModel to obtain origin URL text and update tooltip
text with origin URL;
* Adds unit tests for BraveDownloadItemModel.
  • Loading branch information
mkarolin authored and bsclifton committed Oct 12, 2018
1 parent cf2c77a commit eb2ce61
Show file tree
Hide file tree
Showing 14 changed files with 703 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/vector_icons/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ aggregate_vector_icons("brave_vector_icons") {

icons = [
"autoplay_status.icon",
"download_unlock.icon",
"tor_profile.icon",
]
}
Expand Down
67 changes: 67 additions & 0 deletions app/vector_icons/download_unlock.icon
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.

CANVAS_DIMENSIONS, 32,
MOVE_TO, 24, 3,
R_ARC_TO, 6.9f, 6.9f, 0, 0, 0, -6.89f, 6.89f,
R_V_LINE_TO, 1.73f,
H_LINE_TO, 8.5f,
R_ARC_TO, 3.51f, 3.51f, 0, 0, 0, -3.5f, 3.5f,
V_LINE_TO, 25.5f,
ARC_TO, 3.5f, 3.5f, 0, 0, 0, 8.5f, 29,
R_H_LINE_TO, 15,
R_ARC_TO, 3.5f, 3.5f, 0, 0, 0, 3.5f, -3.5f,
V_LINE_TO, 15.12f,
R_ARC_TO, 3.51f, 3.51f, 0, 0, 0, -3.5f, -3.5f,
R_H_LINE_TO, -3.39f,
V_LINE_TO, 9.89f,
R_ARC_TO, 3.89f, 3.89f, 0, 0, 1, 7.78f, 0,
R_ARC_TO, 1.5f, 1.5f, 0, 0, 0, 3, 0,
ARC_TO, 6.9f, 6.9f, 0, 0, 0, 24, 3,
CL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ions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ source_set("browser") {
"//brave/components/resources:brave_components_resources_grit",
"//brave/browser/resources:brave_extension_grit",
"//chrome/browser",
"download",
"extensions",
"net",
"permissions",
Expand Down
13 changes: 13 additions & 0 deletions browser/download/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import("//build/config/features.gni")

source_set("download") {
sources = [
"brave_download_item_model.cc",
"brave_download_item_model.h",
]

deps = [
"//base",
"//chrome/browser",
]
}
70 changes: 70 additions & 0 deletions browser/download/brave_download_item_model.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/download/brave_download_item_model.h"

#include "base/strings/string16.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/common/origin_util.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/text_constants.h"
#include "ui/gfx/text_elider.h"
#include "url/gurl.h"
#include "url/url_constants.h"

using download::DownloadItem;

BraveDownloadItemModel::BraveDownloadItemModel(DownloadItemModel& model)
: model_(model) {}

BraveDownloadItemModel::~BraveDownloadItemModel() {}

// Adds origin url to the tooltip text and "Not secure", if needed.
base::string16 BraveDownloadItemModel::GetTooltipText(
const gfx::FontList& font_list,
int max_width) {
base::string16 tooltip =
model_.GetTooltipText(font_list, max_width);

bool is_secure;
base::string16 origin_url = GetOriginURLText(is_secure);

if (!origin_url.empty()) {
tooltip += base::ASCIIToUTF16("\n");
base::string16 tooltip_extra;
if (!is_secure)
tooltip_extra = l10n_util::GetStringUTF16(IDS_NOT_SECURE_VERBOSE_STATE) +
base::char16(' ');
tooltip_extra += origin_url;
tooltip += gfx::ElideText(tooltip_extra, font_list, max_width,
gfx::ELIDE_TAIL, gfx::Typesetter::NATIVE);
}

return tooltip;
}

// Returns origin url text and sets |is_secure|.
base::string16 BraveDownloadItemModel::GetOriginURLText(bool& is_secure) {
is_secure = false;
const GURL gurl = model_.download()->GetURL();
if (gurl.is_empty())
return base::string16();

std::string origin;
if (gurl.is_valid()) {
is_secure = content::IsOriginSecure(gurl);
if (gurl.SchemeIs(url::kAboutScheme)) {
origin = gurl.spec();
} else {
origin = gurl.GetOrigin().spec();
if (!gurl.SchemeIsFile())
base::TrimString(origin, "/", &origin);
}
} else
origin = gurl.possibly_invalid_spec();

return base::UTF8ToUTF16(origin);
}
43 changes: 43 additions & 0 deletions browser/download/brave_download_item_model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_DOWNLOAD_BRAVE_DOWNLOAD_ITEM_MODEL_H_
#define BRAVE_BROWSER_DOWNLOAD_BRAVE_DOWNLOAD_ITEM_MODEL_H_

#include "chrome/browser/download/download_item_model.h"

// The purpose of this class is to extend DonwloadItemModel's class
// functionality by adding a method for the origin URL text and
// a method that returns tool tip text that includes origin URL.
// This class, however, doesn't inherit from DownloadItemModel because
// DownloadItemView has a member of DownloadItemModel type. To use this
// extended functionality, BraveDownloadItemView will have this model as
// a member.
class BraveDownloadItemModel {
public:
// Constructs a BraveDownloadItemModel that encapsulates DownloadItemModel.
explicit BraveDownloadItemModel(DownloadItemModel& model);
~BraveDownloadItemModel();

// Method that returns a string suitable for use as a tooltip. For
// a regular download, the tooltip is the filename and the origin URL with an
// indicator if the URL is secure. For an interrupted download, falls back on
// the base class behavior.
// |font_list| and |max_width| are used to elide the filename and/or interrupt
// reason as necessary to keep the width of the tooltip text under
// |max_width|. The tooltip will be at most 3 lines.
base::string16 GetTooltipText(const gfx::FontList& font_list,
int max_width);

// Returns a string suitable for use as the origin URL. |is_secure| is set to
// true if the url is considered secure.
base::string16 GetOriginURLText(bool& is_secure);

// Reference to the encapsulated model.
DownloadItemModel& model_;

DISALLOW_COPY_AND_ASSIGN(BraveDownloadItemModel);
};

#endif
148 changes: 148 additions & 0 deletions browser/download/brave_download_item_model_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/download/brave_download_item_model.h"

#include <stddef.h>
#include <stdint.h>

#include <vector>

#include "base/i18n/rtl.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/strings/string16.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "components/download/public/common/mock_download_item.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/text_utils.h"

using download::DownloadItem;
using safe_browsing::DownloadFileType;
using ::testing::_;
using ::testing::Mock;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::ReturnRef;
using ::testing::ReturnRefOfCopy;
using ::testing::SetArgPointee;

namespace {

// Default target path for a mock download item in DownloadItemModelTest.
const base::FilePath::CharType kDefaultTargetFilePath[] =
FILE_PATH_LITERAL("/foo/bar/foo.bar");

const base::FilePath::CharType kDefaultDisplayFileName[] =
FILE_PATH_LITERAL("foo.bar");

class BraveDownloadItemModelTest : public testing::Test {
public:
BraveDownloadItemModelTest() : model_(&item_), brave_model_(model_) {}
~BraveDownloadItemModelTest() override {}

protected:

void SetupDownloadItemDefaults() {
ON_CALL(item_, GetReceivedBytes()).WillByDefault(Return(1));
ON_CALL(item_, GetTotalBytes()).WillByDefault(Return(2));
ON_CALL(item_, TimeRemaining(_)).WillByDefault(Return(false));
ON_CALL(item_, GetMimeType()).WillByDefault(Return("text/html"));
ON_CALL(item_, AllDataSaved()).WillByDefault(Return(false));
ON_CALL(item_, GetOpenWhenComplete()).WillByDefault(Return(false));
ON_CALL(item_, GetFileExternallyRemoved()).WillByDefault(Return(false));
ON_CALL(item_, GetState()).WillByDefault(Return(DownloadItem::IN_PROGRESS));
ON_CALL(item_, GetURL()).WillByDefault(ReturnRefOfCopy(GURL("")));
ON_CALL(item_, GetFileNameToReportUser())
.WillByDefault(Return(base::FilePath(kDefaultDisplayFileName)));
ON_CALL(item_, GetTargetFilePath())
.WillByDefault(ReturnRefOfCopy(base::FilePath(kDefaultTargetFilePath)));
ON_CALL(item_, GetTargetDisposition())
.WillByDefault(Return(DownloadItem::TARGET_DISPOSITION_OVERWRITE));
ON_CALL(item_, IsPaused()).WillByDefault(Return(false));
}

download::MockDownloadItem& item() { return item_; }
BraveDownloadItemModel& model() { return brave_model_; }

private:
NiceMock<download::MockDownloadItem> item_;
DownloadItemModel model_;
BraveDownloadItemModel brave_model_;
};

} // namespace

TEST_F(BraveDownloadItemModelTest, GetOriginUrlText) {
// Test that we have the correct origin URL text.
const struct TestCase {
// The url.
const char* url;
// Expected text.
const char* expected_text;
// Expected is_secure.
bool expected_is_secure;
} kTestCases[] = {
// Not secure.
{"http://example.com/foo.bar", "http://example.com", false},
// Secure.
{"https://example.com:5678/foo.bar", "https://example.com:5678", true},
// File, secure.
{"file:///c:/foo/bar/foo.bar", "file:///", true},
// about:, secure.
{"about:about", "about:about", true},
// invalid, not secure.
{"foo.bar.baz", "", false},
// empty, not secure.
{"", "", false},
};

SetupDownloadItemDefaults();
for (unsigned i = 0; i < arraysize(kTestCases); ++i) {
const TestCase& test_case = kTestCases[i];
EXPECT_CALL(item(), GetURL())
.WillRepeatedly(ReturnRefOfCopy(GURL(test_case.url)));
bool is_secure = false;
EXPECT_STREQ(
test_case.expected_text,
base::UTF16ToUTF8(model().GetOriginURLText(is_secure)).c_str());
EXPECT_EQ(is_secure, test_case.expected_is_secure);
Mock::VerifyAndClearExpectations(&item());
}
}

TEST_F(BraveDownloadItemModelTest, GetTooltipText) {
// Test that we have the correct tooltip text that includes origin URL.
const struct TestCase {
// The url.
const char* url;
// Expected tooltip text.
const char* expected_tooltip;
} kTestCases[] = {
{"http://example.com/foo.bar", "foo.bar\nNot secure http://example.com"},
{"https://example.com:5678/foo.bar", "foo.bar\nhttps://example.com:5678"},
};

const int kTooltipWidth = 1000;
const gfx::FontList& font_list =
ui::ResourceBundle::GetSharedInstance().GetFontList(
ui::ResourceBundle::BaseFont);

SetupDownloadItemDefaults();
for (unsigned i = 0; i < arraysize(kTestCases); ++i) {
const TestCase& test_case = kTestCases[i];
EXPECT_CALL(item(), GetURL())
.WillRepeatedly(ReturnRefOfCopy(GURL(test_case.url)));
EXPECT_STREQ(
test_case.expected_tooltip,
base::UTF16ToUTF8(model().GetTooltipText(font_list, kTooltipWidth))
.c_str());
Mock::VerifyAndClearExpectations(&item());
}
}
2 changes: 2 additions & 0 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ source_set("ui") {
"views/brave_layout_provider.h",
"views/brave_actions/brave_actions_container.cc",
"views/brave_actions/brave_actions_container.h",
"views/download/brave_download_item_view.cc",
"views/download/brave_download_item_view.h",
"views/frame/brave_browser_view.cc",
"views/frame/brave_browser_view.h",
"views/importer/brave_import_lock_dialog_view.cc",
Expand Down
Loading

0 comments on commit eb2ce61

Please sign in to comment.