[go: nahoru, domu]

cros: Minor cleanup to AppRestoreData

Bug: 318409847
Test: existing tests
Change-Id: I38e00afee5dc2ca431d25e8245ed20d083581e23
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5252278
Commit-Queue: Sammie Quon <sammiequon@chromium.org>
Reviewed-by: Daniel Andersson <dandersson@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1255716}
diff --git a/components/app_restore/app_restore_data.cc b/components/app_restore/app_restore_data.cc
index 0ec2e113..e3a4f58 100644
--- a/components/app_restore/app_restore_data.cc
+++ b/components/app_restore/app_restore_data.cc
@@ -152,12 +152,10 @@
   }
 
   for (const auto& item : *urls_path_value) {
-    if (item.GetString().empty())
-      continue;
     GURL url(item.GetString());
-    if (!url.is_valid())
-      continue;
-    url_paths.push_back(url);
+    if (url.is_valid()) {
+      url_paths.push_back(url);
+    }
   }
 
   return url_paths;
@@ -191,11 +189,7 @@
     return std::nullopt;
   }
 
-  std::vector<int> size;
-  for (const auto& item : *size_value)
-    size.push_back(item.GetInt());
-
-  return gfx::Size(size[0], size[1]);
+  return gfx::Size((*size_value)[0].GetInt(), (*size_value)[1].GetInt());
 }
 
 // Gets gfx::Rect from base::Value, e.g. { 0, 100, 200, 300 } returns
@@ -203,17 +197,12 @@
 std::optional<gfx::Rect> GetBoundsRectFromDict(const base::Value::Dict& dict,
                                                base::StringPiece key_name) {
   const base::Value::List* rect_value = dict.FindList(key_name);
-  if (!rect_value || rect_value->empty())
+  if (!rect_value || rect_value->size() != 4) {
     return std::nullopt;
+  }
 
-  std::vector<int> rect;
-  for (const auto& item : *rect_value)
-    rect.push_back(item.GetInt());
-
-  if (rect.size() != 4)
-    return std::nullopt;
-
-  return gfx::Rect(rect[0], rect[1], rect[2], rect[3]);
+  return gfx::Rect((*rect_value)[0].GetInt(), (*rect_value)[1].GetInt(),
+                   (*rect_value)[2].GetInt(), (*rect_value)[3].GetInt());
 }
 
 // Gets WindowStateType from base::Value::Dict, e.g. { "window_state_type":
@@ -242,6 +231,15 @@
   return base::Uuid();
 }
 
+template <typename T>
+void SetValueIntoDict(std::optional<T> value,
+                      std::string_view key,
+                      base::Value::Dict& dict) {
+  if (value) {
+    dict.Set(key, *value);
+  }
+}
+
 }  // namespace
 
 AppRestoreData::AppRestoreData() = default;
