Commit 6f52ecac authored by Nancy Wang's avatar Nancy Wang Committed by Chromium LUCI CQ

Add the AddAppLaunchInfo interface for RestoreData.

This CL adds the interface AddAppLaunchInfo to the RestoreData class.
This interface will be used by FullRestoreSaveHandler to save the app
launch parameters to the restore data.

The unit tests are added to verify the interface.

BUG=1146900

Change-Id: I4cc9a5f967ce1c9d19d14ffc79f390fd91cb89db
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2581623
Commit-Queue: Nancy Wang <nancylingwang@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#836540}
parent 775e6075
......@@ -468,7 +468,7 @@ content::WebContents* WebAppsChromeOs::LaunchAppWithParams(
params_for_restore.app_id, session_id, params_for_restore.container,
params_for_restore.disposition, params_for_restore.display_id,
std::move(params_for_restore.launch_files),
params_for_restore.intent));
std::move(params_for_restore.intent)));
return web_contents;
}
......
......@@ -5095,6 +5095,7 @@ test("unit_tests") {
"//chromeos/services/machine_learning/public/cpp:test_support",
"//chromeos/services/nearby/public/cpp:test_support",
"//components/arc:arc_test_support",
"//components/full_restore:unit_tests",
]
}
......
......@@ -23,3 +23,14 @@ component("full_restore") {
"//components/services/app_service/public/mojom",
]
}
source_set("unit_tests") {
testonly = true
sources = [ "restore_data_unittest.cc" ]
deps = [
":full_restore",
"//testing/gtest",
]
}
......@@ -12,14 +12,14 @@ AppLaunchInfo::AppLaunchInfo(const std::string& app_id,
WindowOpenDisposition disposition,
int64_t display_id,
std::vector<base::FilePath> launch_files,
apps::mojom::IntentPtr& intent)
: app_id_(app_id),
id_(session_id),
container_(static_cast<int32_t>(container)),
disposition_(static_cast<int32_t>(disposition)),
display_id_(display_id),
file_paths_(std::move(launch_files)),
intent_(std::move(intent)) {}
apps::mojom::IntentPtr intent)
: app_id(app_id),
id(session_id),
container(static_cast<int32_t>(container)),
disposition(static_cast<int32_t>(disposition)),
display_id(display_id),
file_paths(std::move(launch_files)),
intent(std::move(intent)) {}
AppLaunchInfo::~AppLaunchInfo() = default;
......
......@@ -17,30 +17,28 @@ namespace full_restore {
// This class is the parameter for the interface SaveAppLaunchInfo, to save the
// app launch information.
class COMPONENT_EXPORT(FULL_RESTORE) AppLaunchInfo {
public:
struct COMPONENT_EXPORT(FULL_RESTORE) AppLaunchInfo {
AppLaunchInfo(const std::string& app_id,
int32_t session_id,
apps::mojom::LaunchContainer container,
WindowOpenDisposition disposition,
int64_t display_id,
std::vector<base::FilePath> launch_files,
apps::mojom::IntentPtr& intent);
apps::mojom::IntentPtr intent);
~AppLaunchInfo();
AppLaunchInfo(const AppLaunchInfo&) = delete;
AppLaunchInfo& operator=(const AppLaunchInfo&) = delete;
private:
const std::string app_id_;
const base::Optional<int32_t> id_;
const base::Optional<int32_t> event_flag_;
const base::Optional<int32_t> container_;
const base::Optional<int32_t> disposition_;
const base::Optional<int64_t> display_id_;
const base::Optional<GURL> url_;
const base::Optional<std::vector<base::FilePath>> file_paths_;
const base::Optional<apps::mojom::IntentPtr> intent_;
std::string app_id;
base::Optional<int32_t> id;
base::Optional<int32_t> event_flag;
base::Optional<int32_t> container;
base::Optional<int32_t> disposition;
base::Optional<int64_t> display_id;
base::Optional<GURL> url;
base::Optional<std::vector<base::FilePath>> file_paths;
base::Optional<apps::mojom::IntentPtr> intent;
};
} // namespace full_restore
......
......@@ -2,11 +2,41 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <utility>
#include "components/full_restore/app_restore_data.h"
#include "components/full_restore/app_launch_info.h"
namespace full_restore {
AppRestoreData::AppRestoreData() = default;
AppRestoreData::~AppRestoreData() = default;
AppRestoreData::AppRestoreData(std::unique_ptr<AppLaunchInfo> app_launch_info) {
if (!app_launch_info)
return;
if (app_launch_info->event_flag.has_value())
event_flag = app_launch_info->event_flag.value();
if (app_launch_info->container.has_value())
container = app_launch_info->container.value();
if (app_launch_info->disposition.has_value())
disposition = app_launch_info->disposition.value();
if (app_launch_info->display_id.has_value())
display_id = app_launch_info->display_id.value();
if (app_launch_info->url.has_value())
url = std::move(app_launch_info->url.value());
if (app_launch_info->file_paths.has_value())
file_paths = std::move(app_launch_info->file_paths.value());
if (app_launch_info->intent.has_value())
intent = std::move(app_launch_info->intent.value());
}
} // namespace full_restore
......@@ -5,6 +5,7 @@
#ifndef COMPONENTS_FULL_RESTORE_APP_RESTORE_DATA_H_
#define COMPONENTS_FULL_RESTORE_APP_RESTORE_DATA_H_
#include <memory>
#include <vector>
#include "base/component_export.h"
......@@ -15,6 +16,8 @@
namespace full_restore {
struct AppLaunchInfo;
// This is the struct used by RestoreData to save both app launch parameters and
// app window information. This struct can be converted to JSON format to be
// written to the FullRestoreData file.
......@@ -28,6 +31,8 @@ struct COMPONENT_EXPORT(FULL_RESTORE) AppRestoreData {
AppRestoreData(const AppRestoreData&) = delete;
AppRestoreData& operator=(const AppRestoreData&) = delete;
AppRestoreData(std::unique_ptr<AppLaunchInfo> app_launch_info);
// App launch parameters.
base::Optional<int32_t> event_flag;
base::Optional<int32_t> container;
......
......@@ -15,7 +15,7 @@ class FilePath;
namespace full_restore {
class AppLaunchInfo;
struct AppLaunchInfo;
// Saves the app launch parameters to the full restore file.
COMPONENT_EXPORT(FULL_RESTORE)
......
......@@ -2,11 +2,26 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <utility>
#include "components/full_restore/restore_data.h"
#include "components/full_restore/app_launch_info.h"
namespace full_restore {
RestoreData::RestoreData() = default;
RestoreData::~RestoreData() = default;
void RestoreData::AddAppLaunchInfo(
std::unique_ptr<AppLaunchInfo> app_launch_info) {
if (!app_launch_info || !app_launch_info->id.has_value())
return;
const std::string app_id = app_launch_info->app_id;
const int32_t id = app_launch_info->id.value();
app_id_to_launch_list_[app_id][id] =
std::make_unique<AppRestoreData>(std::move(app_launch_info));
}
} // namespace full_restore
......@@ -13,16 +13,17 @@
namespace full_restore {
struct AppLaunchInfo;
// This class is responsible for saving all app launch and app windows
// information. It can be converted to JSON format to be written to the
// FullRestoreData file.
//
// TODO(crbug.com/1146900):
// 1. Add the interface to convert this struct to JSON format.
// 2. Add the interface to add a LaunchAndWindowInfo with AppLaunchInfo.
// 3. Add the interface to modify LaunchAndWindowInfo when the window
// 2. Add the interface to modify LaunchAndWindowInfo when the window
// information is updated.
// 4. Add the interface to remove LaunchAndWindowInfo.
// 3. Add the interface to remove LaunchAndWindowInfo.
class COMPONENT_EXPORT(FULL_RESTORE) RestoreData {
public:
// Map from a window id to AppRestoreData.
......@@ -37,6 +38,13 @@ class COMPONENT_EXPORT(FULL_RESTORE) RestoreData {
RestoreData(const RestoreData&) = delete;
RestoreData& operator=(const RestoreData&) = delete;
// Add |app_launch_info| to |app_id_to_launch_list_|.
void AddAppLaunchInfo(std::unique_ptr<AppLaunchInfo> app_launch_info);
const AppIdToLaunchList& app_id_to_launch_list() {
return app_id_to_launch_list_;
}
private:
AppIdToLaunchList app_id_to_launch_list_;
};
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/full_restore/restore_data.h"
#include "components/full_restore/app_launch_info.h"
#include "components/full_restore/app_restore_data.h"
#include "components/services/app_service/public/mojom/types.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/window_open_disposition.h"
namespace full_restore {
namespace {
const char kAppId1[] = "aaa";
const char kAppId2[] = "bbb";
const int32_t kId1 = 100;
const int32_t kId2 = 200;
const int32_t kId3 = 300;
const int64_t kDisplayId1 = 22000000;
const int64_t kDisplayId2 = 11000000;
const char kFilePath1[] = "path1";
const char kFilePath2[] = "path2";
const char kIntentActionView[] = "view";
const char kIntentActionSend[] = "send";
const char kMimeType[] = "text/plain";
const char kShareText1[] = "text1";
const char kShareText2[] = "text2";
} // namespace
// Unit tests for restore data.
class RestoreDataTest : public testing::Test {
public:
RestoreDataTest() = default;
~RestoreDataTest() override = default;
RestoreDataTest(const RestoreDataTest&) = delete;
RestoreDataTest& operator=(const RestoreDataTest&) = delete;
apps::mojom::IntentPtr CreateIntent(const std::string& action,
const std::string& mime_type,
const std::string& share_text) {
auto intent = apps::mojom::Intent::New();
intent->action = action;
intent->mime_type = mime_type;
intent->share_text = share_text;
return intent;
}
void VerifyAppRestoreData(const std::unique_ptr<AppRestoreData>& data,
apps::mojom::LaunchContainer container,
WindowOpenDisposition disposition,
int64_t display_id,
std::vector<base::FilePath> file_paths,
apps::mojom::IntentPtr intent) {
EXPECT_TRUE(data->container.has_value());
EXPECT_EQ(static_cast<int>(container), data->container.value());
EXPECT_TRUE(data->disposition.has_value());
EXPECT_EQ(static_cast<int>(disposition), data->disposition.value());
EXPECT_TRUE(data->display_id.has_value());
EXPECT_EQ(display_id, data->display_id.value());
EXPECT_TRUE(data->file_paths.has_value());
EXPECT_EQ(file_paths.size(), data->file_paths.value().size());
for (size_t i = 0; i < file_paths.size(); i++)
EXPECT_EQ(file_paths[i], data->file_paths.value()[i]);
EXPECT_TRUE(data->intent.has_value());
EXPECT_EQ(intent->action, data->intent.value()->action);
EXPECT_EQ(intent->mime_type, data->intent.value()->mime_type);
EXPECT_EQ(intent->share_text, data->intent.value()->share_text);
}
RestoreData& restore_data() { return restore_data_; }
const RestoreData::AppIdToLaunchList& app_id_to_launch_list() {
return restore_data_.app_id_to_launch_list();
}
private:
RestoreData restore_data_;
};
TEST_F(RestoreDataTest, AddNullAppLaunchInfo) {
restore_data().AddAppLaunchInfo(nullptr);
EXPECT_TRUE(app_id_to_launch_list().empty());
}
TEST_F(RestoreDataTest, AddAppLaunchInfos) {
std::unique_ptr<AppLaunchInfo> app_launch_info1 =
std::make_unique<AppLaunchInfo>(
kAppId1, kId1, apps::mojom::LaunchContainer::kLaunchContainerWindow,
WindowOpenDisposition::NEW_WINDOW, kDisplayId1,
std::vector<base::FilePath>{base::FilePath(kFilePath1),
base::FilePath(kFilePath2)},
CreateIntent(kIntentActionSend, kMimeType, kShareText1));
std::unique_ptr<AppLaunchInfo> app_launch_info2 =
std::make_unique<AppLaunchInfo>(
kAppId1, kId2, apps::mojom::LaunchContainer::kLaunchContainerTab,
WindowOpenDisposition::NEW_FOREGROUND_TAB, kDisplayId2,
std::vector<base::FilePath>{base::FilePath(kFilePath2)},
CreateIntent(kIntentActionView, kMimeType, kShareText2));
std::unique_ptr<AppLaunchInfo> app_launch_info3 =
std::make_unique<AppLaunchInfo>(
kAppId2, kId3, apps::mojom::LaunchContainer::kLaunchContainerNone,
WindowOpenDisposition::NEW_POPUP, kDisplayId2,
std::vector<base::FilePath>{base::FilePath(kFilePath1)},
CreateIntent(kIntentActionView, kMimeType, kShareText1));
restore_data().AddAppLaunchInfo(std::move(app_launch_info1));
restore_data().AddAppLaunchInfo(std::move(app_launch_info2));
restore_data().AddAppLaunchInfo(std::move(app_launch_info3));
EXPECT_EQ(2u, app_id_to_launch_list().size());
// Verify for |kAppId1|
const auto launch_list_it1 = app_id_to_launch_list().find(kAppId1);
EXPECT_TRUE(launch_list_it1 != app_id_to_launch_list().end());
EXPECT_EQ(2u, launch_list_it1->second.size());
const auto app_restore_data_it1 = launch_list_it1->second.find(kId1);
EXPECT_TRUE(app_restore_data_it1 != launch_list_it1->second.end());
VerifyAppRestoreData(app_restore_data_it1->second,
apps::mojom::LaunchContainer::kLaunchContainerWindow,
WindowOpenDisposition::NEW_WINDOW, kDisplayId1,
std::vector<base::FilePath>{base::FilePath(kFilePath1),
base::FilePath(kFilePath2)},
CreateIntent(kIntentActionSend, kMimeType, kShareText1));
const auto app_restore_data_it2 = launch_list_it1->second.find(kId2);
EXPECT_TRUE(app_restore_data_it2 != launch_list_it1->second.end());
VerifyAppRestoreData(app_restore_data_it2->second,
apps::mojom::LaunchContainer::kLaunchContainerTab,
WindowOpenDisposition::NEW_FOREGROUND_TAB, kDisplayId2,
std::vector<base::FilePath>{base::FilePath(kFilePath2)},
CreateIntent(kIntentActionView, kMimeType, kShareText2));
// Verify for |kAppId2|
const auto launch_list_it2 = app_id_to_launch_list().find(kAppId2);
EXPECT_TRUE(launch_list_it2 != app_id_to_launch_list().end());
EXPECT_EQ(1u, launch_list_it2->second.size());
EXPECT_EQ(kId3, launch_list_it2->second.begin()->first);
VerifyAppRestoreData(launch_list_it2->second.begin()->second,
apps::mojom::LaunchContainer::kLaunchContainerNone,
WindowOpenDisposition::NEW_POPUP, kDisplayId2,
std::vector<base::FilePath>{base::FilePath(kFilePath1)},
CreateIntent(kIntentActionView, kMimeType, kShareText1));
}
} // namespace full_restore
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment