Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Alexey Baskakov | 2f8bf816 | 2018-07-17 01:06:11 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Song Fangzhen | 49468b2 | 2021-09-02 15:41:59 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_HELPERS_H_ |
| 6 | #define CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_HELPERS_H_ |
Alexey Baskakov | 2f8bf816 | 2018-07-17 01:06:11 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | |
Song Fangzhen | cda4af6 | 2021-09-09 05:24:02 | [diff] [blame] | 10 | #include "chrome/browser/web_applications/web_app_id.h" |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame] | 11 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Alan Cutter | e511d18a | 2021-08-04 23:10:38 | [diff] [blame] | 12 | #include "third_party/blink/public/mojom/manifest/manifest.mojom-forward.h" |
Glen Robertson | 271d133 | 2020-01-28 00:16:02 | [diff] [blame] | 13 | |
Alexey Baskakov | 2f8bf816 | 2018-07-17 01:06:11 | [diff] [blame] | 14 | class GURL; |
Eric Willigers | f5089bf | 2020-02-17 21:37:58 | [diff] [blame] | 15 | class Profile; |
Alexey Baskakov | 2f8bf816 | 2018-07-17 01:06:11 | [diff] [blame] | 16 | |
| 17 | namespace web_app { |
| 18 | |
Alan Cutter | 3b063b2 | 2023-06-20 02:29:39 | [diff] [blame] | 19 | class WebApp; |
| 20 | |
Alexander Dunaev | 2193149f | 2022-01-05 18:13:48 | [diff] [blame] | 21 | extern const char kCrxAppPrefix[]; |
| 22 | |
Alexey Baskakov | 2f8bf816 | 2018-07-17 01:06:11 | [diff] [blame] | 23 | // Compute a deterministic name based on the URL. We use this pseudo name |
| 24 | // as a key to store window location per application URLs in Browser and |
| 25 | // as app id for BrowserWindow, shortcut and jump list. |
| 26 | std::string GenerateApplicationNameFromURL(const GURL& url); |
| 27 | |
Alexey Baskakov | f302efe | 2018-07-28 02:02:32 | [diff] [blame] | 28 | // Compute a deterministic name based on an apps's id. |
Alexey Baskakov | c9025c8 | 2018-10-09 04:33:58 | [diff] [blame] | 29 | std::string GenerateApplicationNameFromAppId(const AppId& app_id); |
Alexey Baskakov | f302efe | 2018-07-28 02:02:32 | [diff] [blame] | 30 | |
| 31 | // Extracts the application id from the app name. |
Alexey Baskakov | c9025c8 | 2018-10-09 04:33:58 | [diff] [blame] | 32 | AppId GetAppIdFromApplicationName(const std::string& app_name); |
Alexey Baskakov | f302efe | 2018-07-28 02:02:32 | [diff] [blame] | 33 | |
Daniel Murphy | 137effc | 2023-05-15 18:31:38 | [diff] [blame] | 34 | // Compute the AppId using the given start_url and optional manifest |
| 35 | // id path, which is the path component of the manifest id defined by the spec. |
| 36 | // This mimics what is given to the spec algorithm as the json manifest_id in |
| 37 | // https://www.w3.org/TR/appmanifest/#id-member. The `manifest_id_path` can |
| 38 | // include query arguments and/or fragments, although the fragment will be |
| 39 | // removed. See the `AppId` type for more information. |
Nigel Tao | d75446b | 2018-08-16 23:47:47 | [diff] [blame] | 40 | // |
Daniel Murphy | 137effc | 2023-05-15 18:31:38 | [diff] [blame] | 41 | // This should only be used if a `Manifest` object is not available. |
Nigel Tao | d75446b | 2018-08-16 23:47:47 | [diff] [blame] | 42 | // |
Daniel Murphy | 137effc | 2023-05-15 18:31:38 | [diff] [blame] | 43 | // TODO(b/281881755): Change the optional parameter to required, and refactor |
| 44 | // calls with absl::nullopt to `GenerateManifestIdFromStartUrlOnly`. |
| 45 | AppId GenerateAppId(const absl::optional<std::string>& manifest_id_path, |
Phillis Tang | fca763e8 | 2021-06-23 21:52:12 | [diff] [blame] | 46 | const GURL& start_url); |
Phillis Tang | 8ced9bd4 | 2021-04-09 22:25:01 | [diff] [blame] | 47 | |
Daniel Murphy | 137effc | 2023-05-15 18:31:38 | [diff] [blame] | 48 | // Returns a resolved manifest id given the relative `manifest_id_path`, |
| 49 | // as per the spec algorithm at https://www.w3.org/TR/appmanifest/#id-member. |
| 50 | // The `manifest_id_path` can include query arguments and/or fragments, although |
| 51 | // the fragment will be removed. If there is no `manifest_id_path`, then |
| 52 | // GenerateManifestIdFromStartUrlOnly can be used. |
| 53 | // |
| 54 | // This should only be used if a `Manifest` object is not available. |
| 55 | ManifestId GenerateManifestId(const std::string& manifest_id_path, |
| 56 | const GURL& start_url); |
Phillis Tang | 43184cce | 2021-08-17 23:45:02 | [diff] [blame] | 57 | |
Daniel Murphy | 137effc | 2023-05-15 18:31:38 | [diff] [blame] | 58 | // Generates the chrome-specific `AppId` from the spec-defined manifest id. See |
| 59 | // the `AppId` type for more information. |
| 60 | AppId GenerateAppIdFromManifestId(const ManifestId& manifest_id); |
| 61 | |
| 62 | // Generates the chrome-specific `AppId` from the spec-defined manifest. See the |
| 63 | // `AppId` type for more information. This will CHECK-fail if the `id` field is |
| 64 | // not present on the manifest. |
Alan Cutter | e511d18a | 2021-08-04 23:10:38 | [diff] [blame] | 65 | AppId GenerateAppIdFromManifest(const blink::mojom::Manifest& manifest); |
Nigel Tao | d75446b | 2018-08-16 23:47:47 | [diff] [blame] | 66 | |
Daniel Murphy | 137effc | 2023-05-15 18:31:38 | [diff] [blame] | 67 | // Generates a manifest id by only the start_url, which matches the spec |
| 68 | // algorithm in https://www.w3.org/TR/appmanifest/#id-member where the `id` json |
| 69 | // member is not present or an empty string. To include an identifier path, |
| 70 | // please use `GenerateManifestId`. |
| 71 | // |
| 72 | // This should only be used if a `Manifest` object is not available. |
| 73 | ManifestId GenerateManifestIdFromStartUrlOnly(const GURL& start_url); |
Phillis Tang | 3dd48fc | 2021-09-21 23:53:12 | [diff] [blame] | 74 | |
Alexey Baskakov | 856744c | 2019-05-23 00:36:24 | [diff] [blame] | 75 | // Returns whether the given |app_url| is a valid web app url. |
Alexey Baskakov | 20d192c8 | 2018-08-20 07:00:41 | [diff] [blame] | 76 | bool IsValidWebAppUrl(const GURL& app_url); |
| 77 | |
Eric Willigers | f5089bf | 2020-02-17 21:37:58 | [diff] [blame] | 78 | // Searches for the first locally installed app id in the registry for which |
| 79 | // the |url| is in scope. If |window_only| is specified, only apps that |
| 80 | // open in app windows will be considered. |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame] | 81 | absl::optional<AppId> FindInstalledAppWithUrlInScope(Profile* profile, |
Eric Willigers | f5089bf | 2020-02-17 21:37:58 | [diff] [blame] | 82 | const GURL& url, |
| 83 | bool window_only = false); |
| 84 | |
Dibyajyoti Pal | 64fda1a | 2023-05-31 18:23:48 | [diff] [blame] | 85 | // Searches for the first app id in the registry which is not locally installed |
| 86 | // and for which the |url| is in scope. |
| 87 | bool IsNonLocallyInstalledAppWithUrlInScope(Profile* profile, const GURL& url); |
| 88 | |
Alan Cutter | 3b063b2 | 2023-06-20 02:29:39 | [diff] [blame] | 89 | // Tests if `app` is marked as a placeholder app or appears to be one despite |
| 90 | // not being marked due to corruption, see: https://crbug.com/1427340 |
| 91 | bool LooksLikePlaceholder(const WebApp& app); |
| 92 | |
Alexey Baskakov | 2f8bf816 | 2018-07-17 01:06:11 | [diff] [blame] | 93 | } // namespace web_app |
| 94 | |
Song Fangzhen | 49468b2 | 2021-09-02 15:41:59 | [diff] [blame] | 95 | #endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_HELPERS_H_ |