Commit ac5fd665 authored by koz@chromium.org's avatar koz@chromium.org

Add PerAppSettingsService and use it to remember what desktop apps should create windows on.

BUG=316062
TBR=ben@chromium.org

Review URL: https://codereview.chromium.org/111553008

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@244466 0039d316-1c4b-4281-b951-d872f2087c98
parent 5c3f6402
// Copyright 2014 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/apps/per_app_settings_service.h"
PerAppSettingsService::PerAppSettingsService() {
}
PerAppSettingsService::~PerAppSettingsService() {
}
void PerAppSettingsService::SetDesktopLastLaunchedFrom(
const std::string& app_id, chrome::HostDesktopType host_desktop) {
default_desktops_[app_id] = host_desktop;
}
chrome::HostDesktopType PerAppSettingsService::GetDesktopLastLaunchedFrom(
const std::string& app_id) const {
DesktopMap::const_iterator it = default_desktops_.find(app_id);
if (it == default_desktops_.end())
NOTREACHED();
return it->second;
}
bool PerAppSettingsService::HasDesktopLastLaunchedFrom(
const std::string& app_id) const {
return default_desktops_.find(app_id) != default_desktops_.end();
}
// Copyright 2014 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_APPS_PER_APP_SETTINGS_SERVICE_H_
#define CHROME_BROWSER_APPS_PER_APP_SETTINGS_SERVICE_H_
#include <map>
#include <string>
#include "chrome/browser/ui/host_desktop.h"
#include "components/browser_context_keyed_service/browser_context_keyed_service.h"
// Stores settings for apps that only persist until the browser context is
// destroyed.
class PerAppSettingsService : public BrowserContextKeyedService {
public:
PerAppSettingsService();
virtual ~PerAppSettingsService();
// Sets/gets the desktop that |app_id| was last launched from.
void SetDesktopLastLaunchedFrom(
const std::string& app_id, chrome::HostDesktopType host);
chrome::HostDesktopType GetDesktopLastLaunchedFrom(
const std::string& app_id) const;
bool HasDesktopLastLaunchedFrom(const std::string& app_id) const;
private:
typedef std::map<std::string, chrome::HostDesktopType> DesktopMap;
DesktopMap default_desktops_;
DISALLOW_COPY_AND_ASSIGN(PerAppSettingsService);
};
#endif // CHROME_BROWSER_APPS_PER_APP_SETTINGS_SERVICE_H_
// Copyright 2014 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/apps/per_app_settings_service_factory.h"
#include "base/memory/singleton.h"
#include "chrome/browser/apps/per_app_settings_service.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
// static
PerAppSettingsServiceFactory* PerAppSettingsServiceFactory::GetInstance() {
return Singleton<PerAppSettingsServiceFactory>::get();
}
// static
PerAppSettingsService* PerAppSettingsServiceFactory::GetForBrowserContext(
content::BrowserContext* browser_context) {
return static_cast<PerAppSettingsService*>(
GetInstance()->GetServiceForBrowserContext(
browser_context, true /* create */));
}
PerAppSettingsServiceFactory::PerAppSettingsServiceFactory()
: BrowserContextKeyedServiceFactory(
"PerAppSettingsServiceFactory",
BrowserContextDependencyManager::GetInstance()) {
}
PerAppSettingsServiceFactory::~PerAppSettingsServiceFactory() {}
BrowserContextKeyedService*
PerAppSettingsServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
return new PerAppSettingsService;
}
bool PerAppSettingsServiceFactory::ServiceIsCreatedWithBrowserContext() const {
return false;
}
content::BrowserContext* PerAppSettingsServiceFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return chrome::GetBrowserContextRedirectedInIncognito(context);
}
// Copyright 2014 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_APPS_PER_APP_SETTINGS_SERVICE_FACTORY_H_
#define CHROME_BROWSER_APPS_PER_APP_SETTINGS_SERVICE_FACTORY_H_
#include "components/browser_context_keyed_service/browser_context_keyed_service_factory.h"
namespace content {
class BrowserContext;
}
class PerAppSettingsService;
template <typename T> struct DefaultSingletonTraits;
class PerAppSettingsServiceFactory : public BrowserContextKeyedServiceFactory {
public:
static PerAppSettingsServiceFactory* GetInstance();
static PerAppSettingsService* GetForBrowserContext(
content::BrowserContext* browser_context);
private:
friend struct DefaultSingletonTraits<PerAppSettingsServiceFactory>;
PerAppSettingsServiceFactory();
virtual ~PerAppSettingsServiceFactory();
// BrowserContextKeyedServiceFactory:
virtual BrowserContextKeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const OVERRIDE;
virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE;
virtual content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const OVERRIDE;
};
#endif // CHROME_BROWSER_APPS_PER_APP_SETTINGS_SERVICE_FACTORY_H_
......@@ -12,6 +12,8 @@
#include "base/metrics/histogram.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/app_mode/app_mode_utils.h"
#include "chrome/browser/apps/per_app_settings_service.h"
#include "chrome/browser/apps/per_app_settings_service_factory.h"
#include "chrome/browser/extensions/extension_prefs.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
......@@ -344,6 +346,10 @@ WebContents* OpenEnabledApplication(const AppLaunchParams& params) {
"Extensions.AppLaunchContainer", params.container, 100);
if (extension->is_platform_app()) {
// Remember what desktop the launch happened on so that when the app opens a
// window we can open them on the right desktop.
PerAppSettingsServiceFactory::GetForBrowserContext(profile)->
SetDesktopLastLaunchedFrom(extension->id(), params.desktop_type);
#if !defined(OS_CHROMEOS)
SigninManager* signin_manager =
SigninManagerFactory::GetForProfile(profile);
......
......@@ -376,6 +376,9 @@ bool StartupBrowserCreatorImpl::Launch(Profile* profile,
extensions::LAUNCH_CONTAINER_NONE, NEW_WINDOW);
params.command_line = command_line_;
params.current_directory = cur_dir_;
// If we are being launched from the command line, default to native
// desktop.
params.desktop_type = chrome::HOST_DESKTOP_TYPE_NATIVE;
OpenApplicationWithReenablePrompt(params);
return true;
}
......
......@@ -7,6 +7,8 @@
#include "apps/shell_window.h"
#include "apps/shell_window_registry.h"
#include "ash/shell.h"
#include "chrome/browser/apps/per_app_settings_service.h"
#include "chrome/browser/apps/per_app_settings_service_factory.h"
#include "chrome/browser/metro_utils/metro_chrome_win.h"
#include "chrome/browser/profiles/profile.h"
#include "extensions/common/extension.h"
......@@ -45,11 +47,15 @@ void NativeAppWindowViewsWin::OnBeforeWidgetInit(
desktop_type = chrome::GetHostDesktopTypeForNativeWindow(
any_existing_window->GetNativeWindow());
} else {
// TODO(tapted): Ideally, when an OnLaunched event is dispatched from UI
// and immediately results in window creation, |desktop_type| here should
// match that UI. However, there is no guarantee that an app will create
// a window in its OnLaunched event handler, so linking these up is hard.
desktop_type = chrome::GetActiveDesktop();
PerAppSettingsService* settings =
PerAppSettingsServiceFactory::GetForBrowserContext(profile());
if (settings->HasDesktopLastLaunchedFrom(extension()->id())) {
desktop_type = settings->GetDesktopLastLaunchedFrom(extension()->id());
} else {
// We don't know what desktop this app was last launched from, so take our
// best guess as to what desktop the user is on.
desktop_type = chrome::GetActiveDesktop();
}
}
if (desktop_type == chrome::HOST_DESKTOP_TYPE_ASH)
init_params->context = ash::Shell::GetPrimaryRootWindow();
......
......@@ -78,6 +78,10 @@
'browser/apps/ephemeral_app_service_factory.h',
'browser/apps/ephemeral_app_throttle.cc',
'browser/apps/ephemeral_app_throttle.h',
'browser/apps/per_app_settings_service.cc',
'browser/apps/per_app_settings_service.h',
'browser/apps/per_app_settings_service_factory.cc',
'browser/apps/per_app_settings_service_factory.h',
'browser/apps/shortcut_manager.cc',
'browser/apps/shortcut_manager.h',
'browser/apps/shortcut_manager_factory.cc',
......
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