| // Copyright 2019 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/ui/intent_picker_tab_helper.h" |
| |
| #include <utility> |
| |
| #include "base/bind.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/intent_helper/intent_picker_helpers.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/ui/browser.h" |
| #include "chrome/browser/ui/browser_finder.h" |
| #include "chrome/browser/ui/browser_window.h" |
| #include "chrome/browser/web_applications/web_app_provider.h" |
| #include "content/public/browser/navigation_handle.h" |
| #include "third_party/abseil-cpp/absl/types/optional.h" |
| #include "ui/base/models/image_model.h" |
| #include "ui/gfx/favicon_size.h" |
| #include "ui/gfx/image/image.h" |
| |
| namespace { |
| |
| apps::mojom::AppType GetAppType(apps::PickerEntryType picker_entry_type) { |
| apps::mojom::AppType app_type = apps::mojom::AppType::kUnknown; |
| switch (picker_entry_type) { |
| case apps::PickerEntryType::kUnknown: |
| case apps::PickerEntryType::kDevice: |
| break; |
| case apps::PickerEntryType::kArc: |
| app_type = apps::mojom::AppType::kArc; |
| break; |
| case apps::PickerEntryType::kWeb: |
| app_type = apps::mojom::AppType::kWeb; |
| break; |
| case apps::PickerEntryType::kMacOs: |
| app_type = apps::mojom::AppType::kMacOs; |
| break; |
| } |
| return app_type; |
| } |
| |
| web_app::WebAppRegistrar* MaybeGetWebAppRegistrar( |
| content::WebContents* web_contents) { |
| // Profile for web contents might not contain a web app provider. eg. kiosk |
| // profile in Chrome OS. |
| auto* provider = web_app::WebAppProvider::GetForWebContents(web_contents); |
| return provider ? &provider->registrar() : nullptr; |
| } |
| |
| } // namespace |
| |
| IntentPickerTabHelper::~IntentPickerTabHelper() = default; |
| |
| // static |
| void IntentPickerTabHelper::SetShouldShowIcon( |
| content::WebContents* web_contents, |
| bool should_show_icon) { |
| IntentPickerTabHelper* tab_helper = FromWebContents(web_contents); |
| if (!tab_helper) |
| return; |
| tab_helper->should_show_icon_ = should_show_icon; |
| Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
| if (!browser) |
| return; |
| browser->window()->UpdatePageActionIcon(PageActionIconType::kIntentPicker); |
| } |
| |
| IntentPickerTabHelper::IntentPickerTabHelper(content::WebContents* web_contents) |
| : content::WebContentsObserver(web_contents), |
| registrar_(MaybeGetWebAppRegistrar(web_contents)) { |
| if (registrar_) |
| registrar_observation_.Observe(registrar_); |
| } |
| |
| // static |
| void IntentPickerTabHelper::LoadAppIcons( |
| content::WebContents* web_contents, |
| std::vector<apps::IntentPickerAppInfo> apps, |
| IntentPickerIconLoaderCallback callback) { |
| IntentPickerTabHelper* tab_helper = FromWebContents(web_contents); |
| if (!tab_helper) { |
| std::move(callback).Run(std::move(apps)); |
| return; |
| } |
| tab_helper->LoadAppIcon(std::move(apps), std::move(callback), 0); |
| } |
| |
| void IntentPickerTabHelper::OnAppIconLoaded( |
| std::vector<apps::IntentPickerAppInfo> apps, |
| IntentPickerIconLoaderCallback callback, |
| size_t index, |
| apps::mojom::IconValuePtr icon_value) { |
| apps[index].icon_model = |
| ui::ImageModel::FromImage(gfx::Image(icon_value->uncompressed)); |
| |
| if (index == apps.size() - 1) |
| std::move(callback).Run(std::move(apps)); |
| else |
| LoadAppIcon(std::move(apps), std::move(callback), index + 1); |
| } |
| |
| void IntentPickerTabHelper::LoadAppIcon( |
| std::vector<apps::IntentPickerAppInfo> apps, |
| IntentPickerIconLoaderCallback callback, |
| size_t index) { |
| if (index >= apps.size()) { |
| std::move(callback).Run(std::move(apps)); |
| return; |
| } |
| |
| const std::string& app_id = apps[index].launch_name; |
| auto app_type = GetAppType(apps[index].type); |
| |
| Profile* profile = |
| Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| |
| constexpr bool allow_placeholder_icon = false; |
| apps::AppServiceProxyFactory::GetForProfile(profile)->LoadIcon( |
| app_type, app_id, apps::mojom::IconType::kStandard, gfx::kFaviconSize, |
| allow_placeholder_icon, |
| base::BindOnce(&IntentPickerTabHelper::OnAppIconLoaded, |
| weak_factory_.GetWeakPtr(), std::move(apps), |
| std::move(callback), index)); |
| } |
| |
| void IntentPickerTabHelper::DidFinishNavigation( |
| content::NavigationHandle* navigation_handle) { |
| // For a http/https scheme URL navigation, we will check if the |
| // url can be handled by some apps, and show intent picker icon |
| // or bubble if there are some apps available. We only want to check this if |
| // the navigation happens in the main frame, and the navigation is not the |
| // same document with same URL. |
| // TODO(crbug.com/826982): Check is not error page here. Adding this check |
| // will break the browser test, given this is a refactor CL, will add check in |
| // follow up CL. |
| // TODO(https://crbug.com/1218946): With MPArch there may be multiple main |
| // frames. This caller was converted automatically to the primary main frame |
| // to preserve its semantics. Follow up to confirm correctness. |
| if (navigation_handle->IsInPrimaryMainFrame() && |
| navigation_handle->HasCommitted() && |
| (!navigation_handle->IsSameDocument() || |
| navigation_handle->GetURL() != |
| navigation_handle->GetPreviousMainFrameURL()) && |
| navigation_handle->GetURL().SchemeIsHTTPOrHTTPS()) { |
| apps::MaybeShowIntentPicker(navigation_handle); |
| } |
| } |
| |
| void IntentPickerTabHelper::OnWebAppWillBeUninstalled( |
| const web_app::AppId& app_id) { |
| // WebAppTabHelper has an app_id but it is reset during |
| // OnWebAppWillBeUninstalled so using FindAppWithUrlInScope. |
| absl::optional<web_app::AppId> local_app_id = |
| registrar_->FindAppWithUrlInScope(web_contents()->GetLastCommittedURL()); |
| if (app_id == local_app_id) |
| SetShouldShowIcon(web_contents(), false); |
| } |
| |
| void IntentPickerTabHelper::OnAppRegistrarDestroyed() { |
| registrar_observation_.Reset(); |
| } |
| |
| WEB_CONTENTS_USER_DATA_KEY_IMPL(IntentPickerTabHelper) |