Commit 585e1f1d authored by Christopher Cameron's avatar Christopher Cameron Committed by Commit Bot

Create explicit AppState/ProfileState structures in AppShimHandler

The next steps here will be to allow the AppShimHost to be owned either
by the ProfileState (non-multi-profile-mode) or the AppState (multi-
profile mode).

Bug: 982024
Change-Id: Idbc2de4927895800a79e6300a6dbf24fc3ff652e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1771945Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Commit-Queue: ccameron <ccameron@chromium.org>
Cr-Commit-Position: refs/heads/master@{#690922}
parent d5f0058a
...@@ -237,6 +237,19 @@ bool UsesRemoteViews(const extensions::Extension* extension) { ...@@ -237,6 +237,19 @@ bool UsesRemoteViews(const extensions::Extension* extension) {
namespace apps { namespace apps {
ExtensionAppShimHandler::ProfileState::ProfileState() = default;
ExtensionAppShimHandler::ProfileState::ProfileState(ProfileState&& other) =
default;
ExtensionAppShimHandler::ProfileState& ExtensionAppShimHandler::ProfileState::
operator=(ProfileState&& other) = default;
ExtensionAppShimHandler::ProfileState::~ProfileState() = default;
ExtensionAppShimHandler::AppState::AppState() = default;
ExtensionAppShimHandler::AppState::AppState(AppState&& other) = default;
ExtensionAppShimHandler::AppState& ExtensionAppShimHandler::AppState::operator=(
AppState&& other) = default;
ExtensionAppShimHandler::AppState::~AppState() = default;
base::FilePath ExtensionAppShimHandler::Delegate::GetFullProfilePath( base::FilePath ExtensionAppShimHandler::Delegate::GetFullProfilePath(
const base::FilePath& relative_profile_path) { const base::FilePath& relative_profile_path) {
ProfileManager* profile_manager = g_browser_process->profile_manager(); ProfileManager* profile_manager = g_browser_process->profile_manager();
...@@ -396,8 +409,15 @@ ExtensionAppShimHandler::~ExtensionAppShimHandler() { ...@@ -396,8 +409,15 @@ ExtensionAppShimHandler::~ExtensionAppShimHandler() {
AppShimHost* ExtensionAppShimHandler::FindHost(Profile* profile, AppShimHost* ExtensionAppShimHandler::FindHost(Profile* profile,
const std::string& app_id) { const std::string& app_id) {
HostMap::iterator it = hosts_.find(make_pair(profile, app_id)); auto found_app = apps_.find(app_id);
return it == hosts_.end() ? nullptr : it->second.get(); if (found_app == apps_.end())
return nullptr;
AppState& app_state = found_app->second;
auto found_profile = app_state.profiles.find(profile);
if (found_profile == app_state.profiles.end())
return nullptr;
ProfileState& profile_state = found_profile->second;
return profile_state.host.get();
} }
AppShimHost* ExtensionAppShimHandler::FindOrCreateHost( AppShimHost* ExtensionAppShimHandler::FindOrCreateHost(
...@@ -405,14 +425,10 @@ AppShimHost* ExtensionAppShimHandler::FindOrCreateHost( ...@@ -405,14 +425,10 @@ AppShimHost* ExtensionAppShimHandler::FindOrCreateHost(
const extensions::Extension* extension) { const extensions::Extension* extension) {
if (web_app::AppShimLaunchDisabled()) if (web_app::AppShimLaunchDisabled())
return nullptr; return nullptr;
auto key = make_pair(profile, extension->id()); ProfileState& profile_state = apps_[extension->id()].profiles[profile];
auto it = hosts_.find(key); if (!profile_state.host)
if (it == hosts_.end()) { profile_state.host = delegate_->CreateHost(profile, extension);
std::unique_ptr<AppShimHost> new_host = return profile_state.host.get();
delegate_->CreateHost(profile, extension);
it = hosts_.emplace(make_pair(key, std::move(new_host))).first;
}
return it->second.get();
} }
AppShimHost* ExtensionAppShimHandler::GetHostForBrowser(Browser* browser) { AppShimHost* ExtensionAppShimHandler::GetHostForBrowser(Browser* browser) {
...@@ -546,8 +562,13 @@ void ExtensionAppShimHandler::OnChromeWillHide() { ...@@ -546,8 +562,13 @@ void ExtensionAppShimHandler::OnChromeWillHide() {
// Send OnAppHide to all the shims so that they go into the hidden state. // Send OnAppHide to all the shims so that they go into the hidden state.
// This is necessary so that when the shim is next focused, it will know to // This is necessary so that when the shim is next focused, it will know to
// unhide. // unhide.
for (auto& entry : hosts_) for (auto& iter_app : apps_) {
entry.second->OnAppHide(); AppState& app_state = iter_app.second;
for (auto& iter_profile : app_state.profiles) {
ProfileState& profile_state = iter_profile.second;
profile_state.host->OnAppHide();
}
}
} }
void ExtensionAppShimHandler::OnShimLaunchRequested( void ExtensionAppShimHandler::OnShimLaunchRequested(
...@@ -624,10 +645,8 @@ const Extension* ExtensionAppShimHandler::MaybeGetExtensionOrCloseHost( ...@@ -624,10 +645,8 @@ const Extension* ExtensionAppShimHandler::MaybeGetExtensionOrCloseHost(
const Extension* extension = const Extension* extension =
delegate_->MaybeGetAppExtension(profile, host->GetAppId()); delegate_->MaybeGetAppExtension(profile, host->GetAppId());
if (!extension) { if (!extension)
auto key = std::make_pair(profile, host->GetAppId()); CloseShimForApp(profile, host->GetAppId());
hosts_.erase(key);
}
if (profile_out) if (profile_out)
*profile_out = profile; *profile_out = profile;
...@@ -645,6 +664,28 @@ void ExtensionAppShimHandler::CloseBrowsersForApp(Profile* profile, ...@@ -645,6 +664,28 @@ void ExtensionAppShimHandler::CloseBrowsersForApp(Profile* profile,
browser->window()->Close(); browser->window()->Close();
} }
void ExtensionAppShimHandler::CloseShimsForProfile(Profile* profile) {
for (auto iter_app = apps_.begin(); iter_app != apps_.end();) {
AppState& app_state = iter_app->second;
app_state.profiles.erase(profile);
if (app_state.profiles.empty())
iter_app = apps_.erase(iter_app);
else
++iter_app;
}
}
void ExtensionAppShimHandler::CloseShimForApp(Profile* profile,
const std::string& app_id) {
auto found_app = apps_.find(app_id);
if (found_app == apps_.end())
return;
AppState& app_state = found_app->second;
app_state.profiles.erase(profile);
if (app_state.profiles.empty())
apps_.erase(found_app);
}
void ExtensionAppShimHandler::OnProfileLoaded( void ExtensionAppShimHandler::OnProfileLoaded(
std::unique_ptr<AppShimHostBootstrap> bootstrap, std::unique_ptr<AppShimHostBootstrap> bootstrap,
Profile* profile) { Profile* profile) {
...@@ -735,10 +776,18 @@ bool ExtensionAppShimHandler::IsAcceptablyCodeSigned(pid_t pid) const { ...@@ -735,10 +776,18 @@ bool ExtensionAppShimHandler::IsAcceptablyCodeSigned(pid_t pid) const {
void ExtensionAppShimHandler::OnShimProcessDisconnected(AppShimHost* host) { void ExtensionAppShimHandler::OnShimProcessDisconnected(AppShimHost* host) {
// This might be called when shutting down. Don't try to look up the profile // This might be called when shutting down. Don't try to look up the profile
// since profile_manager might not be around. // since profile_manager might not be around.
for (HostMap::iterator it = hosts_.begin(); it != hosts_.end(); ) { for (auto iter_app = apps_.begin(); iter_app != apps_.end(); ++iter_app) {
HostMap::iterator current = it++; AppState& app_state = iter_app->second;
if (current->second.get() == host) for (auto iter_profile = app_state.profiles.begin();
hosts_.erase(current); iter_profile != app_state.profiles.end(); ++iter_profile) {
ProfileState& profile_state = iter_profile->second;
if (profile_state.host.get() == host) {
app_state.profiles.erase(iter_profile);
if (app_state.profiles.empty())
apps_.erase(iter_app);
return;
}
}
} }
} }
...@@ -844,13 +893,7 @@ void ExtensionAppShimHandler::Observe( ...@@ -844,13 +893,7 @@ void ExtensionAppShimHandler::Observe(
AppLifetimeMonitorFactory::GetForBrowserContext(profile)->RemoveObserver( AppLifetimeMonitorFactory::GetForBrowserContext(profile)->RemoveObserver(
this); this);
// Shut down every shim associated with this profile. CloseShimsForProfile(profile);
for (HostMap::iterator it = hosts_.begin(); it != hosts_.end(); ) {
if (profile->IsSameProfile(it->first.first))
it = hosts_.erase(it);
else
++it;
}
break; break;
} }
case chrome::NOTIFICATION_BROWSER_OPENED: { case chrome::NOTIFICATION_BROWSER_OPENED: {
...@@ -893,9 +936,8 @@ void ExtensionAppShimHandler::OnAppActivated(content::BrowserContext* context, ...@@ -893,9 +936,8 @@ void ExtensionAppShimHandler::OnAppActivated(content::BrowserContext* context,
void ExtensionAppShimHandler::OnAppDeactivated(content::BrowserContext* context, void ExtensionAppShimHandler::OnAppDeactivated(content::BrowserContext* context,
const std::string& app_id) { const std::string& app_id) {
auto key = std::make_pair(static_cast<Profile*>(context), app_id); CloseShimForApp(static_cast<Profile*>(context), app_id);
hosts_.erase(key); if (apps_.empty())
if (hosts_.empty())
delegate_->MaybeTerminate(); delegate_->MaybeTerminate();
} }
......
...@@ -171,9 +171,6 @@ class ExtensionAppShimHandler : public AppShimHandler, ...@@ -171,9 +171,6 @@ class ExtensionAppShimHandler : public AppShimHandler,
void OnBrowserRemoved(Browser* browser) override; void OnBrowserRemoved(Browser* browser) override;
protected: protected:
typedef std::map<std::pair<Profile*, std::string>,
std::unique_ptr<AppShimHost>>
HostMap;
typedef std::set<Browser*> BrowserSet; typedef std::set<Browser*> BrowserSet;
typedef std::map<std::pair<Profile*, std::string>, BrowserSet> AppBrowserMap; typedef std::map<std::pair<Profile*, std::string>, BrowserSet> AppBrowserMap;
...@@ -196,6 +193,12 @@ class ExtensionAppShimHandler : public AppShimHandler, ...@@ -196,6 +193,12 @@ class ExtensionAppShimHandler : public AppShimHandler,
// Closes all browsers associated with an app. // Closes all browsers associated with an app.
void CloseBrowsersForApp(Profile* profile, const std::string& app_id); void CloseBrowsersForApp(Profile* profile, const std::string& app_id);
// Close all app shims associated with the specified profile.
void CloseShimsForProfile(Profile* profile);
// Close one specified app.
void CloseShimForApp(Profile* profile, const std::string& app_id);
// This is passed to Delegate::LoadProfileAsync for shim-initiated launches // This is passed to Delegate::LoadProfileAsync for shim-initiated launches
// where the profile was not yet loaded. // where the profile was not yet loaded.
void OnProfileLoaded(std::unique_ptr<AppShimHostBootstrap> bootstrap, void OnProfileLoaded(std::unique_ptr<AppShimHostBootstrap> bootstrap,
...@@ -207,7 +210,39 @@ class ExtensionAppShimHandler : public AppShimHandler, ...@@ -207,7 +210,39 @@ class ExtensionAppShimHandler : public AppShimHandler,
std::unique_ptr<Delegate> delegate_; std::unique_ptr<Delegate> delegate_;
HostMap hosts_; // The state for an individual (app, Profile) pair. This includes the
// AppShimHost.
// TODO(https://crbug.com/982024): Add browser windows.
struct ProfileState {
ProfileState();
ProfileState(ProfileState&& other);
ProfileState& operator=(ProfileState&& other);
~ProfileState();
std::unique_ptr<AppShimHost> host;
private:
DISALLOW_COPY_AND_ASSIGN(ProfileState);
};
// The state for an individual app. This includes the state for all
// profiles that are using the app.
// TODO(https://crbug.com/982024): Add AppShimHost when shared amongst
// profiles.
struct AppState {
AppState();
AppState(AppState&& other);
AppState& operator=(AppState&& other);
~AppState();
std::map<Profile*, ProfileState> profiles;
private:
DISALLOW_COPY_AND_ASSIGN(AppState);
};
// Map from extension id to the state for that app.
std::map<std::string, AppState> apps_;
// A map of app ids to associated browser windows. // A map of app ids to associated browser windows.
AppBrowserMap app_browser_windows_; AppBrowserMap app_browser_windows_;
......
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