@@ -304,106 +302,35 @@
 AppRestoreData::~AppRestoreData() = default;
 
 std::unique_ptr<AppRestoreData> AppRestoreData::Clone() const {
-  // TODO(http:/b/318409847): Removechecking for value/empty when cloning.
-  std::unique_ptr<AppRestoreData> data = std::make_unique<AppRestoreData>();
+  auto data = std::make_unique<AppRestoreData>();
 
-  if (event_flag.has_value())
-    data->event_flag = event_flag.value();
-
-  if (container.has_value())
-    data->container = container.value();
-
-  if (disposition.has_value())
-    data->disposition = disposition.value();
-
-  if (override_url.has_value())
-    data->override_url = override_url.value();
-
-  if (display_id.has_value())
-    data->display_id = display_id.value();
-
-  if (handler_id.has_value())
-    data->handler_id = handler_id.value();
-
-  if (!file_paths.empty()) {
-    data->file_paths = file_paths;
-  }
-
+  data->event_flag = event_flag;
+  data->container = container;
+  data->disposition = disposition;
+  data->override_url = override_url;
+  data->display_id = display_id;
+  data->handler_id = handler_id;
+  data->file_paths = file_paths;
   if (intent) {
     data->intent = intent->Clone();
   }
 
-  if (!browser_extra_info.urls.empty()) {
-    data->browser_extra_info.urls = browser_extra_info.urls;
-  }
+  data->browser_extra_info = browser_extra_info;
 
-  if (browser_extra_info.active_tab_index.has_value()) {
-    data->browser_extra_info.active_tab_index =
-        browser_extra_info.active_tab_index.value();
-  }
+  data->activation_index = activation_index;
+  data->desk_id = desk_id;
+  data->desk_guid = desk_guid;
+  data->current_bounds = current_bounds;
+  data->window_state_type = window_state_type;
+  data->pre_minimized_show_state_type = pre_minimized_show_state_type;
+  data->snap_percentage = snap_percentage;
+  data->title = title;
 
-  if (browser_extra_info.first_non_pinned_tab_index.has_value()) {
-    data->browser_extra_info.first_non_pinned_tab_index =
-        browser_extra_info.first_non_pinned_tab_index.value();
-  }
-
-  if (browser_extra_info.app_type_browser.has_value()) {
-    data->browser_extra_info.app_type_browser =
-        browser_extra_info.app_type_browser.value();
-  }
-
-  if (browser_extra_info.app_name.has_value()) {
-    data->browser_extra_info.app_name = browser_extra_info.app_name.value();
-  }
-
-  if (!browser_extra_info.tab_group_infos.empty()) {
-    data->browser_extra_info.tab_group_infos =
-        browser_extra_info.tab_group_infos;
-  }
-
-  data->browser_extra_info.lacros_profile_id =
-      browser_extra_info.lacros_profile_id;
-
-  if (activation_index.has_value())
-    data->activation_index = activation_index.value();
-
-  if (desk_id.has_value())
-    data->desk_id = desk_id.value();
-
-  if (desk_guid.is_valid()) {
-    data->desk_guid = desk_guid;
-  }
-
-  if (current_bounds.has_value())
-    data->current_bounds = current_bounds.value();
-
-  if (window_state_type.has_value())
-    data->window_state_type = window_state_type.value();
-
-  if (pre_minimized_show_state_type.has_value())
-    data->pre_minimized_show_state_type = pre_minimized_show_state_type.value();
-
-  if (snap_percentage.has_value())
-    data->snap_percentage = snap_percentage.value();
-
-  if (title.has_value()) {
-    data->title = title.value();
-  }
-
-  if (maximum_size.has_value())
-    data->maximum_size = maximum_size.value();
-
-  if (minimum_size.has_value())
-    data->minimum_size = minimum_size.value();
-
-  if (bounds_in_root.has_value())
-    data->bounds_in_root = bounds_in_root.value();
-
-  if (primary_color.has_value())
-    data->primary_color = primary_color.value();
-
-  if (status_bar_color.has_value())
-    data->status_bar_color = status_bar_color.value();
+  data->maximum_size = maximum_size;
+  data->minimum_size = minimum_size;
+  data->bounds_in_root = bounds_in_root;
+  data->primary_color = primary_color;
+  data->status_bar_color = status_bar_color;
 
   return data;
 }
