Skip to content

Commit

Permalink
WebUI Download Shelf: Mojo download structs & OnNewDownload API.
Browse files Browse the repository at this point in the history
Added mojo enums, structures and an API to facilitate notifying the
WebUI Download Shelf whenever a new download is triggered. The enums
represent one to one mappings from the existing C++ counterparts they
are based on. The added Page method OnNewDownload will enable the UI to
refresh its view to remain consistent with the download model state.

Bug: 1182529
Change-Id: Ibbfc9c0ae031c2dc4b8e9eed02a1a097b52e3105
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2785921
Commit-Queue: Roman Arora <romanarora@chromium.org>
Reviewed-by: Ken Rockot <rockot@google.com>
Reviewed-by: Alex Gough <ajgo@chromium.org>
Reviewed-by: Min Qin <qinmin@chromium.org>
Reviewed-by: Yuheng Huang <yuhengh@chromium.org>
Reviewed-by: Keren Zhu <kerenzhu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#869233}
  • Loading branch information
Roman Arora authored and Chromium LUCI CQ committed Apr 5, 2021
1 parent f397ea5 commit 570c43d
Show file tree
Hide file tree
Showing 13 changed files with 533 additions and 25 deletions.
2 changes: 1 addition & 1 deletion chrome/browser/resources/download_shelf/BUILD.gn
Expand Up @@ -40,7 +40,7 @@ preprocess_if_expr("preprocess") {
}

