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