Alexey Baskakov | fd3894e | 2018-10-16 06:09:58 | [diff] [blame] | 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 | |
Alexey Baskakov | 256a3c43 | 2019-08-15 02:56:24 | [diff] [blame] | 5 | #include "chrome/browser/web_applications/web_app.h" |
| 6 | |
Alexey Baskakov | 4768f623 | 2018-11-08 09:26:46 | [diff] [blame] | 7 | #include <ios> |
Alexey Baskakov | 393b2aa | 2018-10-25 06:42:31 | [diff] [blame] | 8 | #include <ostream> |
Alexey Baskakov | 97881e5 | 2019-09-06 05:51:03 | [diff] [blame] | 9 | #include <tuple> |
Alexey Baskakov | 393b2aa | 2018-10-25 06:42:31 | [diff] [blame] | 10 | |
Alexey Baskakov | 8975f35 | 2018-12-05 02:28:26 | [diff] [blame] | 11 | #include "base/logging.h" |
Eric Willigers | 234703c | 2019-10-04 08:08:26 | [diff] [blame] | 12 | #include "third_party/blink/public/common/manifest/manifest_util.h" |
Alexey Baskakov | 4768f623 | 2018-11-08 09:26:46 | [diff] [blame] | 13 | #include "ui/gfx/color_utils.h" |
Alexey Baskakov | fd3894e | 2018-10-16 06:09:58 | [diff] [blame] | 14 | |
| 15 | namespace web_app { |
| 16 | |
Alexey Baskakov | e3804cf | 2019-08-14 00:16:02 | [diff] [blame] | 17 | WebApp::WebApp(const AppId& app_id) |
Eric Willigers | 234703c | 2019-10-04 08:08:26 | [diff] [blame] | 18 | : app_id_(app_id), display_mode_(blink::mojom::DisplayMode::kUndefined) {} |
Alexey Baskakov | fd3894e | 2018-10-16 06:09:58 | [diff] [blame] | 19 | |
| 20 | WebApp::~WebApp() = default; |
| 21 | |
Alexey Baskakov | db72457e | 2019-10-11 06:55:13 | [diff] [blame] | 22 | WebApp::WebApp(const WebApp& web_app) = default; |
| 23 | |
| 24 | WebApp& WebApp::operator=(WebApp&& web_app) = default; |
| 25 | |
Alexey Baskakov | 97881e5 | 2019-09-06 05:51:03 | [diff] [blame] | 26 | void WebApp::AddSource(Source::Type source) { |
| 27 | sources_[source] = true; |
| 28 | } |
| 29 | |
| 30 | void WebApp::RemoveSource(Source::Type source) { |
| 31 | sources_[source] = false; |
| 32 | } |
| 33 | |
| 34 | bool WebApp::HasAnySources() const { |
| 35 | return sources_.any(); |
| 36 | } |
| 37 | |
Alexey Baskakov | 0b50ec6 | 2019-10-01 03:29:23 | [diff] [blame] | 38 | bool WebApp::IsSynced() const { |
| 39 | return sources_[Source::kSync]; |
| 40 | } |
| 41 | |
Alexey Baskakov | fd3894e | 2018-10-16 06:09:58 | [diff] [blame] | 42 | void WebApp::SetName(const std::string& name) { |
| 43 | name_ = name; |
| 44 | } |
| 45 | |
| 46 | void WebApp::SetDescription(const std::string& description) { |
| 47 | description_ = description; |
| 48 | } |
| 49 | |
Alexey Baskakov | 79b0690 | 2018-11-08 05:30:03 | [diff] [blame] | 50 | void WebApp::SetLaunchUrl(const GURL& launch_url) { |
| 51 | DCHECK(!launch_url.is_empty() && launch_url.is_valid()); |
Alexey Baskakov | fd3894e | 2018-10-16 06:09:58 | [diff] [blame] | 52 | launch_url_ = launch_url; |
| 53 | } |
| 54 | |
Alexey Baskakov | 4768f623 | 2018-11-08 09:26:46 | [diff] [blame] | 55 | void WebApp::SetScope(const GURL& scope) { |
| 56 | DCHECK(scope.is_empty() || scope.is_valid()); |
| 57 | scope_ = scope; |
| 58 | } |
| 59 | |
| 60 | void WebApp::SetThemeColor(base::Optional<SkColor> theme_color) { |
| 61 | theme_color_ = theme_color; |
| 62 | } |
| 63 | |
Eric Willigers | 234703c | 2019-10-04 08:08:26 | [diff] [blame] | 64 | void WebApp::SetDisplayMode(blink::mojom::DisplayMode display_mode) { |
| 65 | DCHECK_NE(blink::mojom::DisplayMode::kUndefined, display_mode); |
| 66 | display_mode_ = display_mode; |
Alexey Baskakov | e3804cf | 2019-08-14 00:16:02 | [diff] [blame] | 67 | } |
| 68 | |
Alexey Baskakov | 256a3c43 | 2019-08-15 02:56:24 | [diff] [blame] | 69 | void WebApp::SetIsLocallyInstalled(bool is_locally_installed) { |
| 70 | is_locally_installed_ = is_locally_installed; |
| 71 | } |
| 72 | |
Alexey Baskakov | d702ffc | 2019-10-16 05:58:10 | [diff] [blame] | 73 | void WebApp::SetIsInSyncInstall(bool is_in_sync_install) { |
| 74 | is_in_sync_install_ = is_in_sync_install; |
Alexey Baskakov | 8c864f18 | 2019-10-04 03:39:56 | [diff] [blame] | 75 | } |
| 76 | |
Alexey Baskakov | 8975f35 | 2018-12-05 02:28:26 | [diff] [blame] | 77 | void WebApp::SetIcons(Icons icons) { |
Alexey Baskakov | 8975f35 | 2018-12-05 02:28:26 | [diff] [blame] | 78 | icons_ = std::move(icons); |
| 79 | } |
| 80 | |
Alexey Baskakov | db72457e | 2019-10-11 06:55:13 | [diff] [blame] | 81 | void WebApp::SetSyncData(SyncData sync_data) { |
| 82 | sync_data_ = std::move(sync_data); |
Alexey Baskakov | 8c864f18 | 2019-10-04 03:39:56 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | WebApp::SyncData::SyncData() = default; |
| 86 | |
| 87 | WebApp::SyncData::~SyncData() = default; |
| 88 | |
Alexey Baskakov | db72457e | 2019-10-11 06:55:13 | [diff] [blame] | 89 | WebApp::SyncData::SyncData(const SyncData& sync_data) = default; |
| 90 | |
| 91 | WebApp::SyncData& WebApp::SyncData::operator=(SyncData&& sync_data) = default; |
| 92 | |
Alexey Baskakov | 393b2aa | 2018-10-25 06:42:31 | [diff] [blame] | 93 | std::ostream& operator<<(std::ostream& out, const WebApp& app) { |
Alexey Baskakov | 4768f623 | 2018-11-08 09:26:46 | [diff] [blame] | 94 | const std::string theme_color = |
Alexey Baskakov | 8c864f18 | 2019-10-04 03:39:56 | [diff] [blame] | 95 | app.theme_color_.has_value() |
| 96 | ? color_utils::SkColorToRgbaString(app.theme_color_.value()) |
| 97 | : "none"; |
| 98 | const std::string sync_theme_color = |
| 99 | app.sync_data_.theme_color.has_value() |
| 100 | ? color_utils::SkColorToRgbaString(app.sync_data_.theme_color.value()) |
Alexey Baskakov | 4768f623 | 2018-11-08 09:26:46 | [diff] [blame] | 101 | : "none"; |
Eric Willigers | 234703c | 2019-10-04 08:08:26 | [diff] [blame] | 102 | const std::string display_mode = |
| 103 | blink::DisplayModeToString(app.display_mode_); |
Alexey Baskakov | 8c864f18 | 2019-10-04 03:39:56 | [diff] [blame] | 104 | const bool is_locally_installed = app.is_locally_installed_; |
Alexey Baskakov | d702ffc | 2019-10-16 05:58:10 | [diff] [blame] | 105 | const bool is_in_sync_install = app.is_in_sync_install_; |
Alexey Baskakov | 4768f623 | 2018-11-08 09:26:46 | [diff] [blame] | 106 | |
Alexey Baskakov | 97881e5 | 2019-09-06 05:51:03 | [diff] [blame] | 107 | return out << "app_id: " << app.app_id_ << std::endl |
| 108 | << " name: " << app.name_ << std::endl |
| 109 | << " launch_url: " << app.launch_url_ << std::endl |
| 110 | << " scope: " << app.scope_ << std::endl |
Alexey Baskakov | 4768f623 | 2018-11-08 09:26:46 | [diff] [blame] | 111 | << " theme_color: " << theme_color << std::endl |
Eric Willigers | 234703c | 2019-10-04 08:08:26 | [diff] [blame] | 112 | << " display_mode: " << display_mode << std::endl |
Alexey Baskakov | 8c864f18 | 2019-10-04 03:39:56 | [diff] [blame] | 113 | << " sources: " << app.sources_.to_string() << std::endl |
Alexey Baskakov | 256a3c43 | 2019-08-15 02:56:24 | [diff] [blame] | 114 | << " is_locally_installed: " << is_locally_installed << std::endl |
Alexey Baskakov | d702ffc | 2019-10-16 05:58:10 | [diff] [blame] | 115 | << " is_in_sync_install: " << is_in_sync_install << std::endl |
Alexey Baskakov | 8c864f18 | 2019-10-04 03:39:56 | [diff] [blame] | 116 | << " sync_data.name: " << app.sync_data_.name << std::endl |
| 117 | << " sync_data.theme_color: " << sync_theme_color << std::endl |
Alexey Baskakov | 97881e5 | 2019-09-06 05:51:03 | [diff] [blame] | 118 | << " description: " << app.description_; |
| 119 | } |
| 120 | |
| 121 | bool operator==(const WebApp::IconInfo& icon_info1, |
| 122 | const WebApp::IconInfo& icon_info2) { |
| 123 | return std::tie(icon_info1.url, icon_info1.size_in_px) == |
| 124 | std::tie(icon_info2.url, icon_info2.size_in_px); |
| 125 | } |
| 126 | |
Alexey Baskakov | 8c864f18 | 2019-10-04 03:39:56 | [diff] [blame] | 127 | bool operator==(const WebApp::SyncData& sync_data1, |
| 128 | const WebApp::SyncData& sync_data2) { |
| 129 | return std::tie(sync_data1.name, sync_data1.theme_color) == |
| 130 | std::tie(sync_data2.name, sync_data2.theme_color); |
| 131 | } |
| 132 | |
Alexey Baskakov | 97881e5 | 2019-09-06 05:51:03 | [diff] [blame] | 133 | bool operator==(const WebApp& app1, const WebApp& app2) { |
| 134 | return std::tie(app1.app_id_, app1.sources_, app1.name_, app1.launch_url_, |
| 135 | app1.description_, app1.scope_, app1.theme_color_, |
Eric Willigers | 234703c | 2019-10-04 08:08:26 | [diff] [blame] | 136 | app1.icons_, app1.display_mode_, app1.is_locally_installed_, |
Alexey Baskakov | d702ffc | 2019-10-16 05:58:10 | [diff] [blame] | 137 | app1.is_in_sync_install_, app1.sync_data_) == |
Alexey Baskakov | 97881e5 | 2019-09-06 05:51:03 | [diff] [blame] | 138 | std::tie(app2.app_id_, app2.sources_, app2.name_, app2.launch_url_, |
| 139 | app2.description_, app2.scope_, app2.theme_color_, |
Eric Willigers | 234703c | 2019-10-04 08:08:26 | [diff] [blame] | 140 | app2.icons_, app2.display_mode_, app2.is_locally_installed_, |
Alexey Baskakov | d702ffc | 2019-10-16 05:58:10 | [diff] [blame] | 141 | app2.is_in_sync_install_, app2.sync_data_); |
Alexey Baskakov | 97881e5 | 2019-09-06 05:51:03 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | bool operator!=(const WebApp& app1, const WebApp& app2) { |
| 145 | return !(app1 == app2); |
Alexey Baskakov | 393b2aa | 2018-10-25 06:42:31 | [diff] [blame] | 146 | } |
| 147 | |
Alexey Baskakov | fd3894e | 2018-10-16 06:09:58 | [diff] [blame] | 148 | } // namespace web_app |