1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/app_list/search/arc/arc_app_shortcut_search_result.h"
6
7 #include <string>
8 #include <utility>
9
10 #include "ash/public/cpp/app_list/app_list_config.h"
11 #include "ash/public/cpp/app_list/app_list_types.h"
12 #include "base/bind.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/apps/app_service/app_icon_factory.h"
15 #include "chrome/browser/chromeos/arc/icon_decode_request.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h"
18 #include "chrome/browser/ui/app_list/app_service/app_service_app_icon_loader.h"
19 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h"
20 #include "chrome/browser/ui/app_list/arc/arc_app_utils.h"
21 #include "chrome/common/chrome_features.h"
22 #include "chrome/grit/generated_resources.h"
23 #include "ui/base/l10n/l10n_util.h"
24
25 namespace app_list {
26
27 namespace {
28 constexpr char kAppShortcutSearchPrefix[] = "appshortcutsearch://";
29 } // namespace
30
ArcAppShortcutSearchResult(arc::mojom::AppShortcutItemPtr data,Profile * profile,AppListControllerDelegate * list_controller,bool is_recommendation)31 ArcAppShortcutSearchResult::ArcAppShortcutSearchResult(
32 arc::mojom::AppShortcutItemPtr data,
33 Profile* profile,
34 AppListControllerDelegate* list_controller,
35 bool is_recommendation)
36 : data_(std::move(data)),
37 profile_(profile),
38 list_controller_(list_controller) {
39 SetTitle(base::UTF8ToUTF16(data_->short_label));
40 set_id(kAppShortcutSearchPrefix + GetAppId() + "/" + data_->shortcut_id);
41 SetAccessibleName(ComputeAccessibleName());
42 SetResultType(ash::AppListSearchResultType::kArcAppShortcut);
43 SetDisplayType(ash::SearchResultDisplayType::kTile);
44 SetMetricsType(ash::PLAY_STORE_APP_SHORTCUT);
45 SetIsRecommendation(is_recommendation);
46
47 const int icon_dimension =
48 ash::AppListConfig::instance().search_tile_icon_dimension();
49 if (base::FeatureList::IsEnabled(features::kAppServiceAdaptiveIcon)) {
50 DCHECK(data_->icon);
51 apps::ArcRawIconPngDataToImageSkia(
52 std::move(data_->icon), icon_dimension,
53 base::BindOnce(&ArcAppShortcutSearchResult::SetIcon,
54 weak_ptr_factory_.GetWeakPtr()));
55 } else if (data_->icon && data_->icon->icon_png_data &&
56 !data_->icon->icon_png_data->empty()) {
57 icon_decode_request_ = std::make_unique<arc::IconDecodeRequest>(
58 base::BindOnce(&ArcAppShortcutSearchResult::SetIcon,
59 weak_ptr_factory_.GetWeakPtr()),
60 icon_dimension);
61 icon_decode_request_->StartWithOptions(data_->icon->icon_png_data.value());
62 } else {
63 // TODO(crbug.com/1083331): Remove when the ARC change is rolled in Chrome
64 // OS.
65 icon_decode_request_ = std::make_unique<arc::IconDecodeRequest>(
66 base::BindOnce(&ArcAppShortcutSearchResult::SetIcon,
67 weak_ptr_factory_.GetWeakPtr()),
68 icon_dimension);
69 icon_decode_request_->StartWithOptions(data_->icon_png);
70 }
71
72 badge_icon_loader_ = std::make_unique<AppServiceAppIconLoader>(
73 profile_,
74 ash::AppListConfig::instance().search_tile_badge_icon_dimension(), this);
75 badge_icon_loader_->FetchImage(GetAppId());
76 }
77
78 ArcAppShortcutSearchResult::~ArcAppShortcutSearchResult() = default;
79
Open(int event_flags)80 void ArcAppShortcutSearchResult::Open(int event_flags) {
81 arc::LaunchAppShortcutItem(profile_, GetAppId(), data_->shortcut_id,
82 list_controller_->GetAppListDisplayId());
83 }
84
OnAppImageUpdated(const std::string & app_id,const gfx::ImageSkia & image)85 void ArcAppShortcutSearchResult::OnAppImageUpdated(
86 const std::string& app_id,
87 const gfx::ImageSkia& image) {
88 SetBadgeIcon(image);
89 }
90
GetAppId() const91 std::string ArcAppShortcutSearchResult::GetAppId() const {
92 if (!data_->package_name)
93 return std::string();
94 const ArcAppListPrefs* arc_prefs = ArcAppListPrefs::Get(profile_);
95 DCHECK(arc_prefs);
96 return arc_prefs->GetAppIdByPackageName(data_->package_name.value());
97 }
98
ComputeAccessibleName() const99 base::string16 ArcAppShortcutSearchResult::ComputeAccessibleName() const {
100 const ArcAppListPrefs* arc_prefs = ArcAppListPrefs::Get(profile_);
101 DCHECK(arc_prefs);
102 std::unique_ptr<ArcAppListPrefs::AppInfo> app_info =
103 arc_prefs->GetApp(GetAppId());
104 if (!app_info.get())
105 return base::string16();
106
107 return l10n_util::GetStringFUTF16(IDS_APP_ACTION_SHORTCUT_ACCESSIBILITY_NAME,
108 base::UTF8ToUTF16(data_->short_label),
109 base::UTF8ToUTF16(app_info->name));
110 }
111
112 } // namespace app_list
113