1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/app_list/arc/arc_app_launcher.h"
#include <memory>
#include <utility>
#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/profiles/profile.h"
#include "components/services/app_service/public/cpp/app_launch_util.h"
#include "ui/events/event_constants.h"
ArcAppLauncher::ArcAppLauncher(content::BrowserContext* context,
const std::string& app_id,
apps::IntentPtr launch_intent,
bool deferred_launch_allowed,
int64_t display_id,
apps::LaunchSource launch_source)
: context_(context),
app_id_(app_id),
launch_intent_(std::move(launch_intent)),
deferred_launch_allowed_(deferred_launch_allowed),
display_id_(display_id),
launch_source_(launch_source) {
ArcAppListPrefs* prefs = ArcAppListPrefs::Get(context_);
DCHECK(prefs);
std::unique_ptr<ArcAppListPrefs::AppInfo> app_info = prefs->GetApp(app_id_);
if (!app_info ||
!MaybeLaunchApp(app_id, *app_info, apps::Readiness::kUnknown)) {
arc_app_list_prefs_observer_.Observe(prefs);
}
auto* profile = Profile::FromBrowserContext(context_);
DCHECK(
apps::AppServiceProxyFactory::IsAppServiceAvailableForProfile(profile));
app_registry_cache_observer_.Observe(
&apps::AppServiceProxyFactory::GetForProfile(profile)
->AppRegistryCache());
}
ArcAppLauncher::~ArcAppLauncher() {
if (!app_launched_) {
VLOG(2) << "App " << app_id_ << "was not launched.";
}
}
void ArcAppLauncher::OnAppRegistered(
const std::string& app_id,
const ArcAppListPrefs::AppInfo& app_info) {
MaybeLaunchApp(app_id, app_info, apps::Readiness::kUnknown);
}
void ArcAppLauncher::OnAppStatesChanged(
const std::string& app_id,
const ArcAppListPrefs::AppInfo& app_info) {
MaybeLaunchApp(app_id, app_info, apps::Readiness::kUnknown);
}
void ArcAppLauncher::OnArcAppListPrefsDestroyed() {
arc_app_list_prefs_observer_.Reset();
}
void ArcAppLauncher::OnAppUpdate(const apps::AppUpdate& update) {
if (update.AppId() != app_id_ ||
update.Readiness() != apps::Readiness::kReady) {
return;
}
ArcAppListPrefs* prefs = ArcAppListPrefs::Get(context_);
DCHECK(prefs);
const std::unique_ptr<ArcAppListPrefs::AppInfo> app_info =
prefs->GetApp(app_id_);
if (!app_info)
return;
MaybeLaunchApp(app_id_, *app_info, apps::Readiness::kReady);
}
void ArcAppLauncher::OnAppRegistryCacheWillBeDestroyed(
apps::AppRegistryCache* cache) {
app_registry_cache_observer_.Reset();
}
bool ArcAppLauncher::MaybeLaunchApp(const std::string& app_id,
const ArcAppListPrefs::AppInfo& app_info,
apps::Readiness readiness) {
if (app_launched_)
return true;
if (app_id != app_id_ || (!app_info.ready && !deferred_launch_allowed_) ||
app_info.suspended) {
return false;
}
auto* profile = Profile::FromBrowserContext(context_);
DCHECK(
apps::AppServiceProxyFactory::IsAppServiceAvailableForProfile(profile));
auto* proxy = apps::AppServiceProxyFactory::GetForProfile(profile);
if (readiness == apps::Readiness::kUnknown) {
if (proxy->AppRegistryCache().GetAppType(app_id) ==
apps::AppType::kUnknown) {
return false;
}
proxy->AppRegistryCache().ForOneApp(
app_id, [&readiness](const apps::AppUpdate& update) {
readiness = update.Readiness();
});
}
// Launch requests disabled by local settings should go through to App service
// This is to ensure that the blocked app dialog is shown.
if (readiness != apps::Readiness::kReady &&
readiness != apps::Readiness::kDisabledByLocalSettings) {
return false;
}
ArcAppListPrefs* prefs = ArcAppListPrefs::Get(context_);
DCHECK(prefs && prefs->GetApp(app_id_));
app_registry_cache_observer_.Reset();
arc_app_list_prefs_observer_.Reset();
if (launch_intent_) {
proxy->LaunchAppWithIntent(
app_id_, ui::EF_NONE, std::move(launch_intent_), launch_source_,
std::make_unique<apps::WindowInfo>(display_id_), base::DoNothing());
} else {
proxy->Launch(app_id_, ui::EF_NONE, launch_source_,
std::make_unique<apps::WindowInfo>(display_id_));
}
app_launched_ = true;
return true;
}
|