Skip to content

Commit

Permalink
[Nearby] Add NearbyNotificationManager and handler
Browse files Browse the repository at this point in the history
This adds a basic progress notification that will be properly translated
in a follow up CL. We add a new NotificationHandler type so we can use
native notifications on all supported desktop OSs. Handling notification
actions is guarded by the kNearbySharing flag.

Bug: 1102348
Change-Id: Id1524fc5771513e5c21cde3d20c446bfa7c013aa
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2282926
Reviewed-by: Alex Chau <alexchau@chromium.org>
Commit-Queue: Richard Knoll <knollr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#785541}
  • Loading branch information
rknoll authored and Commit Bot committed Jul 6, 2020
1 parent 0299359 commit 257bcab
Show file tree
Hide file tree
Showing 13 changed files with 430 additions and 17 deletions.
4 changes: 4 additions & 0 deletions chrome/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3311,6 +3311,10 @@ static_library("browser") {
"nearby_sharing/nearby_connections_manager_impl.cc",
"nearby_sharing/nearby_connections_manager_impl.h",
"nearby_sharing/nearby_constants.h",
"nearby_sharing/nearby_notification_handler.cc",
"nearby_sharing/nearby_notification_handler.h",
"nearby_sharing/nearby_notification_manager.cc",
"nearby_sharing/nearby_notification_manager.h",
"nearby_sharing/nearby_process_manager.cc",
"nearby_sharing/nearby_process_manager.h",
"nearby_sharing/nearby_sharing_prefs.cc",
Expand Down
38 changes: 38 additions & 0 deletions chrome/browser/nearby_sharing/nearby_notification_handler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2020 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 "chrome/browser/nearby_sharing/nearby_notification_handler.h"

#include <utility>

#include "base/callback.h"

NearbyNotificationHandler::NearbyNotificationHandler() = default;

NearbyNotificationHandler::~NearbyNotificationHandler() = default;

void NearbyNotificationHandler::OnClick(
Profile* profile,
const GURL& origin,
const std::string& notification_id,
const base::Optional<int>& action_index,
const base::Optional<base::string16>& reply,
base::OnceClosure completed_closure) {
// TODO(crbug.com/1102348): Route to NearbySharingService.
std::move(completed_closure).Run();
}

void NearbyNotificationHandler::OnClose(Profile* profile,
const GURL& origin,
const std::string& notification_id,
bool by_user,
base::OnceClosure completed_closure) {
// TODO(crbug.com/1102348): Route to NearbySharingService.
std::move(completed_closure).Run();
}

void NearbyNotificationHandler::OpenSettings(Profile* profile,
const GURL& origin) {
// TODO(crbug.com/1102348): Route to NearbySharingService.
}
42 changes: 42 additions & 0 deletions chrome/browser/nearby_sharing/nearby_notification_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2020 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_NEARBY_SHARING_NEARBY_NOTIFICATION_HANDLER_H_
#define CHROME_BROWSER_NEARBY_SHARING_NEARBY_NOTIFICATION_HANDLER_H_

#include <string>

#include "base/callback_forward.h"
#include "base/optional.h"
#include "base/strings/string16.h"
#include "chrome/browser/notifications/notification_handler.h"
#include "url/gurl.h"

class Profile;

// Handles NEARBY_SHARE notification actions.
class NearbyNotificationHandler : public NotificationHandler {
public:
NearbyNotificationHandler();
NearbyNotificationHandler(const NearbyNotificationHandler&) = delete;
NearbyNotificationHandler& operator=(const NearbyNotificationHandler&) =
delete;
~NearbyNotificationHandler() override;

// NotificationHandler:
void OnClick(Profile* profile,
const GURL& origin,
const std::string& notification_id,
const base::Optional<int>& action_index,
const base::Optional<base::string16>& reply,
base::OnceClosure completed_closure) override;
void OnClose(Profile* profile,
const GURL& origin,
const std::string& notification_id,
bool by_user,
base::OnceClosure completed_closure) override;
void OpenSettings(Profile* profile, const GURL& origin) override;
};

#endif // CHROME_BROWSER_NEARBY_SHARING_NEARBY_NOTIFICATION_HANDLER_H_
124 changes: 124 additions & 0 deletions chrome/browser/nearby_sharing/nearby_notification_manager.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright 2020 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 "chrome/browser/nearby_sharing/nearby_notification_manager.h"

#include "base/strings/utf_string_conversions.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/notifications/notification_display_service.h"
#include "chrome/browser/notifications/notification_display_service_factory.h"
#include "chrome/grit/generated_resources.h"
#include "content/public/browser/browser_thread.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_types.h"
#include "ui/strings/grit/ui_strings.h"

namespace {

constexpr char kNearbyNotificationId[] = "chrome://nearby";
constexpr char kNearbyNotifier[] = "nearby";

FileAttachment::Type GetCommonFileAttachmentType(
const std::vector<FileAttachment>& files) {
if (files.empty())
return FileAttachment::Type::kUnknown;

FileAttachment::Type type = files[0].type();
for (size_t i = 1; i < files.size(); ++i) {
if (files[i].type() != type)
return FileAttachment::Type::kUnknown;
}
return type;
}

TextAttachment::Type GetCommonTextAttachmentType(
const std::vector<TextAttachment>& texts) {
if (texts.empty())
return TextAttachment::Type::kText;

TextAttachment::Type type = texts[0].type();
for (size_t i = 1; i < texts.size(); ++i) {
if (texts[i].type() != type)
return TextAttachment::Type::kText;
}
return type;
}

base::string16 GetUnknownAttachmentsString(size_t count) {
// TODO(crbug.com/1102348): Provide translated string.
return base::string16();
}

base::string16 GetFileAttachmentsString(
const std::vector<FileAttachment>& files) {
// TODO(crbug.com/1102348): Add translated special cases for file types.
switch (GetCommonFileAttachmentType(files)) {
default:
return GetUnknownAttachmentsString(files.size());
}
}

base::string16 GetTextAttachmentsString(
const std::vector<TextAttachment>& texts) {
// TODO(crbug.com/1102348): Add translated special cases for text types.
switch (GetCommonTextAttachmentType(texts)) {
default:
return GetUnknownAttachmentsString(texts.size());
}
}

base::string16 GetAttachmentsString(const ShareTarget& share_target) {
size_t file_count = share_target.file_attachments().size();
size_t text_count = share_target.text_attachments().size();

if (file_count > 0 && text_count == 0)
return GetFileAttachmentsString(share_target.file_attachments());

if (text_count > 0 && file_count == 0)
return GetTextAttachmentsString(share_target.text_attachments());

return GetUnknownAttachmentsString(file_count + text_count);
}

} // namespace

NearbyNotificationManager::NearbyNotificationManager(Profile* profile)
: profile_(profile) {}

NearbyNotificationManager::~NearbyNotificationManager() = default;

void NearbyNotificationManager::ShowProgress(
const ShareTarget& share_target,
const TransferMetadata& transfer_metadata) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

message_center::RichNotificationData rich_notification_data;
rich_notification_data.never_timeout = true;

message_center::Notification notification(
message_center::NOTIFICATION_TYPE_PROGRESS, kNearbyNotificationId,
// TODO(crbug.com/1102348): Provide translated title.
/*title=*/GetAttachmentsString(share_target),
/*message=*/base::string16(),
/*icon=*/gfx::Image(),
// TODO(crbug.com/1102348): Provide translated source.
/*display_source=*/base::string16(),
/*origin_url=*/GURL(),
message_center::NotifierId(message_center::NotifierType::SYSTEM_COMPONENT,
kNearbyNotifier),
rich_notification_data,
/*delegate=*/nullptr);

// TODO(crbug.com/1102348): Set Nearby Share icon.
notification.set_progress(100.0 * transfer_metadata.progress());

std::vector<message_center::ButtonInfo> notification_actions;
notification_actions.emplace_back(l10n_util::GetStringUTF16(IDS_APP_CANCEL));
notification.set_buttons(notification_actions);

NotificationDisplayServiceFactory::GetForProfile(profile_)->Display(
NotificationHandler::Type::NEARBY_SHARE, notification,
/*metadata=*/nullptr);
}
31 changes: 31 additions & 0 deletions chrome/browser/nearby_sharing/nearby_notification_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2020 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_NEARBY_SHARING_NEARBY_NOTIFICATION_MANAGER_H_
#define CHROME_BROWSER_NEARBY_SHARING_NEARBY_NOTIFICATION_MANAGER_H_

#include "base/optional.h"
#include "base/strings/string16.h"
#include "base/time/time.h"
#include "chrome/browser/nearby_sharing/share_target.h"
#include "chrome/browser/nearby_sharing/transfer_metadata.h"

class Profile;

// Manages notifications shown for Nearby Share. Only a single notification will
// be shown as simultaneous connections are not supported. All methods should be
// called from the UI thread.
class NearbyNotificationManager {
public:
explicit NearbyNotificationManager(Profile* profile);
~NearbyNotificationManager();

void ShowProgress(const ShareTarget& share_target,
const TransferMetadata& transfer_metadata);

private:
Profile* profile_;
};

#endif // CHROME_BROWSER_NEARBY_SHARING_NEARBY_NOTIFICATION_MANAGER_H_

0 comments on commit 257bcab

Please sign in to comment.