Commit 0aa8cbbf authored by Jesse McKenna's avatar Jesse McKenna Committed by Commit Bot

desktop-pwas: Store last Chrome path in user data

This change adds a new file "Last Path" to the user-data directory
containing the path of the last chrome.exe that ran using it. The
implementation mimics the "Last Version" file implementation, so the
last-run chrome.exe path is updated each time chrome.exe is launched.

This will be used by Progressive Web Apps, whose launcher executables
will be located inside the user-data directory, to find the correct
chrome.exe to launch.


Bug: 960245
Change-Id: I0ec658d414dd1a67f03f7d02e645f21f03191360
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1891121Reviewed-by: default avatarGreg Thompson <grt@chromium.org>
Reviewed-by: default avatarMarc Treib <treib@chromium.org>
Reviewed-by: default avatarJay Harris <harrisjay@chromium.org>
Commit-Queue: Jesse McKenna <jessemckenna@google.com>
Cr-Commit-Position: refs/heads/master@{#715031}
parent 56cd1dc8
......@@ -253,6 +253,7 @@
#include "chrome/browser/first_run/upgrade_util_win.h"
#include "chrome/browser/ui/network_profile_bubble.h"
#include "chrome/browser/ui/views/try_chrome_dialog_win/try_chrome_dialog.h"
#include "chrome/browser/web_applications/components/web_app_file_handler_registration_win.h"
#include "chrome/browser/win/browser_util.h"
#include "chrome/browser/win/chrome_select_file_dialog_factory.h"
#include "chrome/install_static/install_util.h"
......@@ -1412,6 +1413,13 @@ int ChromeBrowserMainParts::PreMainMessageLoopRunImpl() {
if (downgrade_manager_.IsMigrationRequired(user_data_dir_))
return chrome::RESULT_CODE_DOWNGRADE_AND_RELAUNCH;
downgrade_manager_.UpdateLastVersion(user_data_dir_);
// Write current executable path to |user_data_dir_| to inform Progressive Web
// App launchers inside |user_data_dir_| which chrome.exe to launch from.
base::PostTask(
FROM_HERE,
{base::ThreadPool(), base::TaskPriority::BEST_EFFORT, base::MayBlock()},
base::BindOnce(&web_app::UpdateChromeExePath, user_data_dir_));
#endif
#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
......
......@@ -44,7 +44,6 @@ source_set("components") {
"web_app_data_retriever.h",
"web_app_file_handler_registration.cc",
"web_app_file_handler_registration.h",
"web_app_file_handler_registration_win.cc",
"web_app_helpers.cc",
"web_app_helpers.h",
"web_app_icon_generator.cc",
......@@ -57,11 +56,6 @@ source_set("components") {
"web_app_provider_base_factory.h",
"web_app_shortcut.cc",
"web_app_shortcut.h",
"web_app_shortcut_chromeos.cc",
"web_app_shortcut_mac.h",
"web_app_shortcut_mac.mm",
"web_app_shortcut_win.cc",
"web_app_shortcut_win.h",
"web_app_tab_helper.cc",
"web_app_tab_helper.h",
"web_app_ui_manager.h",
......@@ -82,6 +76,10 @@ source_set("components") {
"web_app_icon_downloader.h",
]
if (is_chromeos) {
sources += [ "web_app_shortcut_chromeos.cc" ]
}
if (is_desktop_linux) {
# Desktop linux, doesn't count ChromeOS.
sources += [
......@@ -91,6 +89,22 @@ source_set("components") {
]
}
if (is_mac) {
sources += [
"web_app_shortcut_mac.h",
"web_app_shortcut_mac.mm",
]
}
if (is_win) {
sources += [
"web_app_file_handler_registration_win.cc",
"web_app_file_handler_registration_win.h",
"web_app_shortcut_win.cc",
"web_app_shortcut_win.h",
]
}
deps = [
"//base/util/values:values_util",
"//chrome/app/resources:platform_locale_settings",
......@@ -122,11 +136,18 @@ source_set("unit_tests") {
"web_app_icon_downloader_unittest.cc",
"web_app_icon_generator_unittest.cc",
"web_app_install_utils_unittest.cc",
"web_app_shortcut_mac_unittest.mm",
"web_app_shortcut_unittest.cc",
"web_app_utils_unittest.cc",
]
if (is_win) {
sources += [ "web_app_file_handler_registration_win_unittest.cc" ]
}
if (is_mac) {
sources += [ "web_app_shortcut_mac_unittest.mm" ]
}
if (is_desktop_linux) {
# Desktop linux, doesn't count ChromeOS.
sources += [ "web_app_shortcut_linux_unittest.cc" ]
......
......@@ -14,7 +14,7 @@ class Profile;
namespace web_app {
// returns true if Chrome supports WebApp File Handling on this OS.
// Returns true if Chrome supports WebApp File Handling on this OS.
bool OsSupportsWebAppFileHandling();
// Do OS-specific registration to handle opening files with the specified
......
......@@ -2,12 +2,18 @@
// 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_handler_registration.h"
#include "chrome/browser/web_applications/components/web_app_file_handler_registration_win.h"
#include "base/base_paths.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "chrome/browser/profiles/profile.h"
namespace web_app {
const base::FilePath::StringPieceType kLastBrowserFile(
FILE_PATH_LITERAL("Last Browser"));
bool OsSupportsWebAppFileHandling() {
return true;
}
......@@ -21,8 +27,21 @@ void RegisterFileHandlersForWebApp(const AppId& app_id,
}
void UnregisterFileHandlersForWebApp(const AppId& app_id, Profile* profile) {
// TODO(davidbienvenu): Cleanup windows registry entries for this
// |app_id|.
// TODO(davidbienvenu): Cleanup windows registry entries for this |app_id|.
}
void UpdateChromeExePath(const base::FilePath& user_data_dir) {
DCHECK(!user_data_dir.empty());
base::FilePath chrome_exe_path;
if (!base::PathService::Get(base::FILE_EXE, &chrome_exe_path))
return;
const base::FilePath::StringType& chrome_exe_path_str =
chrome_exe_path.value();
DCHECK(!chrome_exe_path_str.empty());
base::WriteFile(
user_data_dir.Append(kLastBrowserFile),
reinterpret_cast<const char*>(chrome_exe_path_str.data()),
chrome_exe_path_str.size() * sizeof(base::FilePath::CharType));
}
} // 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_HANDLER_REGISTRATION_WIN_H_
#define CHROME_BROWSER_WEB_APPLICATIONS_COMPONENTS_WEB_APP_FILE_HANDLER_REGISTRATION_WIN_H_
#include "chrome/browser/web_applications/components/web_app_file_handler_registration.h"
#include "base/files/file_path.h"
namespace web_app {
// The name of "Last Browser" file, where UpdateChromeExePath() stores the path
// of the last Chrome executable to use the containing user-data directory.
extern const base::FilePath::StringPieceType kLastBrowserFile;
// Writes the current executable path into the "Last Browser" file in
// |user_data_dir|. This allows Progressive Web Apps in |user_data_dir| to
// find and launch |user_data_dir|'s corresponding chrome.exe, even if it has
// moved (e.g. if a user-level install has been replaced by a system-level
// install), in which case the path will be fixed when the new chrome.exe is
// launched.
void UpdateChromeExePath(const base::FilePath& user_data_dir);
} // namespace web_app
#endif // CHROME_BROWSER_WEB_APPLICATIONS_COMPONENTS_WEB_APP_FILE_HANDLER_REGISTRATION_WIN_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_handler_registration_win.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_path_override.h"
#include "chrome/common/chrome_paths.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace web_app {
class UpdateChromeExePathTest : public testing::Test {
protected:
UpdateChromeExePathTest() : user_data_dir_override_(chrome::DIR_USER_DATA) {}
void SetUp() override {
ASSERT_TRUE(base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir_));
ASSERT_FALSE(user_data_dir_.empty());
last_browser_file_ = user_data_dir_.Append(kLastBrowserFile);
}
static base::FilePath GetCurrentExePath() {
base::FilePath current_exe_path;
EXPECT_TRUE(base::PathService::Get(base::FILE_EXE, &current_exe_path));
return current_exe_path;
}
base::FilePath GetLastBrowserPathFromFile() const {
std::string last_browser_file_data;
EXPECT_TRUE(
base::ReadFileToString(last_browser_file_, &last_browser_file_data));
base::FilePath::StringPieceType last_browser_path(
reinterpret_cast<const base::FilePath::CharType*>(
last_browser_file_data.data()),
last_browser_file_data.size() / sizeof(base::FilePath::CharType));
return base::FilePath(last_browser_path);
}
const base::FilePath& user_data_dir() const { return user_data_dir_; }
private:
// Redirect |chrome::DIR_USER_DATA| to a temporary directory during testing.
base::ScopedPathOverride user_data_dir_override_;
base::FilePath user_data_dir_;
base::FilePath last_browser_file_;
};
TEST_F(UpdateChromeExePathTest, UpdateChromeExePath) {
UpdateChromeExePath(user_data_dir());
EXPECT_EQ(GetLastBrowserPathFromFile(), GetCurrentExePath());
}
} // 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