summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorbenwells@chromium.org <benwells@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-01-22 02:23:29 +0000
committerbenwells@chromium.org <benwells@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-01-22 02:23:29 +0000
commitc2e2b6da588746d381e810a44538694ee6d8d36e (patch)
tree2f0676fbd0e504e2fac396834de34fac559816f8 /apps
parent31f82edf8844b5d471131603c2d0a53c3dec8a3d (diff)
downloadchromium_src-c2e2b6da588746d381e810a44538694ee6d8d36e.zip
chromium_src-c2e2b6da588746d381e810a44538694ee6d8d36e.tar.gz
chromium_src-c2e2b6da588746d381e810a44538694ee6d8d36e.tar.bz2
Create top level apps component and move some apps code there.
This is a small patch to begin the process of pulling apps code out of chrome and the extensions system. BUG=159366 Review URL: https://chromiumcodereview.appspot.com/11829055 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@177968 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'apps')
-rw-r--r--apps/DEPS10
-rw-r--r--apps/app_restore_service.cc103
-rw-r--r--apps/app_restore_service.h48
-rw-r--r--apps/app_restore_service_browsertest.cc55
-rw-r--r--apps/app_restore_service_factory.cc46
-rw-r--r--apps/app_restore_service_factory.h42
-rw-r--r--apps/apps.gypi39
7 files changed, 343 insertions, 0 deletions
diff --git a/apps/DEPS b/apps/DEPS
new file mode 100644
index 0000000..980e672
--- /dev/null
+++ b/apps/DEPS
@@ -0,0 +1,10 @@
+include_rules = [
+ "+base",
+ "+content",
+ # Temporary allowed includes.
+ # TODO(benwells): remove these.
+ "+chrome/browser/extensions",
+ "+chrome/browser/profiles",
+ "+chrome/common/chrome_notification_types.h",
+ "+chrome/common/extensions",
+]
diff --git a/apps/app_restore_service.cc b/apps/app_restore_service.cc
new file mode 100644
index 0000000..554f873
--- /dev/null
+++ b/apps/app_restore_service.cc
@@ -0,0 +1,103 @@
+// Copyright (c) 2012 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 "apps/app_restore_service.h"
+
+#include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
+#include "chrome/browser/extensions/event_router.h"
+#include "chrome/browser/extensions/extension_host.h"
+#include "chrome/browser/extensions/extension_service.h"
+#include "chrome/browser/extensions/extension_system.h"
+#include "chrome/common/chrome_notification_types.h"
+#include "chrome/common/extensions/extension.h"
+#include "chrome/common/extensions/extension_set.h"
+#include "content/public/browser/notification_details.h"
+#include "content/public/browser/notification_service.h"
+#include "content/public/browser/notification_types.h"
+
+using extensions::AppEventRouter;
+using extensions::Extension;
+using extensions::ExtensionHost;
+using extensions::ExtensionPrefs;
+using extensions::ExtensionSystem;
+
+namespace apps {
+
+AppRestoreService::AppRestoreService(Profile* profile)
+ : profile_(profile) {
+ registrar_.Add(
+ this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
+ content::NotificationService::AllSources());
+ registrar_.Add(
+ this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
+ content::NotificationService::AllSources());
+ registrar_.Add(
+ this, chrome::NOTIFICATION_APP_TERMINATING,
+ content::NotificationService::AllSources());
+}
+
+void AppRestoreService::HandleStartup(bool should_restore_apps) {
+ ExtensionService* extension_service =
+ ExtensionSystem::Get(profile_)->extension_service();
+ const ExtensionSet* extensions = extension_service->extensions();
+ ExtensionPrefs* extension_prefs = extension_service->extension_prefs();
+
+ for (ExtensionSet::const_iterator it = extensions->begin();
+ it != extensions->end(); ++it) {
+ const Extension* extension = *it;
+ if (extension_prefs->IsExtensionRunning(extension->id())) {
+ RecordAppStop(extension->id());
+ if (should_restore_apps)
+ RestoreApp(*it);
+ }
+ }
+}
+
+void AppRestoreService::Observe(int type,
+ const content::NotificationSource& source,
+ const content::NotificationDetails& details) {
+ switch (type) {
+ case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: {
+ ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
+ const Extension* extension = host->extension();
+ if (extension && extension->is_platform_app())
+ RecordAppStart(extension->id());
+ break;
+ }
+
+ case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
+ ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
+ const Extension* extension = host->extension();
+ if (extension && extension->is_platform_app())
+ RecordAppStop(extension->id());
+ break;
+ }
+
+ case chrome::NOTIFICATION_APP_TERMINATING: {
+ // Stop listening to NOTIFICATION_EXTENSION_HOST_DESTROYED in particular
+ // as all extension hosts will be destroyed as a result of shutdown.
+ registrar_.RemoveAll();
+ break;
+ }
+ }
+}
+
+
+void AppRestoreService::RecordAppStart(const std::string& extension_id) {
+ ExtensionPrefs* extension_prefs =
+ ExtensionSystem::Get(profile_)->extension_service()->extension_prefs();
+ extension_prefs->SetExtensionRunning(extension_id, true);
+}
+
+void AppRestoreService::RecordAppStop(const std::string& extension_id) {
+ ExtensionPrefs* extension_prefs =
+ ExtensionSystem::Get(profile_)->extension_service()->extension_prefs();
+ extension_prefs->SetExtensionRunning(extension_id, false);
+}
+
+void AppRestoreService::RestoreApp(const Extension* extension) {
+ AppEventRouter::DispatchOnRestartedEvent(profile_, extension);
+}
+
+} // namespace apps
diff --git a/apps/app_restore_service.h b/apps/app_restore_service.h
new file mode 100644
index 0000000..f919a7a
--- /dev/null
+++ b/apps/app_restore_service.h
@@ -0,0 +1,48 @@
+// Copyright (c) 2012 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_EXTENSIONS_APP_RESTORE_SERVICE_H_
+#define CHROME_BROWSER_EXTENSIONS_APP_RESTORE_SERVICE_H_
+
+#include <string>
+
+#include "chrome/browser/profiles/profile_keyed_service.h"
+#include "content/public/browser/notification_observer.h"
+#include "content/public/browser/notification_registrar.h"
+
+class Profile;
+
+namespace extensions {
+class Extension;
+}
+
+namespace apps {
+
+// Tracks what apps need to be restarted when the browser restarts.
+class AppRestoreService : public ProfileKeyedService,
+ public content::NotificationObserver {
+ public:
+ explicit AppRestoreService(Profile* profile);
+
+ // Restart apps that need to be restarted and clear the "running" preference
+ // from apps to prevent them being restarted in subsequent restarts.
+ void HandleStartup(bool should_restore_apps);
+
+ private:
+ // content::NotificationObserver.
+ virtual void Observe(int type,
+ const content::NotificationSource& source,
+ const content::NotificationDetails& details) OVERRIDE;
+
+ void RecordAppStart(const std::string& extension_id);
+ void RecordAppStop(const std::string& extension_id);
+ void RestoreApp(const extensions::Extension* extension);
+
+ content::NotificationRegistrar registrar_;
+ Profile* profile_;
+};
+
+} // namespace apps
+
+#endif // CHROME_BROWSER_EXTENSIONS_APP_RESTORE_SERVICE_H_
diff --git a/apps/app_restore_service_browsertest.cc b/apps/app_restore_service_browsertest.cc
new file mode 100644
index 0000000..094812d
--- /dev/null
+++ b/apps/app_restore_service_browsertest.cc
@@ -0,0 +1,55 @@
+// Copyright (c) 2013 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 "apps/app_restore_service.h"
+#include "apps/app_restore_service_factory.h"
+#include "chrome/browser/extensions/extension_prefs.h"
+#include "chrome/browser/extensions/extension_service.h"
+#include "chrome/browser/extensions/extension_system.h"
+#include "chrome/browser/extensions/extension_test_message_listener.h"
+#include "chrome/browser/extensions/platform_app_browsertest_util.h"
+#include "chrome/common/extensions/extension.h"
+#include "content/public/test/test_utils.h"
+
+using extensions::Extension;
+using extensions::ExtensionPrefs;
+using extensions::ExtensionSystem;
+// TODO(benwells): Move PlatformAppBrowserTest to apps namespace in apps
+// component.
+using extensions::PlatformAppBrowserTest;
+
+namespace apps {
+
+// Tests that a running app is recorded in the preferences as such.
+IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, RunningAppsAreRecorded) {
+ content::WindowedNotificationObserver extension_suspended(
+ chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
+ content::NotificationService::AllSources());
+
+ const Extension* extension = LoadExtension(
+ test_data_dir_.AppendASCII("platform_apps/restart_test"));
+ ASSERT_TRUE(extension);
+ ExtensionService* extension_service =
+ ExtensionSystem::Get(browser()->profile())->extension_service();
+ ExtensionPrefs* extension_prefs = extension_service->extension_prefs();
+
+ // App is running.
+ ASSERT_TRUE(extension_prefs->IsExtensionRunning(extension->id()));
+
+ // Wait for the extension to get suspended.
+ extension_suspended.Wait();
+
+ // App isn't running because it got suspended.
+ ASSERT_FALSE(extension_prefs->IsExtensionRunning(extension->id()));
+
+ // Pretend that the app is supposed to be running.
+ extension_prefs->SetExtensionRunning(extension->id(), true);
+
+ ExtensionTestMessageListener restart_listener("onRestarted", false);
+ apps::AppRestoreServiceFactory::GetForProfile(browser()->profile())->
+ HandleStartup(true);
+ restart_listener.WaitUntilSatisfied();
+}
+
+} // namespace apps
diff --git a/apps/app_restore_service_factory.cc b/apps/app_restore_service_factory.cc
new file mode 100644
index 0000000..22ebe32
--- /dev/null
+++ b/apps/app_restore_service_factory.cc
@@ -0,0 +1,46 @@
+// Copyright (c) 2012 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 "apps/app_restore_service_factory.h"
+
+#include "apps/app_restore_service.h"
+#include "chrome/browser/profiles/profile_dependency_manager.h"
+
+namespace apps {
+
+// static
+AppRestoreService* AppRestoreServiceFactory::GetForProfile(Profile* profile) {
+ return static_cast<AppRestoreService*>(
+ GetInstance()->GetServiceForProfile(profile, true));
+}
+
+// static
+void AppRestoreServiceFactory::ResetForProfile(Profile* profile) {
+ AppRestoreServiceFactory* factory = GetInstance();
+ factory->ProfileShutdown(profile);
+ factory->ProfileDestroyed(profile);
+}
+
+AppRestoreServiceFactory* AppRestoreServiceFactory::GetInstance() {
+ return Singleton<AppRestoreServiceFactory>::get();
+}
+
+AppRestoreServiceFactory::AppRestoreServiceFactory()
+ : ProfileKeyedServiceFactory("AppRestoreService",
+ ProfileDependencyManager::GetInstance()) {
+}
+
+AppRestoreServiceFactory::~AppRestoreServiceFactory() {
+}
+
+ProfileKeyedService* AppRestoreServiceFactory::BuildServiceInstanceFor(
+ Profile* profile) const {
+ return new AppRestoreService(profile);
+}
+
+bool AppRestoreServiceFactory::ServiceIsCreatedWithProfile() const {
+ return true;
+}
+
+} // namespace apps
diff --git a/apps/app_restore_service_factory.h b/apps/app_restore_service_factory.h
new file mode 100644
index 0000000..cf17ff2
--- /dev/null
+++ b/apps/app_restore_service_factory.h
@@ -0,0 +1,42 @@
+// Copyright (c) 2012 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_EXTENSIONS_APP_RESTORE_SERVICE_FACTORY_H_
+#define CHROME_BROWSER_EXTENSIONS_APP_RESTORE_SERVICE_FACTORY_H_
+
+#include "base/memory/singleton.h"
+#include "chrome/browser/profiles/profile_keyed_service_factory.h"
+
+class Profile;
+
+namespace apps {
+
+class AppRestoreService;
+
+// Singleton that owns all AppRestoreServices and associates them with
+// Profiles. Listens for the Profile's destruction notification and cleans up
+// the associated AppRestoreService.
+class AppRestoreServiceFactory : public ProfileKeyedServiceFactory {
+ public:
+ static AppRestoreService* GetForProfile(Profile* profile);
+
+ static void ResetForProfile(Profile* profile);
+
+ static AppRestoreServiceFactory* GetInstance();
+
+ private:
+ friend struct DefaultSingletonTraits<AppRestoreServiceFactory>;
+
+ AppRestoreServiceFactory();
+ virtual ~AppRestoreServiceFactory();
+
+ // ProfileKeyedServiceFactory:
+ virtual ProfileKeyedService* BuildServiceInstanceFor(
+ Profile* profile) const OVERRIDE;
+ virtual bool ServiceIsCreatedWithProfile() const OVERRIDE;
+};
+
+} // namespace apps
+
+#endif // CHROME_BROWSER_EXTENSIONS_APP_RESTORE_SERVICE_FACTORY_H_
diff --git a/apps/apps.gypi b/apps/apps.gypi
new file mode 100644
index 0000000..3cb7813
--- /dev/null
+++ b/apps/apps.gypi
@@ -0,0 +1,39 @@
+# Copyright (c) 2013 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.
+
+{
+ 'targets': [
+ {
+ 'target_name': 'apps',
+ 'type': 'static_library',
+ 'variables': { 'enable_wexit_time_destructors': 1, },
+ # Since browser and browser_extensions actually depend on each other,
+ # we must omit the dependency from browser_extensions to browser.
+ # However, this means browser_extensions and browser should more or less
+ # have the same dependencies. Once browser_extensions is untangled from
+ # browser, then we can clean up these dependencies.
+ 'dependencies': [
+ 'browser_extensions',
+ 'common/extensions/api/api.gyp:api',
+ '../skia/skia.gyp:skia',
+ ],
+ 'include_dirs': [
+ '<(INTERMEDIATE_DIR)',
+ ],
+ 'sources': [
+ 'app_restore_service.cc',
+ 'app_restore_service.h',
+ 'app_restore_service_factory.cc',
+ 'app_restore_service_factory.h',
+ ],
+ 'conditions': [
+ ['enable_extensions==0', {
+ 'sources/': [
+ ['exclude', '^apps/'],
+ ],
+ }],
+ ],
+ },
+ ],
+}