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

Make Windows jumplist menu items be profile-specific.

If the user has more than one profile,
specify the profile directory in the jumplist menu item.

Bug: 19072
Change-Id: I6ece249f4414d01779f30473a24984a73164e08e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2079216
Commit-Queue: David Bienvenu <davidbienvenu@chromium.org>
Reviewed-by: default avatarGreg Thompson <grt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#747435}
parent 31b5ab0c
......@@ -25,11 +25,14 @@
#include "base/threading/thread.h"
#include "base/timer/elapsed_timer.h"
#include "base/trace_event/trace_event.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/favicon/favicon_service_factory.h"
#include "chrome/browser/history/top_sites_factory.h"
#include "chrome/browser/metrics/jumplist_metrics_win.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/sessions/tab_restore_service_factory.h"
#include "chrome/browser/shell_integration_win.h"
#include "chrome/browser/win/jumplist_file_util.h"
......@@ -98,18 +101,25 @@ constexpr base::TimeDelta kTimeOutForCommitUpdate =
base::TimeDelta::FromMilliseconds(1000);
// Appends the common switches to each shell link.
void AppendCommonSwitches(ShellLinkItem* shell_link) {
void AppendCommonSwitches(const base::FilePath& cmd_line_profile_dir,
ShellLinkItem* shell_link) {
const char* kSwitchNames[] = { switches::kUserDataDir };
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
shell_link->GetCommandLine()->CopySwitchesFrom(command_line, kSwitchNames,
base::size(kSwitchNames));
if (!cmd_line_profile_dir.empty()) {
shell_link->GetCommandLine()->AppendSwitchPath(switches::kProfileDirectory,
cmd_line_profile_dir);
}
}
// Creates a ShellLinkItem preloaded with common switches.
scoped_refptr<ShellLinkItem> CreateShellLink() {
// Creates a ShellLinkItem preloaded with common switches, and the profile
// directory, if |profile_dir| is non-empty.
scoped_refptr<ShellLinkItem> CreateShellLink(
const base::FilePath& cmd_line_profile_dir) {
auto link = base::MakeRefCounted<ShellLinkItem>();
AppendCommonSwitches(link.get());
AppendCommonSwitches(cmd_line_profile_dir, link.get());
return link;
}
......@@ -152,9 +162,9 @@ bool CreateIconFile(const gfx::ImageSkia& image_skia,
}
// Updates the "Tasks" category of the JumpList.
bool UpdateTaskCategory(
JumpListUpdater* jumplist_updater,
IncognitoModePrefs::Availability incognito_availability) {
bool UpdateTaskCategory(JumpListUpdater* jumplist_updater,
IncognitoModePrefs::Availability incognito_availability,
const base::FilePath& cmd_line_profile_dir) {
base::FilePath chrome_path;
if (!base::PathService::Get(base::FILE_EXE, &chrome_path))
return false;
......@@ -168,7 +178,7 @@ bool UpdateTaskCategory(
// We remove '&' characters from this string so we can share it with our
// system menu.
if (incognito_availability != IncognitoModePrefs::FORCED) {
scoped_refptr<ShellLinkItem> chrome = CreateShellLink();
scoped_refptr<ShellLinkItem> chrome = CreateShellLink(cmd_line_profile_dir);
base::string16 chrome_title = l10n_util::GetStringUTF16(IDS_NEW_WINDOW);
base::ReplaceSubstringsAfterOffset(
&chrome_title, 0, L"&", base::StringPiece16());
......@@ -180,7 +190,8 @@ bool UpdateTaskCategory(
// Create an IShellLink object which launches Chrome in incognito mode, and
// add it to the collection.
if (incognito_availability != IncognitoModePrefs::DISABLED) {
scoped_refptr<ShellLinkItem> incognito = CreateShellLink();
scoped_refptr<ShellLinkItem> incognito =
CreateShellLink(cmd_line_profile_dir);
incognito->GetCommandLine()->AppendSwitch(switches::kIncognito);
base::string16 incognito_title =
l10n_util::GetStringUTF16(IDS_NEW_INCOGNITO_WINDOW);
......@@ -411,18 +422,20 @@ void JumpList::ProcessTabRestoreServiceNotification() {
recently_closed_pages_.clear();
base::FilePath profile_dir(GetCmdLineProfileDir());
for (const auto& entry : tab_restore_service->entries()) {
if (recently_closed_pages_.size() >= kRecentlyClosedItems)
break;
switch (entry->type) {
case sessions::TabRestoreService::TAB:
AddTab(static_cast<const sessions::TabRestoreService::Tab&>(*entry),
kRecentlyClosedItems);
profile_dir, kRecentlyClosedItems);
break;
case sessions::TabRestoreService::WINDOW:
AddWindow(
static_cast<const sessions::TabRestoreService::Window&>(*entry),
kRecentlyClosedItems);
profile_dir, kRecentlyClosedItems);
break;
}
}
......@@ -442,11 +455,11 @@ void JumpList::OnMostVisitedURLsAvailable(
return;
most_visited_pages_.clear();
base::FilePath profile_dir = GetCmdLineProfileDir();
const size_t num_items = std::min(urls.size(), kMostVisitedItems);
for (size_t i = 0; i < num_items; ++i) {
const history::MostVisitedURL& url = urls[i];
scoped_refptr<ShellLinkItem> link = CreateShellLink();
scoped_refptr<ShellLinkItem> link = CreateShellLink(profile_dir);
std::string url_string = url.url.spec();
base::string16 url_string_wide = base::UTF8ToUTF16(url_string);
link->GetCommandLine()->AppendArgNative(url_string_wide);
......@@ -466,6 +479,7 @@ void JumpList::OnMostVisitedURLsAvailable(
}
bool JumpList::AddTab(const sessions::TabRestoreService::Tab& tab,
const base::FilePath& cmd_line_profile_dir,
size_t max_items) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
......@@ -474,7 +488,7 @@ bool JumpList::AddTab(const sessions::TabRestoreService::Tab& tab,
if (recently_closed_pages_.size() >= max_items)
return false;
scoped_refptr<ShellLinkItem> link = CreateShellLink();
scoped_refptr<ShellLinkItem> link = CreateShellLink(cmd_line_profile_dir);
const sessions::SerializedNavigationEntry& current_navigation =
tab.navigations.at(tab.current_navigation_index);
std::string url = current_navigation.virtual_url().spec();
......@@ -491,12 +505,13 @@ bool JumpList::AddTab(const sessions::TabRestoreService::Tab& tab,
}
void JumpList::AddWindow(const sessions::TabRestoreService::Window& window,
const base::FilePath& cmd_line_profile_dir,
size_t max_items) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!window.tabs.empty());
for (const auto& tab : window.tabs) {
if (!AddTab(*tab, max_items))
if (!AddTab(*tab, cmd_line_profile_dir, max_items))
return;
}
}
......@@ -572,9 +587,9 @@ void JumpList::PostRunUpdate() {
// Parameter evaluation order is unspecified in C++. Do the first bind and
// then move it into PostTaskAndReply to ensure the pointer value is obtained
// before base::Passed() is called.
auto run_update =
base::Bind(&JumpList::RunUpdateJumpList, app_id_, profile_dir,
most_visited_pages_, recently_closed_pages_,
auto run_update = base::BindOnce(
&JumpList::RunUpdateJumpList, app_id_, profile_dir, most_visited_pages_,
recently_closed_pages_, GetCmdLineProfileDir(),
most_visited_should_update_, recently_closed_should_update_,
incognito_availability, update_transaction.get());
......@@ -582,7 +597,7 @@ void JumpList::PostRunUpdate() {
// 2) notify the OS, 3) delete old icons.
if (!update_jumplist_task_runner_->PostTaskAndReply(
FROM_HERE, std::move(run_update),
base::Bind(&JumpList::OnRunUpdateCompletion,
base::BindOnce(&JumpList::OnRunUpdateCompletion,
weak_ptr_factory_.GetWeakPtr(),
base::Passed(std::move(update_transaction))))) {
OnRunUpdateCompletion(std::make_unique<UpdateTransaction>());
......@@ -674,6 +689,7 @@ void JumpList::RunUpdateJumpList(
const base::FilePath& profile_dir,
const ShellLinkItemList& most_visited_pages,
const ShellLinkItemList& recently_closed_pages,
const base::FilePath& cmd_line_profile_dir,
bool most_visited_should_update,
bool recently_closed_should_update,
IncognitoModePrefs::Availability incognito_availability,
......@@ -687,9 +703,9 @@ void JumpList::RunUpdateJumpList(
CreateNewJumpListAndNotifyOS(
app_id, most_visited_icon_dir, recently_closed_icon_dir,
most_visited_pages, recently_closed_pages, most_visited_should_update,
recently_closed_should_update, incognito_availability,
update_transaction);
most_visited_pages, recently_closed_pages, cmd_line_profile_dir,
most_visited_should_update, recently_closed_should_update,
incognito_availability, update_transaction);
// Delete any obsolete icon files.
if (most_visited_should_update) {
......@@ -709,6 +725,7 @@ void JumpList::CreateNewJumpListAndNotifyOS(
const base::FilePath& recently_closed_icon_dir,
const ShellLinkItemList& most_visited_pages,
const ShellLinkItemList& recently_closed_pages,
const base::FilePath& cmd_line_profile_dir,
bool most_visited_should_update,
bool recently_closed_should_update,
IncognitoModePrefs::Availability incognito_availability,
......@@ -794,7 +811,8 @@ void JumpList::CreateNewJumpListAndNotifyOS(
}
// Update the "Tasks" category of the JumpList.
if (!UpdateTaskCategory(&jumplist_updater, incognito_availability))
if (!UpdateTaskCategory(&jumplist_updater, incognito_availability,
cmd_line_profile_dir))
return;
base::ElapsedTimer commit_update_timer;
......@@ -893,3 +911,11 @@ void JumpList::DeleteIconFiles(const base::FilePath& icon_dir,
DeleteNonCachedFiles(icon_dir, cached_files);
}
base::FilePath JumpList::GetCmdLineProfileDir() {
return g_browser_process->profile_manager()
->GetProfileAttributesStorage()
.GetNumberOfProfiles() < 2
? base::FilePath()
: profile_->GetPath().BaseName();
}
......@@ -170,12 +170,18 @@ class JumpList : public sessions::TabRestoreServiceObserver,
void OnMostVisitedURLsAvailable(const history::MostVisitedURLList& data);
// Adds a new ShellLinkItem for |tab| to the JumpList data provided that doing
// so will not exceed |max_items|.
bool AddTab(const sessions::TabRestoreService::Tab& tab, size_t max_items);
// so will not exceed |max_items|. If |cmd_line_profile_dir| is not empty,
// it will be added to the command line switch --profile-directory.
bool AddTab(const sessions::TabRestoreService::Tab& tab,
const base::FilePath& cmd_line_profile_dir,
size_t max_items);
// Adds a new ShellLinkItem for each tab in |window| to the JumpList data
// provided that doing so will not exceed |max_items|.
// provided that doing so will not exceed |max_items|. If
// |cmd_line_profile_dir| is not empty, it will be added to the command line
// switch --profile-directory.
void AddWindow(const sessions::TabRestoreService::Window& window,
const base::FilePath& cmd_line_profile_dir,
size_t max_items);
// Starts loading a favicon for each URL in |icon_urls_|.
......@@ -218,6 +224,7 @@ class JumpList : public sessions::TabRestoreServiceObserver,
const base::FilePath& profile_dir,
const ShellLinkItemList& most_visited_pages,
const ShellLinkItemList& recently_closed_pages,
const base::FilePath& cmd_line_profile_dir,
bool most_visited_should_update,
bool recently_closed_should_update,
IncognitoModePrefs::Availability incognito_availability,
......@@ -231,6 +238,7 @@ class JumpList : public sessions::TabRestoreServiceObserver,
const base::FilePath& recently_closed_icon_dir,
const ShellLinkItemList& most_visited_pages,
const ShellLinkItemList& recently_closed_pages,
const base::FilePath& cmd_line_profile_dir,
bool most_visited_should_update,
bool recently_closed_should_update,
IncognitoModePrefs::Availability incognito_availability,
......@@ -262,6 +270,11 @@ class JumpList : public sessions::TabRestoreServiceObserver,
static void DeleteIconFiles(const base::FilePath& icon_dir,
const URLIconCache& icons_cache);
// Gets the basename of the profile directory for |profile_|, suitable for
// appending --profile-directory=<profile name> to jumplist items' command
// lines. If the user has only one profile, this returns an empty FilePath.
base::FilePath GetCmdLineProfileDir();
// Tracks FaviconService tasks.
base::CancelableTaskTracker cancelable_task_tracker_;
......
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