preprocess_if_expr("preprocess_mojo") {
deps = [ "//chrome/browser/ui/webui/download_shelf:mojo_bindings_js" ]
deps = [ "//chrome/browser/ui/webui/download_shelf:mojo_bindings_webui_js" ]
in_folder =
"$root_gen_dir/mojom-webui/chrome/browser/ui/webui/download_shelf/"
out_folder = "$target_gen_dir/$preprocess_folder"
Expand Down
21 changes: 18 additions & 3 deletions chrome/browser/resources/download_shelf/download_list.js
Expand Up @@ -31,18 +31,33 @@ export class DownloadListElement extends CustomElement {
/** @private {!DownloadShelfApiProxy} */
this.apiProxy_ = DownloadShelfApiProxyImpl.getInstance();

/** @private {Element} */
this.listElement_ = this.$('#downloadList');
/** @private {!Element} */
this.listElement_ = assert(this.$('#downloadList'));

/** @private {!Array<number>} */
this.listenerIds_ = [];

/** @private {ResizeObserver} */
this.resizeObserver_ = new ResizeObserver(() => this.updateElements_());
this.resizeObserver_.observe(assert(this.listElement_));
this.resizeObserver_.observe(this.listElement_);

this.apiProxy_.getDownloads().then(downloadItems => {
this.items_ = downloadItems;
this.updateElements_();
});

const callbackRouter = this.apiProxy_.getCallbackRouter();
// TODO(romanarora): Once we implement a close panel button, we should
// ensure it removes all listeners from this.listenerIds_ when triggered.

// Triggers for downloads other than the first one, as the page handler will
// not be ready by the first download.
this.listenerIds_.push(callbackRouter.onNewDownload.addListener(
(download_model) => {
// TODO(romanarora): Implement this method as replacement to
// onCreated.
}));

this.apiProxy_.onCreated(item => {
this.items_.unshift(item);
this.updateElements_();
Expand Down
19 changes: 14 additions & 5 deletions chrome/browser/ui/views/download/download_shelf_web_view.cc
Expand Up @@ -11,7 +11,6 @@
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/webui/download_shelf/download_shelf_ui.h"
#include "chrome/common/webui_url_constants.h"
#include "ui/gfx/animation/animation.h"
#include "ui/views/border.h"
Expand All @@ -33,9 +32,9 @@ DownloadShelfWebView::DownloadShelfWebView(Browser* browser,
web_contents());
task_manager::WebContentsTags::CreateForTabContents(web_contents());

content::WebUI* web_ui = GetWebContents()->GetWebUI();
if (web_ui)
web_ui->GetController()->GetAs<DownloadShelfUI>()->set_embedder(this);
DownloadShelfUI* download_shelf_ui = GetDownloadShelfUI();
if (download_shelf_ui)
download_shelf_ui->set_embedder(this);
}

DownloadShelfWebView::~DownloadShelfWebView() = default;
Expand All @@ -54,7 +53,12 @@ void DownloadShelfWebView::OnThemeChanged() {
}

void DownloadShelfWebView::DoShowDownload(
DownloadUIModel::DownloadUIModelPtr download) {}
DownloadUIModel::DownloadUIModelPtr download) {
DownloadShelfUI* download_shelf_ui = GetDownloadShelfUI();
if (download_shelf_ui) {
download_shelf_ui->DoShowDownload(std::move(download));
}
}

void DownloadShelfWebView::DoOpen() {
SetVisible(true);
Expand Down Expand Up @@ -118,3 +122,8 @@ bool DownloadShelfWebView::IsShowing() const {
bool DownloadShelfWebView::IsClosing() const {
return shelf_animation_.IsClosing();
}

DownloadShelfUI* DownloadShelfWebView::GetDownloadShelfUI() {
content::WebUI* web_ui = GetWebContents()->GetWebUI();
return web_ui ? web_ui->GetController()->GetAs<DownloadShelfUI>() : nullptr;
}
3 changes: 3 additions & 0 deletions chrome/browser/ui/views/download/download_shelf_web_view.h
Expand Up @@ -7,6 +7,7 @@

#include "chrome/browser/download/download_shelf.h"
#include "chrome/browser/ui/views/download/download_shelf_context_menu_view.h"
#include "chrome/browser/ui/webui/download_shelf/download_shelf_ui.h"
#include "chrome/browser/ui/webui/download_shelf/download_shelf_ui_embedder.h"
#include "ui/gfx/animation/slide_animation.h"
#include "ui/views/animation/animation_delegate_views.h"
Expand Down Expand Up @@ -55,6 +56,8 @@ class DownloadShelfWebView : public DownloadShelf,
bool IsShowing() const override;
bool IsClosing() const override;

DownloadShelfUI* GetDownloadShelfUI();

BrowserView* parent_;

// The show/hide animation for the shelf itself.
Expand Down
27 changes: 26 additions & 1 deletion chrome/browser/ui/webui/download_shelf/BUILD.gn
Expand Up @@ -6,6 +6,31 @@ import("//mojo/public/tools/bindings/mojom.gni")

mojom("mojo_bindings") {
sources = [ "download_shelf.mojom" ]
public_deps = [ "//mojo/public/mojom/base" ]
public_deps = [
"//mojo/public/mojom/base",
"//url/mojom:url_mojom_gurl",
]
webui_module_path = "/"

# TODO(crbug.com/1195085): Remove the typemaps and use the mojo types in other
# code parts.
cpp_typemaps = [
{
types = [
{
mojom = "download_shelf.mojom.DangerType"
cpp = "::download::DownloadDangerType"
},
{
mojom = "download_shelf.mojom.DownloadState"
cpp = "::download::DownloadItem::DownloadState"
},
{
mojom = "download_shelf.mojom.MixedContentStatus"
cpp = "::download::DownloadItem::MixedContentStatus"
},
]
traits_headers = [ "download_mojom_traits.h" ]
},
]
}
2 changes: 2 additions & 0 deletions chrome/browser/ui/webui/download_shelf/OWNERS
Expand Up @@ -5,3 +5,5 @@ kerenzhu@chromium.org

per-file *.mojom=set noparent
per-file *.mojom=file://ipc/SECURITY_OWNERS
per-file *_mojom_traits*.*=set noparent
per-file *_mojom_traits*.*=file://ipc/SECURITY_OWNERS
236 changes: 236 additions & 0 deletions chrome/browser/ui/webui/download_shelf/download_mojom_traits.h
@@ -0,0 +1,236 @@
// Copyright 2021 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.

#ifndef CHROME_BROWSER_UI_WEBUI_DOWNLOAD_SHELF_DOWNLOAD_MOJOM_TRAITS_H_
#define CHROME_BROWSER_UI_WEBUI_DOWNLOAD_SHELF_DOWNLOAD_MOJOM_TRAITS_H_

#include "chrome/browser/ui/webui/download_shelf/download_shelf.mojom.h"
#include "components/download/public/common/download_danger_type.h"
#include "components/download/public/common/download_item.h"

namespace mojo {

template <>
struct EnumTraits<download_shelf::mojom::DangerType,
download::DownloadDangerType> {
using DownloadDangerType = download::DownloadDangerType;
using MojoDangerType = download_shelf::mojom::DangerType;

static MojoDangerType ToMojom(DownloadDangerType input) {
switch (input) {
case DownloadDangerType::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS:
return MojoDangerType::kNotDangerous;
case DownloadDangerType::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE:
return MojoDangerType::kDangerousFile;
case DownloadDangerType::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL:
return MojoDangerType::kDangerousUrl;
case DownloadDangerType::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT:
return MojoDangerType::kDangerousContent;
case DownloadDangerType::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT:
return MojoDangerType::kMaybeDangerousContent;
case DownloadDangerType::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT:
return MojoDangerType::kUncommonContent;
case DownloadDangerType::DOWNLOAD_DANGER_TYPE_USER_VALIDATED:
return MojoDangerType::kUserValidated;
case DownloadDangerType::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST:
return MojoDangerType::kDangerousHost;
case DownloadDangerType::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED:
return MojoDangerType::kPotentiallyUnwanted;
case DownloadDangerType::DOWNLOAD_DANGER_TYPE_ALLOWLISTED_BY_POLICY:
return MojoDangerType::kAllowListedByPolicy;
case DownloadDangerType::DOWNLOAD_DANGER_TYPE_ASYNC_SCANNING:
return MojoDangerType::kAsyncScanning;
case DownloadDangerType::DOWNLOAD_DANGER_TYPE_BLOCKED_PASSWORD_PROTECTED:
return MojoDangerType::kBlockedPasswordProtected;
case DownloadDangerType::DOWNLOAD_DANGER_TYPE_BLOCKED_TOO_LARGE:
return MojoDangerType::kBlockedTooLarge;
case DownloadDangerType::DOWNLOAD_DANGER_TYPE_SENSITIVE_CONTENT_WARNING:
return MojoDangerType::kSensitiveContentWarning;
case DownloadDangerType::DOWNLOAD_DANGER_TYPE_SENSITIVE_CONTENT_BLOCK:
return MojoDangerType::kSensitiveContentBlock;
case DownloadDangerType::DOWNLOAD_DANGER_TYPE_DEEP_SCANNED_SAFE:
return MojoDangerType::kDeepScannedSafe;
case DownloadDangerType::
DOWNLOAD_DANGER_TYPE_DEEP_SCANNED_OPENED_DANGEROUS:
return MojoDangerType::kDeepScannedOpenedDangerous;
case DownloadDangerType::DOWNLOAD_DANGER_TYPE_PROMPT_FOR_SCANNING:
return MojoDangerType::kPromptForScanning;
case DownloadDangerType::
DOWNLOAD_DANGER_TYPE_BLOCKED_UNSUPPORTED_FILETYPE:
return MojoDangerType::kBlockedUnsupportedFileType;
case DownloadDangerType::DOWNLOAD_DANGER_TYPE_MAX:
break;
}
NOTREACHED();
return MojoDangerType::kNotDangerous;
}

static bool FromMojom(MojoDangerType input, DownloadDangerType* out) {
switch (input) {
case MojoDangerType::kNotDangerous:
*out = DownloadDangerType::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS;
return true;
case MojoDangerType::kDangerousFile:
*out = DownloadDangerType::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE;
return true;
case MojoDangerType::kDangerousUrl:
*out = DownloadDangerType::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL;
return true;
case MojoDangerType::kDangerousContent:
*out = DownloadDangerType::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT;
return true;
case MojoDangerType::kMaybeDangerousContent:
*out = DownloadDangerType::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT;
return true;
case MojoDangerType::kUncommonContent:
*out = DownloadDangerType::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT;
return true;
case MojoDangerType::kUserValidated:
*out = DownloadDangerType::DOWNLOAD_DANGER_TYPE_USER_VALIDATED;
return true;
case MojoDangerType::kDangerousHost:
*out = DownloadDangerType::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST;
return true;
case MojoDangerType::kPotentiallyUnwanted:
*out = DownloadDangerType::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED;
return true;
case MojoDangerType::kAllowListedByPolicy:
*out = DownloadDangerType::DOWNLOAD_DANGER_TYPE_ALLOWLISTED_BY_POLICY;
return true;
case MojoDangerType::kAsyncScanning:
*out = DownloadDangerType::DOWNLOAD_DANGER_TYPE_ASYNC_SCANNING;
return true;
case MojoDangerType::kBlockedPasswordProtected:
*out =
DownloadDangerType::DOWNLOAD_DANGER_TYPE_BLOCKED_PASSWORD_PROTECTED;
return true;
case MojoDangerType::kBlockedTooLarge:
*out = DownloadDangerType::DOWNLOAD_DANGER_TYPE_BLOCKED_TOO_LARGE;
return true;
case MojoDangerType::kSensitiveContentWarning:
*out =
DownloadDangerType::DOWNLOAD_DANGER_TYPE_SENSITIVE_CONTENT_WARNING;
return true;
case MojoDangerType::kSensitiveContentBlock:
*out = DownloadDangerType::DOWNLOAD_DANGER_TYPE_SENSITIVE_CONTENT_BLOCK;
return true;
case MojoDangerType::kDeepScannedSafe:
*out = DownloadDangerType::DOWNLOAD_DANGER_TYPE_DEEP_SCANNED_SAFE;
return true;
case MojoDangerType::kDeepScannedOpenedDangerous:
*out = DownloadDangerType::
DOWNLOAD_DANGER_TYPE_DEEP_SCANNED_OPENED_DANGEROUS;
return true;
case MojoDangerType::kPromptForScanning:
*out = DownloadDangerType::DOWNLOAD_DANGER_TYPE_PROMPT_FOR_SCANNING;
return true;
case MojoDangerType::kBlockedUnsupportedFileType:
*out = DownloadDangerType::
DOWNLOAD_DANGER_TYPE_BLOCKED_UNSUPPORTED_FILETYPE;
return true;
}
NOTREACHED();
return false;
}
};

template <>
struct EnumTraits<download_shelf::mojom::DownloadState,
download::DownloadItem::DownloadState> {
using DownloadState = download::DownloadItem::DownloadState;
using MojoDownloadState = download_shelf::mojom::DownloadState;

static MojoDownloadState ToMojom(DownloadState input) {
switch (input) {
case DownloadState::IN_PROGRESS:
return MojoDownloadState::kInProgress;
case DownloadState::COMPLETE:
return MojoDownloadState::kComplete;
case DownloadState::CANCELLED:
return MojoDownloadState::kCancelled;
case DownloadState::INTERRUPTED:
return MojoDownloadState::kInterrupted;
case DownloadState::MAX_DOWNLOAD_STATE:
break;
}
NOTREACHED();
return MojoDownloadState::kInProgress;
}

static bool FromMojom(MojoDownloadState input, DownloadState* out) {
switch (input) {
case MojoDownloadState::kInProgress:
*out = DownloadState::IN_PROGRESS;
return true;
case MojoDownloadState::kComplete:
*out = DownloadState::COMPLETE;
return true;
case MojoDownloadState::kCancelled:
*out = DownloadState::CANCELLED;
return true;
case MojoDownloadState::kInterrupted:
*out = DownloadState::INTERRUPTED;
return true;
}
NOTREACHED();
return false;
}
};

template <>
struct EnumTraits<download_shelf::mojom::MixedContentStatus,
download::DownloadItem::MixedContentStatus> {
using DownloadItemMixedContentStatus =
download::DownloadItem::MixedContentStatus;
using MojoMixedContentStatus = download_shelf::mojom::MixedContentStatus;

static MojoMixedContentStatus ToMojom(DownloadItemMixedContentStatus input) {
switch (input) {
case DownloadItemMixedContentStatus::UNKNOWN:
return MojoMixedContentStatus::kUnknown;
case DownloadItemMixedContentStatus::SAFE:
return MojoMixedContentStatus::kSafe;
case DownloadItemMixedContentStatus::VALIDATED:
return MojoMixedContentStatus::kValidated;
case DownloadItemMixedContentStatus::WARN:
return MojoMixedContentStatus::kWarn;
case DownloadItemMixedContentStatus::BLOCK:
return MojoMixedContentStatus::kBlock;
case DownloadItemMixedContentStatus::SILENT_BLOCK:
return MojoMixedContentStatus::kSilentBlock;
}
NOTREACHED();
return MojoMixedContentStatus::kUnknown;
}

static bool FromMojom(MojoMixedContentStatus input,
DownloadItemMixedContentStatus* out) {
switch (input) {
case MojoMixedContentStatus::kUnknown:
*out = DownloadItemMixedContentStatus::UNKNOWN;
return true;
case MojoMixedContentStatus::kSafe:
*out = DownloadItemMixedContentStatus::SAFE;
return true;
case MojoMixedContentStatus::kValidated:
*out = DownloadItemMixedContentStatus::VALIDATED;
return true;
case MojoMixedContentStatus::kWarn:
*out = DownloadItemMixedContentStatus::WARN;
return true;
case MojoMixedContentStatus::kBlock:
*out = DownloadItemMixedContentStatus::BLOCK;
return true;
case MojoMixedContentStatus::kSilentBlock:
*out = DownloadItemMixedContentStatus::SILENT_BLOCK;
return true;
}
NOTREACHED();
return false;
}
};

} // namespace mojo

#endif // CHROME_BROWSER_UI_WEBUI_DOWNLOAD_SHELF_DOWNLOAD_MOJOM_TRAITS_H_

0 comments on commit 570c43d

Please sign in to comment.