@@ -411,25 +338,20 @@
 base::Value AppRestoreData::ConvertToValue() const {
   base::Value::Dict launch_info_dict;
 
-  if (event_flag.has_value())
-    launch_info_dict.Set(kEventFlagKey, event_flag.value());
+  SetValueIntoDict(event_flag, kEventFlagKey, launch_info_dict);
+  SetValueIntoDict(container, kContainerKey, launch_info_dict);
+  SetValueIntoDict(disposition, kDispositionKey, launch_info_dict);
 
-  if (container.has_value())
-    launch_info_dict.Set(kContainerKey, container.value());
-
-  if (disposition.has_value())
-    launch_info_dict.Set(kDispositionKey, disposition.value());
-
-  if (override_url.has_value())
+  if (override_url.has_value()) {
     launch_info_dict.Set(kOverrideUrlKey, override_url.value().spec());
+  }
 
   if (display_id.has_value()) {
     launch_info_dict.Set(kDisplayIdKey,
                          base::NumberToString(display_id.value()));
   }
 
-  if (handler_id.has_value())
-    launch_info_dict.Set(kHandlerIdKey, handler_id.value());
+  SetValueIntoDict(handler_id, kHandlerIdKey, launch_info_dict);
 
   if (!file_paths.empty()) {
     base::Value::List file_paths_list;
@@ -451,24 +373,13 @@
     launch_info_dict.Set(kUrlsKey, std::move(urls_list));
   }
 
-  if (browser_extra_info.active_tab_index.has_value()) {
-    launch_info_dict.Set(kActiveTabIndexKey,
-                         browser_extra_info.active_tab_index.value());
-  }
-
-  if (browser_extra_info.first_non_pinned_tab_index.has_value()) {
-    launch_info_dict.Set(kFirstNonPinnedTabIndexKey,
-                         browser_extra_info.first_non_pinned_tab_index.value());
-  }
-
-  if (browser_extra_info.app_type_browser.has_value()) {
-    launch_info_dict.Set(kAppTypeBrowserKey,
-                         browser_extra_info.app_type_browser.value());
-  }
-
-  if (browser_extra_info.app_name.has_value()) {
-    launch_info_dict.Set(kAppNameKey, browser_extra_info.app_name.value());
-  }
+  SetValueIntoDict(browser_extra_info.active_tab_index, kActiveTabIndexKey,
+                   launch_info_dict);
+  SetValueIntoDict(browser_extra_info.first_non_pinned_tab_index,
+                   kFirstNonPinnedTabIndexKey, launch_info_dict);
+  SetValueIntoDict(browser_extra_info.app_name, kAppNameKey, launch_info_dict);
+  SetValueIntoDict(browser_extra_info.app_type_browser, kAppTypeBrowserKey,
+                   launch_info_dict);
 
   if (browser_extra_info.lacros_profile_id.has_value()) {
     launch_info_dict.Set(
@@ -476,14 +387,8 @@
         ConvertUint64ToValue(browser_extra_info.lacros_profile_id.value()));
   }
 
-  if (title.has_value())
-    launch_info_dict.Set(kTitleKey, base::UTF16ToUTF8(title.value()));
-
-  if (activation_index.has_value())
-    launch_info_dict.Set(kActivationIndexKey, activation_index.value());
-
-  if (desk_id.has_value())
-    launch_info_dict.Set(kDeskIdKey, desk_id.value());
+  SetValueIntoDict(activation_index, kActivationIndexKey, launch_info_dict);
+  SetValueIntoDict(desk_id, kDeskIdKey, launch_info_dict);
 
   if (desk_guid.is_valid()) {
     launch_info_dict.Set(kDeskUuidKey, desk_guid.AsLowercaseString());
@@ -510,6 +415,8 @@
                          ConvertUintToValue(snap_percentage.value()));
   }
 
+  SetValueIntoDict(title, kTitleKey, launch_info_dict);
+
   if (maximum_size.has_value()) {
     launch_info_dict.Set(kMaximumSizeKey,
                          ConvertSizeToList(maximum_size.value()));
@@ -619,33 +526,14 @@
 
 std::unique_ptr<WindowInfo> AppRestoreData::GetWindowInfo() const {
   auto window_info = std::make_unique<WindowInfo>();
-
-  if (activation_index.has_value())
-    window_info->activation_index = activation_index;
-
-  if (desk_id.has_value())
-    window_info->desk_id = desk_id.value();
-
-  if (desk_guid.is_valid()) {
-    window_info->desk_guid = desk_guid;
-  }
-
-  if (current_bounds.has_value())
-    window_info->current_bounds = current_bounds.value();
-
-  if (window_state_type.has_value())
-    window_info->window_state_type = window_state_type.value();
-
-  if (pre_minimized_show_state_type.has_value()) {
-    window_info->pre_minimized_show_state_type =
-        pre_minimized_show_state_type.value();
-  }
-
-  if (snap_percentage.has_value())
-    window_info->snap_percentage = snap_percentage;
-
-  if (title.has_value())
-    window_info->app_title = title;
+  window_info->activation_index = activation_index;
+  window_info->desk_id = desk_id;
+  window_info->desk_guid = desk_guid;
+  window_info->current_bounds = current_bounds;
+  window_info->window_state_type = window_state_type;
+  window_info->pre_minimized_show_state_type = pre_minimized_show_state_type;
+  window_info->snap_percentage = snap_percentage;
+  window_info->app_title = title;
 
   if (maximum_size.has_value() || minimum_size.has_value() ||
       title.has_value() || bounds_in_root.has_value()) {
@@ -666,13 +554,9 @@
     window_info->display_id = display_id.value();
 
   if (bounds_in_root.has_value()) {
-    window_info->bounds = gfx::Rect{
-        bounds_in_root.value().x(), bounds_in_root.value().y(),
-        bounds_in_root.value().width(), bounds_in_root.value().height()};
+    window_info->bounds = bounds_in_root.value();
   } else if (current_bounds.has_value()) {
-    window_info->bounds = gfx::Rect{
-        current_bounds.value().x(), current_bounds.value().y(),
-        current_bounds.value().width(), current_bounds.value().height()};
+    window_info->bounds = current_bounds.value();
   }
 
   if (window_state_type.has_value())