Commit 59ca374f authored by Melissa Zhang's avatar Melissa Zhang Committed by Commit Bot

[Sharesheet] Integrate App Service

This CL integrates apps from the App Service into the
Sharesheet as targets. When clicked on, the app opens
with the supplied intent and the Sharesheet closes.

Bug: 1097623
Change-Id: I216f39ada019125807f92fd93e0d00f77d4622ae
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2306137
Commit-Queue: Melissa Zhang <melzhang@chromium.org>
Reviewed-by: default avatarMaggie Cai <mxcai@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#790722}
parent 5f72675b
......@@ -6,19 +6,29 @@
#include <utility>
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/apps/app_service/launch_utils.h"
#include "chrome/browser/sharesheet/share_action.h"
#include "chrome/browser/sharesheet/sharesheet_service_delegate.h"
#include "chrome/browser/sharesheet/sharesheet_types.h"
#include "ui/display/types/display_constants.h"
#include "ui/views/view.h"
namespace sharesheet {
SharesheetService::SharesheetService(Profile* profile)
: sharesheet_action_cache_(std::make_unique<SharesheetActionCache>()) {}
: sharesheet_action_cache_(std::make_unique<SharesheetActionCache>()),
app_service_proxy_(apps::AppServiceProxyFactory::GetForProfile(profile)) {
DCHECK(app_service_proxy_);
}
SharesheetService::~SharesheetService() = default;
void SharesheetService::ShowBubble(views::View* bubble_anchor_view) {
void SharesheetService::ShowBubble(views::View* bubble_anchor_view,
apps::mojom::IntentPtr intent) {
auto sharesheet_service_delegate =
std::make_unique<SharesheetServiceDelegate>(
delegate_counter_++, std::move(bubble_anchor_view), this);
......@@ -33,7 +43,17 @@ void SharesheetService::ShowBubble(views::View* bubble_anchor_view) {
++iter;
}
sharesheet_service_delegate->ShowBubble(std::move(targets));
std::vector<apps::AppIdAndActivityName> app_id_and_activities =
app_service_proxy_->GetAppsForIntent(intent);
for (const auto& app_id_and_activity : app_id_and_activities) {
// TODO(1097623) : Load Real Icons.
targets.emplace(targets.begin(), TargetType::kApp, gfx::Image(),
base::UTF8ToUTF16(app_id_and_activity.app_id),
base::UTF8ToUTF16(app_id_and_activity.activity_name));
}
sharesheet_service_delegate->ShowBubble(std::move(targets),
std::move(intent));
active_delegates_.push_back(std::move(sharesheet_service_delegate));
}
......@@ -60,19 +80,29 @@ void SharesheetService::OnBubbleClosed(uint32_t id,
void SharesheetService::OnTargetSelected(uint32_t delegate_id,
const base::string16& target_name,
const TargetType type,
apps::mojom::IntentPtr intent,
views::View* share_action_view) {
SharesheetServiceDelegate* delegate = GetDelegate(delegate_id);
if (delegate == nullptr)
return;
if (type == TargetType::kAction) {
ShareAction* share_action =
sharesheet_action_cache_->GetActionFromName(target_name);
if (share_action == nullptr)
return;
SharesheetServiceDelegate* delegate = GetDelegate(delegate_id);
if (delegate == nullptr)
return;
delegate->OnActionLaunched();
share_action->LaunchAction(delegate, share_action_view);
} else if (type == TargetType::kApp) {
// TODO(1097623) : Add LaunchSource::KFromSharesheet
auto launch_source = apps::mojom::LaunchSource::kFromLink;
app_service_proxy_->LaunchAppWithIntent(
base::UTF16ToUTF8(target_name),
apps::GetEventFlags(
apps::mojom::LaunchContainer::kLaunchContainerWindow,
WindowOpenDisposition::NEW_WINDOW,
/*prefer_container=*/true),
std::move(intent), launch_source, display::kDefaultDisplayId);
}
}
......
......@@ -12,9 +12,14 @@
#include "chrome/browser/sharesheet/sharesheet_action_cache.h"
#include "chrome/browser/sharesheet/sharesheet_types.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/services/app_service/public/mojom/types.mojom.h"
class Profile;
namespace apps {
class AppServiceProxy;
}
namespace views {
class View;
}
......@@ -33,17 +38,20 @@ class SharesheetService : public KeyedService {
SharesheetService(const SharesheetService&) = delete;
SharesheetService& operator=(const SharesheetService&) = delete;
void ShowBubble(views::View* bubble_anchor_view);
void ShowBubble(views::View* bubble_anchor_view,
apps::mojom::IntentPtr intent);
void OnBubbleClosed(uint32_t id, const base::string16& active_action);
void OnTargetSelected(uint32_t delegate_id,
const base::string16& target_name,
const TargetType type,
apps::mojom::IntentPtr intent,
views::View* share_action_view);
SharesheetServiceDelegate* GetDelegate(uint32_t delegate_id);
private:
uint32_t delegate_counter_ = 0;
std::unique_ptr<SharesheetActionCache> sharesheet_action_cache_;
apps::AppServiceProxy* app_service_proxy_;
// Record of all active SharesheetServiceDelegates. These can be retrieved
// by ShareActions and used as SharesheetControllers to make bubble changes.
......
......@@ -26,8 +26,9 @@ SharesheetServiceDelegate::SharesheetServiceDelegate(
SharesheetServiceDelegate::~SharesheetServiceDelegate() = default;
void SharesheetServiceDelegate::ShowBubble(std::vector<TargetInfo> targets) {
sharesheet_bubble_view_->ShowBubble(std::move(targets));
void SharesheetServiceDelegate::ShowBubble(std::vector<TargetInfo> targets,
apps::mojom::IntentPtr intent) {
sharesheet_bubble_view_->ShowBubble(std::move(targets), std::move(intent));
}
void SharesheetServiceDelegate::OnBubbleClosed(
......@@ -36,16 +37,17 @@ void SharesheetServiceDelegate::OnBubbleClosed(
sharesheet_service_->OnBubbleClosed(id_, active_action);
}
void SharesheetServiceDelegate::OnActionLaunched() {
sharesheet_bubble_view_->ShowActionView();
}
void SharesheetServiceDelegate::OnTargetSelected(
const base::string16& target_name,
const TargetType type,
apps::mojom::IntentPtr intent,
views::View* share_action_view) {
sharesheet_service_->OnTargetSelected(id_, target_name, type,
share_action_view);
}
void SharesheetServiceDelegate::OnActionLaunched() {
sharesheet_bubble_view_->ShowActionView();
std::move(intent), share_action_view);
}
uint32_t SharesheetServiceDelegate::GetId() {
......
......@@ -10,6 +10,7 @@
#include "base/strings/string16.h"
#include "chrome/browser/sharesheet/sharesheet_controller.h"
#include "chrome/browser/sharesheet/sharesheet_types.h"
#include "components/services/app_service/public/mojom/types.mojom.h"
class SharesheetBubbleView;
......@@ -33,10 +34,12 @@ class SharesheetServiceDelegate : public SharesheetController {
SharesheetServiceDelegate& operator=(const SharesheetServiceDelegate&) =
delete;
void ShowBubble(std::vector<TargetInfo> targets);
void ShowBubble(std::vector<TargetInfo> targets,
apps::mojom::IntentPtr intent);
void OnBubbleClosed(const base::string16& active_action);
void OnTargetSelected(const base::string16& target_name,
const TargetType type,
apps::mojom::IntentPtr intent,
views::View* share_action_view);
void OnActionLaunched();
......
......@@ -6,6 +6,7 @@
#include <memory>
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sharesheet/sharesheet_service.h"
......@@ -39,7 +40,9 @@ SharesheetServiceFactory* SharesheetServiceFactory::GetInstance() {
SharesheetServiceFactory::SharesheetServiceFactory()
: BrowserContextKeyedServiceFactory(
"SharesheetService",
BrowserContextDependencyManager::GetInstance()) {}
BrowserContextDependencyManager::GetInstance()) {
DependsOn(apps::AppServiceProxyFactory::GetInstance());
}
SharesheetServiceFactory::~SharesheetServiceFactory() = default;
......
......@@ -110,8 +110,10 @@ SharesheetBubbleView::SharesheetBubbleView(
SharesheetBubbleView::~SharesheetBubbleView() = default;
void SharesheetBubbleView::ShowBubble(std::vector<TargetInfo> targets) {
void SharesheetBubbleView::ShowBubble(std::vector<TargetInfo> targets,
apps::mojom::IntentPtr intent) {
targets_ = std::move(targets);
intent_ = std::move(intent);
auto* main_layout =
main_view_->SetLayoutManager(std::make_unique<views::GridLayout>());
......@@ -185,9 +187,13 @@ void SharesheetBubbleView::CloseBubble() {
void SharesheetBubbleView::ButtonPressed(views::Button* sender,
const ui::Event& event) {
active_target_ = targets_[sender->tag()].launch_name;
delegate_->OnTargetSelected(active_target_, targets_[sender->tag()].type,
share_action_view_);
auto type = targets_[sender->tag()].type;
if (type == sharesheet::TargetType::kAction) {
active_target_ = targets_[sender->tag()].launch_name;
}
delegate_->OnTargetSelected(targets_[sender->tag()].launch_name, type,
std::move(intent_), share_action_view_);
intent_.reset();
RequestFocus();
}
......
......@@ -8,6 +8,7 @@
#include <vector>
#include "chrome/browser/sharesheet/sharesheet_types.h"
#include "components/services/app_service/public/mojom/types.mojom.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"
#include "ui/views/controls/button/button.h"
......@@ -26,7 +27,8 @@ class SharesheetBubbleView : public views::BubbleDialogDelegateView,
SharesheetBubbleView& operator=(const SharesheetBubbleView&) = delete;
~SharesheetBubbleView() override;
void ShowBubble(std::vector<TargetInfo> targets);
void ShowBubble(std::vector<TargetInfo> targets,
apps::mojom::IntentPtr intent);
void ShowActionView();
void CloseBubble();
......@@ -46,6 +48,7 @@ class SharesheetBubbleView : public views::BubbleDialogDelegateView,
sharesheet::SharesheetServiceDelegate* delegate_;
std::vector<TargetInfo> targets_;
base::string16 active_target_;
apps::mojom::IntentPtr intent_;
views::View* root_view_ = nullptr;
views::View* main_view_ = nullptr;
......
......@@ -36,7 +36,8 @@ void SharesheetButton::ButtonPressed(views::Button* sender,
browser_->tab_strip_model()->GetActiveWebContents()->GetBrowserContext());
auto* sharesheet_service =
sharesheet::SharesheetServiceFactory::GetForProfile(profile);
sharesheet_service->ShowBubble(/* bubble_anchor_view */ this);
sharesheet_service->ShowBubble(/* bubble_anchor_view */ this,
/* intent */ nullptr);
}
int SharesheetButton::GetIconSize() const {
......
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