Jay Harris | 4e71072 | 2019-11-26 05:40:48 | [diff] [blame] | 1 | // Copyright 2019 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 | |
Song Fangzhen | cda4af6 | 2021-09-09 05:24:02 | [diff] [blame] | 5 | #include "chrome/browser/web_applications/web_app_prefs_utils.h" |
Jay Harris | 4e71072 | 2019-11-26 05:40:48 | [diff] [blame] | 6 | |
Phillis Tang | 1565f1f | 2020-11-12 03:07:51 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
Albert J. Wong | f60c8d2 | 2021-07-27 07:25:26 | [diff] [blame] | 9 | #include "base/json/values_util.h" |
Phillis Tang | 4379e29 | 2020-11-18 00:12:33 | [diff] [blame] | 10 | #include "base/numerics/safe_conversions.h" |
David Sanders | c89b9b4 | 2022-02-19 16:34:41 | [diff] [blame] | 11 | #include "base/strings/string_piece.h" |
Phillis Tang | 1565f1f | 2020-11-12 03:07:51 | [diff] [blame] | 12 | #include "base/time/time.h" |
Jay Harris | 4e71072 | 2019-11-26 05:40:48 | [diff] [blame] | 13 | #include "base/values.h" |
Song Fangzhen | cda4af6 | 2021-09-09 05:24:02 | [diff] [blame] | 14 | #include "chrome/browser/web_applications/web_app_constants.h" |
Jay Harris | 4e71072 | 2019-11-26 05:40:48 | [diff] [blame] | 15 | #include "chrome/common/pref_names.h" |
| 16 | #include "components/pref_registry/pref_registry_syncable.h" |
| 17 | #include "components/prefs/pref_service.h" |
Nancy Wang | 071f6e2 | 2021-09-27 06:45:34 | [diff] [blame] | 18 | #include "components/webapps/browser/installable/installable_metrics.h" |
Jay Harris | 4e71072 | 2019-11-26 05:40:48 | [diff] [blame] | 19 | #include "content/public/browser/browser_thread.h" |
| 20 | #include "services/preferences/public/cpp/dictionary_value_update.h" |
Phillis Tang | 4379e29 | 2020-11-18 00:12:33 | [diff] [blame] | 21 | #include "services/preferences/public/cpp/scoped_pref_update.h" |
Jay Harris | 4e71072 | 2019-11-26 05:40:48 | [diff] [blame] | 22 | |
| 23 | namespace web_app { |
| 24 | |
| 25 | namespace { |
| 26 | |
Evan Stade | da2a317 | 2022-02-04 02:01:35 | [diff] [blame] | 27 | const char kLatestWebAppInstallSource[] = "latest_web_app_install_source"; |
| 28 | |
Roland Bock | 6d4b6c6 | 2022-08-02 18:50:00 | [diff] [blame] | 29 | const base::Value::Dict* GetWebAppDictionary(const PrefService* pref_service, |
| 30 | const AppId& app_id) { |
Jay Harris | 4e71072 | 2019-11-26 05:40:48 | [diff] [blame] | 31 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
Roland Bock | 6d4b6c6 | 2022-08-02 18:50:00 | [diff] [blame] | 32 | const base::Value::Dict& web_apps_prefs = |
| 33 | pref_service->GetValueDict(prefs::kWebAppsPreferences); |
Jay Harris | 4e71072 | 2019-11-26 05:40:48 | [diff] [blame] | 34 | |
Roland Bock | 6d4b6c6 | 2022-08-02 18:50:00 | [diff] [blame] | 35 | return web_apps_prefs.FindDict(app_id); |
Jay Harris | 4e71072 | 2019-11-26 05:40:48 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | std::unique_ptr<prefs::DictionaryValueUpdate> UpdateWebAppDictionary( |
| 39 | std::unique_ptr<prefs::DictionaryValueUpdate> web_apps_prefs_update, |
| 40 | const AppId& app_id) { |
| 41 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 42 | std::unique_ptr<prefs::DictionaryValueUpdate> web_app_prefs_update; |
| 43 | if (!web_apps_prefs_update->GetDictionaryWithoutPathExpansion( |
| 44 | app_id, &web_app_prefs_update)) { |
| 45 | web_app_prefs_update = |
| 46 | web_apps_prefs_update->SetDictionaryWithoutPathExpansion( |
| 47 | app_id, std::make_unique<base::DictionaryValue>()); |
| 48 | } |
| 49 | return web_app_prefs_update; |
| 50 | } |
| 51 | |
Phillis Tang | 4379e29 | 2020-11-18 00:12:33 | [diff] [blame] | 52 | // Returns whether the time occurred within X days. |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame] | 53 | bool TimeOccurredWithinDays(absl::optional<base::Time> time, int days) { |
Phillis Tang | 4379e29 | 2020-11-18 00:12:33 | [diff] [blame] | 54 | return time && (base::Time::Now() - time.value()).InDays() < days; |
| 55 | } |
| 56 | |
Evan Stade | da2a317 | 2022-02-04 02:01:35 | [diff] [blame] | 57 | // Removes all the empty app ID dictionaries from the `web_app_ids` dictionary. |
| 58 | // That is, this dictionary: |
| 59 | // |
| 60 | // "web_app_ids": { |
| 61 | // "<app_id_1>": {}, |
| 62 | // "<app_id_2>": { "foo": true } |
| 63 | // } |
| 64 | // |
| 65 | // will become this dictionary: |
| 66 | // |
| 67 | // "web_app_ids": { |
| 68 | // "<app_id_2>": { "foo": true } |
| 69 | // } |
| 70 | void RemoveEmptyWebAppPrefs(PrefService* pref_service) { |
| 71 | prefs::ScopedDictionaryPrefUpdate update(pref_service, |
| 72 | prefs::kWebAppsPreferences); |
| 73 | |
| 74 | std::vector<AppId> apps_to_remove; |
Anders Hartvoll Ruud | 1951f1f1 | 2022-07-28 10:34:02 | [diff] [blame] | 75 | for (const auto [app_id, dict] : *update.Get()->AsDict()) { |
Evan Stade | da2a317 | 2022-02-04 02:01:35 | [diff] [blame] | 76 | if (dict.is_dict() && dict.DictEmpty()) |
| 77 | apps_to_remove.push_back(app_id); |
| 78 | } |
| 79 | |
| 80 | for (const AppId& app_id : apps_to_remove) |
| 81 | update.Get()->Remove(app_id); |
| 82 | } |
| 83 | |
Jay Harris | 4e71072 | 2019-11-26 05:40:48 | [diff] [blame] | 84 | } // namespace |
| 85 | |
| 86 | // The stored preferences look like: |
Jay Harris | 4e71072 | 2019-11-26 05:40:48 | [diff] [blame] | 87 | // "web_app_ids": { |
| 88 | // "<app_id_1>": { |
| 89 | // "was_external_app_uninstalled_by_user": true, |
Jay Harris | 46b47a6c | 2020-01-28 04:17:26 | [diff] [blame] | 90 | // A double representing the number of seconds since epoch, in local time. |
| 91 | // Convert from/to using base::Time::FromDoubleT() and |
| 92 | // base::Time::ToDoubleT(). |
Phillis Tang | 4379e29 | 2020-11-18 00:12:33 | [diff] [blame] | 93 | // "IPH_num_of_consecutive_ignore": 2, |
| 94 | // A string-flavored base::value representing the int64_t number of |
Albert J. Wong | f60c8d2 | 2021-07-27 07:25:26 | [diff] [blame] | 95 | // microseconds since the Windows epoch, using base::TimeToValue(). |
Phillis Tang | 4379e29 | 2020-11-18 00:12:33 | [diff] [blame] | 96 | // "IPH_last_ignore_time": "13249617864945580", |
Jay Harris | 4e71072 | 2019-11-26 05:40:48 | [diff] [blame] | 97 | // }, |
Phillis Tang | 4379e29 | 2020-11-18 00:12:33 | [diff] [blame] | 98 | // }, |
| 99 | // "app_agnostic_iph_state": { |
| 100 | // "IPH_num_of_consecutive_ignore": 3, |
| 101 | // A string-flavored base::Value representing int64_t number of microseconds |
Albert J. Wong | f60c8d2 | 2021-07-27 07:25:26 | [diff] [blame] | 102 | // since the Windows epoch, using base::TimeToValue(). |
Phillis Tang | 4379e29 | 2020-11-18 00:12:33 | [diff] [blame] | 103 | // "IPH_last_ignore_time": "13249617864945500", |
| 104 | // }, |
Robbie McElrath | ebc64b7 | 2021-06-04 01:39:48 | [diff] [blame] | 105 | // isolation_state is managed by isolation_prefs_utils |
| 106 | // "isolation_state": { |
| 107 | // "<origin>": { |
| 108 | // "storage_isolation_key": "abc123", |
| 109 | // }, |
Evan Stade | da2a317 | 2022-02-04 02:01:35 | [diff] [blame] | 110 | // } |
Jay Harris | 4e71072 | 2019-11-26 05:40:48 | [diff] [blame] | 111 | // |
Jay Harris | 4e71072 | 2019-11-26 05:40:48 | [diff] [blame] | 112 | |
Phillis Tang | 1565f1f | 2020-11-12 03:07:51 | [diff] [blame] | 113 | const char kIphIgnoreCount[] = "IPH_num_of_consecutive_ignore"; |
| 114 | |
| 115 | const char kIphLastIgnoreTime[] = "IPH_last_ignore_time"; |
| 116 | |
Jay Harris | 4e71072 | 2019-11-26 05:40:48 | [diff] [blame] | 117 | void WebAppPrefsUtilsRegisterProfilePrefs( |
| 118 | user_prefs::PrefRegistrySyncable* registry) { |
| 119 | registry->RegisterDictionaryPref(::prefs::kWebAppsPreferences); |
Phillis Tang | 4379e29 | 2020-11-18 00:12:33 | [diff] [blame] | 120 | registry->RegisterDictionaryPref(::prefs::kWebAppsAppAgnosticIphState); |
Jay Harris | 4e71072 | 2019-11-26 05:40:48 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | bool GetBoolWebAppPref(const PrefService* pref_service, |
| 124 | const AppId& app_id, |
| 125 | base::StringPiece path) { |
Roland Bock | 6d4b6c6 | 2022-08-02 18:50:00 | [diff] [blame] | 126 | if (const base::Value::Dict* web_app_prefs = |
Carlos Caballero | f5e79fb | 2021-11-24 04:08:32 | [diff] [blame] | 127 | GetWebAppDictionary(pref_service, app_id)) { |
Roland Bock | 6d4b6c6 | 2022-08-02 18:50:00 | [diff] [blame] | 128 | return web_app_prefs->FindBoolByDottedPath(path).value_or(false); |
Carlos Caballero | f5e79fb | 2021-11-24 04:08:32 | [diff] [blame] | 129 | } |
| 130 | return false; |
Jay Harris | 4e71072 | 2019-11-26 05:40:48 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void UpdateBoolWebAppPref(PrefService* pref_service, |
| 134 | const AppId& app_id, |
| 135 | base::StringPiece path, |
| 136 | bool value) { |
| 137 | prefs::ScopedDictionaryPrefUpdate update(pref_service, |
| 138 | prefs::kWebAppsPreferences); |
| 139 | |
| 140 | std::unique_ptr<prefs::DictionaryValueUpdate> web_app_prefs = |
| 141 | UpdateWebAppDictionary(update.Get(), app_id); |
| 142 | web_app_prefs->SetBoolean(path, value); |
| 143 | } |
| 144 | |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame] | 145 | absl::optional<int> GetIntWebAppPref(const PrefService* pref_service, |
Glen Robertson | 24f339c | 2020-04-08 04:50:40 | [
|