Commit b330e5ab authored by Melissa Zhang's avatar Melissa Zhang Committed by Commit Bot

[Sharesheet] Pass ShareActions to Sharesheet as Targets

This CL gets ShareActions from the ShareActionCache and plums
them through to the Sharesheet bubble. It also adds a Share
label to the bubble.

Bug: 1097623
Change-Id: I98d17217f5b0fd1790c9866bb8bed4fc5a05bfed
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2301262Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Commit-Queue: Melissa Zhang <melzhang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#790259}
parent f5db3bc2
...@@ -1493,6 +1493,8 @@ static_library("browser") { ...@@ -1493,6 +1493,8 @@ static_library("browser") {
"sharesheet/share_action.h", "sharesheet/share_action.h",
"sharesheet/sharesheet_action_cache.cc", "sharesheet/sharesheet_action_cache.cc",
"sharesheet/sharesheet_action_cache.h", "sharesheet/sharesheet_action_cache.h",
"sharesheet/sharesheet_types.cc",
"sharesheet/sharesheet_types.h",
"sharing/ack_message_handler.cc", "sharing/ack_message_handler.cc",
"sharing/ack_message_handler.h", "sharing/ack_message_handler.h",
"sharing/click_to_call/feature.cc", "sharing/click_to_call/feature.cc",
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "chrome/browser/sharesheet/sharesheet_controller.h" #include "chrome/browser/sharesheet/sharesheet_controller.h"
#include "ui/gfx/image/image.h"
#include "ui/views/view.h" #include "ui/views/view.h"
namespace sharesheet { namespace sharesheet {
...@@ -14,27 +15,30 @@ namespace sharesheet { ...@@ -14,27 +15,30 @@ namespace sharesheet {
// An interface implemented by each ShareAction. // An interface implemented by each ShareAction.
class ShareAction { class ShareAction {
public: public:
explicit ShareAction(SharesheetController* controller);
virtual ~ShareAction() = default; virtual ~ShareAction() = default;
virtual const base::string16 GetActionName() = 0; virtual const base::string16 GetActionName() = 0;
virtual const gfx::Image GetActionIcon() = 0;
// LaunchAction should synchronously create all UI needed and fill // LaunchAction should synchronously create all UI needed and fill
// the |root_view|. Methods on |controller| can be used to inform // the |root_view|. Methods on |controller| can be used to inform
// the sharesheet about the lifecycle of the ShareAction. // the sharesheet about the lifecycle of the ShareAction.
// //
// |root_view| is a container within the larger share_sheet which should act // |root_view| is a container within the larger share_sheet which should act
// as the parent view for ShareAction views. It is guaranteed that // as the parent view for ShareAction views. It is guaranteed that
// |root_view| will stay alive and visible until either // |root_view| and |controller| will stay alive and visible until either
// ShareAction::OnClosing is called, or the ShareAction calls // ShareAction::OnClosing is called, or the ShareAction calls
// |controller|->ShareActionCompleted(). // |controller|->ShareActionCompleted().
virtual void LaunchAction(views::View* root_view) = 0; virtual void LaunchAction(SharesheetController* controller,
views::View* root_view) = 0;
// OnClosing informs the ShareAction when the sharesheet is closed. This
// occurs when the user presses the back button out of the share action view // OnClosing informs the ShareAction when the sharesheet with |controller| is
// or closes the sharesheet. All processes in ShareAction should shutdown when // closed. This occurs when the user presses the back button out of the share
// OnClosing is called, and not use |root_view| once the method completes. // action view or closes the sharesheet. All processes in ShareAction should
virtual void OnClosing() = 0; // shutdown when OnClosing is called, and not use |root_view| or |controller|
// once the method completes as they will be destroyed.
virtual void OnClosing(SharesheetController* controller) = 0;
}; };
} // namespace sharesheet } // namespace sharesheet
......
...@@ -4,10 +4,30 @@ ...@@ -4,10 +4,30 @@
#include "chrome/browser/sharesheet/sharesheet_action_cache.h" #include "chrome/browser/sharesheet/sharesheet_action_cache.h"
#include "chrome/browser/sharesheet/share_action.h"
namespace sharesheet { namespace sharesheet {
SharesheetActionCache::SharesheetActionCache() = default; SharesheetActionCache::SharesheetActionCache() = default;
SharesheetActionCache::~SharesheetActionCache() = default; SharesheetActionCache::~SharesheetActionCache() = default;
const std::vector<std::unique_ptr<ShareAction>>&
SharesheetActionCache::GetShareActions() {
return share_actions_;
}
ShareAction* SharesheetActionCache::GetActionFromName(
const base::string16& action_name) {
auto iter = share_actions_.begin();
while (iter != share_actions_.end()) {
if ((*iter)->GetActionName() == action_name) {
return iter->get();
} else {
iter++;
}
}
return nullptr;
}
} // namespace sharesheet } // namespace sharesheet
...@@ -5,8 +5,15 @@ ...@@ -5,8 +5,15 @@
#ifndef CHROME_BROWSER_SHARESHEET_SHARESHEET_ACTION_CACHE_H_ #ifndef CHROME_BROWSER_SHARESHEET_SHARESHEET_ACTION_CACHE_H_
#define CHROME_BROWSER_SHARESHEET_SHARESHEET_ACTION_CACHE_H_ #define CHROME_BROWSER_SHARESHEET_SHARESHEET_ACTION_CACHE_H_
#include <memory>
#include <vector>
#include "base/strings/string16.h"
namespace sharesheet { namespace sharesheet {
class ShareAction;
// The SharesheetActionCache facilitates communication between ShareActions // The SharesheetActionCache facilitates communication between ShareActions
// and the SharesheetService. // and the SharesheetService.
class SharesheetActionCache { class SharesheetActionCache {
...@@ -16,6 +23,13 @@ class SharesheetActionCache { ...@@ -16,6 +23,13 @@ class SharesheetActionCache {
SharesheetActionCache(const SharesheetActionCache&) = delete; SharesheetActionCache(const SharesheetActionCache&) = delete;
SharesheetActionCache& operator=(const SharesheetActionCache&) = delete; SharesheetActionCache& operator=(const SharesheetActionCache&) = delete;
ShareAction* GetActionFromName(const base::string16& action_name);
const std::vector<std::unique_ptr<ShareAction>>& GetShareActions();
private:
std::vector<std::unique_ptr<ShareAction>> share_actions_;
}; };
} // namespace sharesheet } // namespace sharesheet
......
...@@ -13,6 +13,10 @@ class SharesheetController { ...@@ -13,6 +13,10 @@ class SharesheetController {
public: public:
virtual ~SharesheetController() = default; virtual ~SharesheetController() = default;
// Each Controller is assigned a unique id used to distinuish between
// different invocations of the sharesheet.
virtual uint32_t GetId() = 0;
// Called by ShareAction to notify SharesheetBubbleView that ShareAction // Called by ShareAction to notify SharesheetBubbleView that ShareAction
// has completed. // has completed.
virtual void ShareActionCompleted() = 0; virtual void ShareActionCompleted() = 0;
......
...@@ -6,7 +6,9 @@ ...@@ -6,7 +6,9 @@
#include <utility> #include <utility>
#include "chrome/browser/sharesheet/share_action.h"
#include "chrome/browser/sharesheet/sharesheet_service_delegate.h" #include "chrome/browser/sharesheet/sharesheet_service_delegate.h"
#include "chrome/browser/sharesheet/sharesheet_types.h"
#include "ui/views/view.h" #include "ui/views/view.h"
namespace sharesheet { namespace sharesheet {
...@@ -21,7 +23,17 @@ void SharesheetService::ShowBubble(views::View* bubble_anchor_view) { ...@@ -21,7 +23,17 @@ void SharesheetService::ShowBubble(views::View* bubble_anchor_view) {
std::make_unique<SharesheetServiceDelegate>( std::make_unique<SharesheetServiceDelegate>(
delegate_counter_++, std::move(bubble_anchor_view), this); delegate_counter_++, std::move(bubble_anchor_view), this);
sharesheet_service_delegate->ShowBubble(); std::vector<TargetInfo> targets;
auto& actions = sharesheet_action_cache_->GetShareActions();
auto iter = actions.begin();
while (iter != actions.end()) {
targets.emplace(targets.begin(), TargetType::kAction,
(*iter)->GetActionIcon(), (*iter)->GetActionName(),
(*iter)->GetActionName());
++iter;
}
sharesheet_service_delegate->ShowBubble(std::move(targets));
active_delegates_.push_back(std::move(sharesheet_service_delegate)); active_delegates_.push_back(std::move(sharesheet_service_delegate));
} }
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "chrome/browser/sharesheet/sharesheet_service_delegate.h" #include "chrome/browser/sharesheet/sharesheet_service_delegate.h"
#include <utility>
#include "base/bind.h" #include "base/bind.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sharesheet/sharesheet_service.h" #include "chrome/browser/sharesheet/sharesheet_service.h"
...@@ -24,21 +26,21 @@ SharesheetServiceDelegate::SharesheetServiceDelegate( ...@@ -24,21 +26,21 @@ SharesheetServiceDelegate::SharesheetServiceDelegate(
SharesheetServiceDelegate::~SharesheetServiceDelegate() = default; SharesheetServiceDelegate::~SharesheetServiceDelegate() = default;
uint32_t SharesheetServiceDelegate::GetId() { void SharesheetServiceDelegate::ShowBubble(std::vector<TargetInfo> targets) {
return id_; sharesheet_bubble_view_->ShowBubble(std::move(targets));
} }
void SharesheetServiceDelegate::ShowBubble() { void SharesheetServiceDelegate::OnBubbleClosed() {
sharesheet_bubble_view_->ShowBubble(); sharesheet_bubble_view_.release();
sharesheet_service_->OnBubbleClosed(id_);
} }
void SharesheetServiceDelegate::ShareActionCompleted() { uint32_t SharesheetServiceDelegate::GetId() {
sharesheet_bubble_view_->CloseBubble(); return id_;
} }
void SharesheetServiceDelegate::OnBubbleClosed() { void SharesheetServiceDelegate::ShareActionCompleted() {
sharesheet_bubble_view_.release(); sharesheet_bubble_view_->CloseBubble();
sharesheet_service_->OnBubbleClosed(id_);
} }
} // namespace sharesheet } // namespace sharesheet
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <memory> #include <memory>
#include "chrome/browser/sharesheet/sharesheet_controller.h" #include "chrome/browser/sharesheet/sharesheet_controller.h"
#include "chrome/browser/sharesheet/sharesheet_types.h"
class SharesheetBubbleView; class SharesheetBubbleView;
...@@ -31,16 +32,15 @@ class SharesheetServiceDelegate : public SharesheetController { ...@@ -31,16 +32,15 @@ class SharesheetServiceDelegate : public SharesheetController {
SharesheetServiceDelegate& operator=(const SharesheetServiceDelegate&) = SharesheetServiceDelegate& operator=(const SharesheetServiceDelegate&) =
delete; delete;
uint32_t GetId(); void ShowBubble(std::vector<TargetInfo> targets);
void ShowBubble();
void OnBubbleClosed(); void OnBubbleClosed();
// SharesheetController overrides // SharesheetController overrides
uint32_t GetId() override;
void ShareActionCompleted() override; void ShareActionCompleted() override;
private: private:
uint32_t id_; const uint32_t id_;
std::unique_ptr<SharesheetBubbleView> sharesheet_bubble_view_; std::unique_ptr<SharesheetBubbleView> sharesheet_bubble_view_;
SharesheetService* sharesheet_service_; SharesheetService* sharesheet_service_;
}; };
......
// 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/sharesheet/sharesheet_types.h"
namespace sharesheet {
TargetInfo::TargetInfo(TargetType type,
const gfx::Image& icon,
const base::string16& launch_name,
const base::string16& display_name)
: type(type),
icon(icon),
launch_name(launch_name),
display_name(display_name) {}
TargetInfo::TargetInfo(TargetInfo&& other) = default;
TargetInfo& TargetInfo::operator=(TargetInfo&& other) = default;
} // namespace sharesheet
// 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_SHARESHEET_SHARESHEET_TYPES_H_
#define CHROME_BROWSER_SHARESHEET_SHARESHEET_TYPES_H_
#include "base/strings/string16.h"
#include "ui/gfx/image/image.h"
namespace sharesheet {
// The type of a target.
enum class TargetType {
kUnknown = 0,
kApp,
kAction,
};
struct TargetInfo {
TargetInfo(TargetType type,
const gfx::Image& icon,
const base::string16& launch_name,
const base::string16& display_name);
// Allow move.
TargetInfo(TargetInfo&& other);
TargetInfo& operator=(TargetInfo&& other);
// Disallow copy and assign.
TargetInfo(const TargetInfo&) = delete;
TargetInfo& operator=(const TargetInfo&) = delete;
// The type of target that this object represents.
TargetType type;
// The icon to be displayed for this target in the sharesheet bubble.
gfx::Image icon;
// The string used to launch this target. Represents an Android package name
// when the app type is kArc.
base::string16 launch_name;
// The string shown to the user to identify this target in the sharesheet
// bubble.
base::string16 display_name;
};
} // namespace sharesheet
#endif // CHROME_BROWSER_SHARESHEET_SHARESHEET_TYPES_H_
...@@ -4,13 +4,26 @@ ...@@ -4,13 +4,26 @@
#include "chrome/browser/ui/views/sharesheet_bubble_view.h" #include "chrome/browser/ui/views/sharesheet_bubble_view.h"
#include <utility>
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/sharesheet/sharesheet_service_delegate.h" #include "chrome/browser/sharesheet/sharesheet_service_delegate.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h" #include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "ui/gfx/geometry/insets.h" #include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/size.h" #include "ui/gfx/geometry/size.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/layout/box_layout.h" #include "ui/views/layout/box_layout.h"
#include "ui/views/widget/widget.h" #include "ui/views/widget/widget.h"
namespace {
constexpr int kColumnSetIdTitle = 0;
constexpr int kVerticalSpacing = 24;
constexpr char kTitle[] = "Share";
} // namespace
SharesheetBubbleView::SharesheetBubbleView( SharesheetBubbleView::SharesheetBubbleView(
views::View* anchor_view, views::View* anchor_view,
sharesheet::SharesheetServiceDelegate* delegate) sharesheet::SharesheetServiceDelegate* delegate)
...@@ -23,11 +36,51 @@ SharesheetBubbleView::SharesheetBubbleView( ...@@ -23,11 +36,51 @@ SharesheetBubbleView::SharesheetBubbleView(
views::BoxLayout::Orientation::kVertical, gfx::Insets(), views::BoxLayout::Orientation::kVertical, gfx::Insets(),
ChromeLayoutProvider::Get()->GetDistanceMetric( ChromeLayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_RELATED_CONTROL_VERTICAL))); views::DISTANCE_RELATED_CONTROL_VERTICAL)));
auto root_view = std::make_unique<views::View>();
root_view->SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kVertical, gfx::Insets(),
ChromeLayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_RELATED_CONTROL_VERTICAL)));
root_view_ = AddChildView(std::move(root_view));
auto main_view = std::make_unique<views::View>();
main_view_ = root_view_->AddChildView(std::move(main_view));
auto share_action_view = std::make_unique<views::View>();
share_action_view->SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kVertical, gfx::Insets(),
ChromeLayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_RELATED_CONTROL_VERTICAL)));
share_action_view_ = root_view_->AddChildView(std::move(share_action_view));
share_action_view_->SetVisible(false);
} }
SharesheetBubbleView::~SharesheetBubbleView() = default; SharesheetBubbleView::~SharesheetBubbleView() = default;
void SharesheetBubbleView::ShowBubble() { void SharesheetBubbleView::ShowBubble(std::vector<TargetInfo> targets) {
targets_ = std::move(targets);
auto* main_layout =
main_view_->SetLayoutManager(std::make_unique<views::GridLayout>());
// Set up columnsets
views::ColumnSet* cs = main_layout->AddColumnSet(kColumnSetIdTitle);
cs->AddColumn(/* h_align */ views::GridLayout::FILL,
/* v_align */ views::GridLayout::CENTER,
/* resize_percent */ 1.0,
views::GridLayout::ColumnSize::kUsePreferred,
/* fixed_width */ 0, /*min_width*/ 0);
// Add Title label
main_layout->AddPaddingRow(views::GridLayout::kFixedSize, kVerticalSpacing);
main_layout->StartRow(views::GridLayout::kFixedSize, kColumnSetIdTitle,
/* height */ kVerticalSpacing);
auto* title = main_layout->AddView(std::make_unique<views::Label>(
base::UTF8ToUTF16(base::StringPiece(kTitle)),
views::style::CONTEXT_DIALOG_TITLE, views::style::STYLE_PRIMARY));
title->SetHorizontalAlignment(gfx::ALIGN_CENTER);
views::Widget* widget = views::BubbleDialogDelegateView::CreateBubble(this); views::Widget* widget = views::BubbleDialogDelegateView::CreateBubble(this);
GetWidget()->GetRootView()->Layout(); GetWidget()->GetRootView()->Layout();
widget->Show(); widget->Show();
......
...@@ -5,6 +5,9 @@ ...@@ -5,6 +5,9 @@
#ifndef CHROME_BROWSER_UI_VIEWS_SHARESHEET_BUBBLE_VIEW_H_ #ifndef CHROME_BROWSER_UI_VIEWS_SHARESHEET_BUBBLE_VIEW_H_
#define CHROME_BROWSER_UI_VIEWS_SHARESHEET_BUBBLE_VIEW_H_ #define CHROME_BROWSER_UI_VIEWS_SHARESHEET_BUBBLE_VIEW_H_
#include <vector>
#include "chrome/browser/sharesheet/sharesheet_types.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h" #include "ui/views/bubble/bubble_dialog_delegate_view.h"
namespace sharesheet { namespace sharesheet {
...@@ -13,13 +16,15 @@ class SharesheetServiceDelegate; ...@@ -13,13 +16,15 @@ class SharesheetServiceDelegate;
class SharesheetBubbleView : public views::BubbleDialogDelegateView { class SharesheetBubbleView : public views::BubbleDialogDelegateView {
public: public:
using TargetInfo = sharesheet::TargetInfo;
SharesheetBubbleView(views::View* anchor_view, SharesheetBubbleView(views::View* anchor_view,
sharesheet::SharesheetServiceDelegate* delegate); sharesheet::SharesheetServiceDelegate* delegate);
SharesheetBubbleView(const SharesheetBubbleView&) = delete; SharesheetBubbleView(const SharesheetBubbleView&) = delete;
SharesheetBubbleView& operator=(const SharesheetBubbleView&) = delete; SharesheetBubbleView& operator=(const SharesheetBubbleView&) = delete;
~SharesheetBubbleView() override; ~SharesheetBubbleView() override;
void ShowBubble(); void ShowBubble(std::vector<TargetInfo> targets);
void CloseBubble(); void CloseBubble();
// views::BubbleDialogDelegateView overrides // views::BubbleDialogDelegateView overrides
...@@ -29,6 +34,11 @@ class SharesheetBubbleView : public views::BubbleDialogDelegateView { ...@@ -29,6 +34,11 @@ class SharesheetBubbleView : public views::BubbleDialogDelegateView {
private: private:
// Owns this class. // Owns this class.
sharesheet::SharesheetServiceDelegate* delegate_; sharesheet::SharesheetServiceDelegate* delegate_;
std::vector<TargetInfo> targets_;
views::View* root_view_ = nullptr;
views::View* main_view_ = nullptr;
views::View* share_action_view_ = nullptr;
}; };
#endif // CHROME_BROWSER_UI_VIEWS_SHARESHEET_BUBBLE_VIEW_H_ #endif // CHROME_BROWSER_UI_VIEWS_SHARESHEET_BUBBLE_VIEW_H_
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