Commit a2ec149b authored by David Bienvenu's avatar David Bienvenu Committed by Commit Bot

desktop-pwas: Flesh out OnWebAppInstalled function for file extension registration.

These will be used to register the file extensions the PWA handles in the
Windows registry, and set up the shim app, when a PWA is installed,
once the Windows-specific code is implemented.

Bug: 960245
Change-Id: Id6142b59277c60cb72204c37f086f4df3c0ffb91
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1797024
Commit-Queue: David Bienvenu <davidbienvenu@chromium.org>
Reviewed-by: default avatarAlexey Baskakov <loyso@chromium.org>
Cr-Commit-Position: refs/heads/master@{#697398}
parent c183c84a
......@@ -37,6 +37,8 @@ source_set("components") {
"web_app_constants.h",
"web_app_data_retriever.cc",
"web_app_data_retriever.h",
"web_app_file_extension_registration.h",
"web_app_file_extension_registration_win.cc",
"web_app_helpers.cc",
"web_app_helpers.h",
"web_app_icon_generator.cc",
......@@ -102,6 +104,7 @@ source_set("unit_tests") {
testonly = true
sources = [
"file_handler_manager_unittest.cc",
"pending_app_manager_unittest.cc",
"web_app_data_retriever_unittest.cc",
"web_app_helpers_unittest.cc",
......
......@@ -4,6 +4,11 @@
#include "chrome/browser/web_applications/components/file_handler_manager.h"
#include "base/feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/web_applications/components/web_app_file_extension_registration.h"
#include "third_party/blink/public/common/features.h"
namespace web_app {
FileHandlerManager::FileHandlerManager(Profile* profile)
......@@ -17,17 +22,39 @@ void FileHandlerManager::SetSubsystems(AppRegistrar* registrar) {
}
void FileHandlerManager::OnWebAppInstalled(const AppId& installed_app_id) {
// TODO(davidbienvenu): Hook up with file association registration code on
// windows.
#if defined(OS_WIN)
if (!base::FeatureList::IsEnabled(blink::features::kFileHandlingAPI))
return;
std::string app_name = registrar_->GetAppShortName(installed_app_id);
const std::vector<apps::FileHandlerInfo>* file_handlers =
GetFileHandlers(installed_app_id);
if (!file_handlers)
return;
std::set<std::string> file_extensions =
GetFileExtensionsFromFileHandlers(*file_handlers);
RegisterFileHandlersForWebApp(installed_app_id, app_name, *profile_,
file_extensions);
#endif // OS_WIN
}
void FileHandlerManager::OnWebAppUninstalled(const AppId& installed_app_id) {
// TODO(davidbienvenu): Hook up to file association de-registration code on
// windows.
#if defined(OS_WIN)
UnregisterFileHandlersForWebApp(installed_app_id, *profile_);
#endif // OS_WIN
}
void FileHandlerManager::OnAppRegistrarDestroyed() {
registrar_observer_.RemoveAll();
}
std::set<std::string> GetFileExtensionsFromFileHandlers(
const std::vector<apps::FileHandlerInfo>& file_handlers) {
std::set<std::string> file_extensions;
for (const auto& file_handler : file_handlers) {
for (const auto& file_ext : file_handler.extensions)
file_extensions.insert(file_ext);
}
return file_extensions;
}
} // namespace web_app
......@@ -5,6 +5,8 @@
#ifndef CHROME_BROWSER_WEB_APPLICATIONS_COMPONENTS_FILE_HANDLER_MANAGER_H_
#define CHROME_BROWSER_WEB_APPLICATIONS_COMPONENTS_FILE_HANDLER_MANAGER_H_
#include <set>
#include <string>
#include <vector>
#include "base/macros.h"
......@@ -49,6 +51,10 @@ class FileHandlerManager : public AppRegistrarObserver {
DISALLOW_COPY_AND_ASSIGN(FileHandlerManager);
};
// Compute the set of file extensions specified in |file_handlers|.
std::set<std::string> GetFileExtensionsFromFileHandlers(
const std::vector<apps::FileHandlerInfo>& file_handlers);
} // namespace web_app
#endif // CHROME_BROWSER_WEB_APPLICATIONS_COMPONENTS_FILE_HANDLER_MANAGER_H_
// Copyright 2019 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 "chrome/browser/web_applications/components/file_handler_manager.h"
#include <set>
#include <string>
#include <vector>
#include "chrome/browser/web_applications/test/test_file_handler_manager.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace web_app {
TEST(FileHandlerUtilsTest, GetFileExtensionsFromFileHandlers) {
// Construct FileHandlerInfo vector with multiple file extensions.
const std::vector<std::string> test_file_extensions = {"txt", "xls", "doc"};
apps::FileHandlerInfo fhi1;
apps::FileHandlerInfo fhi2;
fhi1.extensions.insert(test_file_extensions[0]);
fhi1.extensions.insert(test_file_extensions[1]);
fhi2.extensions.insert(test_file_extensions[2]);
std::vector<apps::FileHandlerInfo> file_handlers = {fhi1, fhi2};
std::set<std::string> file_extensions =
GetFileExtensionsFromFileHandlers(file_handlers);
EXPECT_EQ(file_extensions.size(), test_file_extensions.size());
for (const auto& test_file_extension : test_file_extensions) {
EXPECT_TRUE(file_extensions.find(test_file_extension) !=
file_extensions.end());
}
}
} // namespace web_app
// Copyright 2019 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.
#ifndef CHROME_BROWSER_WEB_APPLICATIONS_COMPONENTS_WEB_APP_FILE_EXTENSION_REGISTRATION_H_
#define CHROME_BROWSER_WEB_APPLICATIONS_COMPONENTS_WEB_APP_FILE_EXTENSION_REGISTRATION_H_
#include <set>
#include <string>
#include "chrome/browser/web_applications/components/web_app_helpers.h"
class Profile;
namespace web_app {
// Do OS-specific registration to handle opening files with the specified
// |file_extensions| with the PWA with the specified |app_id|.
// This may also involve creating a shim app to launch Chrome from.
void RegisterFileHandlersForWebApp(
const AppId& app_id,
const std::string& app_name,
const Profile& profile,
const std::set<std::string>& file_extensions);
// Undo the file extensions registration for the PWA with specified |app_id|.
// If a shim app was required, also removes the shim app.
void UnregisterFileHandlersForWebApp(const AppId& app_id,
const Profile& profile);
} // namespace web_app
#endif // CHROME_BROWSER_WEB_APPLICATIONS_COMPONENTS_WEB_APP_FILE_EXTENSION_REGISTRATION_H_
// Copyright 2019 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 "chrome/browser/web_applications/components/web_app_file_extension_registration.h"
#include "chrome/browser/profiles/profile.h"
namespace web_app {
void RegisterFileHandlersForWebApp(
const AppId& app_id,
const std::string& app_name,
const Profile& profile,
const std::set<std::string>& file_extensions) {
// TODO(davidbienvenu): Setup shim app and windows registry for this |app_id|.
}
void UnregisterFileHandlersForWebApp(const AppId& app_id,
const Profile& profile) {
// TODO(davidbienvenu): Cleanup windows registry entries for this
// |app_id|.
}
} // namespace web_app
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