Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef APPS_APP_LOAD_SERVICE_H_ |
| 6 | #define APPS_APP_LOAD_SERVICE_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | |
Torne (Richard Coles) | 5e3f23d | 2013-06-11 16:24:11 +0100 | [diff] [blame] | 11 | #include "base/command_line.h" |
| 12 | #include "base/files/file_path.h" |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 13 | #include "components/keyed_service/core/keyed_service.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 14 | #include "content/public/browser/notification_observer.h" |
| 15 | #include "content/public/browser/notification_registrar.h" |
Primiano Tucci | 1320f92 | 2014-09-30 14:45:55 +0100 | [diff] [blame] | 16 | #include "extensions/browser/extension_registry_observer.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 17 | |
| 18 | class Profile; |
| 19 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 20 | namespace extensions { |
| 21 | struct UnloadedExtensionInfo; |
| 22 | } |
| 23 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 24 | namespace apps { |
| 25 | |
| 26 | // Monitors apps being reloaded and performs app specific actions (like launch |
| 27 | // or restart) on them. Also provides an interface to schedule these actions. |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 28 | class AppLoadService : public KeyedService, |
Primiano Tucci | 1320f92 | 2014-09-30 14:45:55 +0100 | [diff] [blame] | 29 | public content::NotificationObserver, |
| 30 | public extensions::ExtensionRegistryObserver { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 31 | public: |
Torne (Richard Coles) | 5e3f23d | 2013-06-11 16:24:11 +0100 | [diff] [blame] | 32 | enum PostReloadActionType { |
| 33 | LAUNCH, |
| 34 | RESTART, |
| 35 | LAUNCH_WITH_COMMAND_LINE, |
| 36 | }; |
| 37 | |
| 38 | struct PostReloadAction { |
| 39 | PostReloadAction(); |
| 40 | |
| 41 | PostReloadActionType action_type; |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 42 | base::CommandLine command_line; |
Torne (Richard Coles) | 5e3f23d | 2013-06-11 16:24:11 +0100 | [diff] [blame] | 43 | base::FilePath current_dir; |
| 44 | }; |
| 45 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 46 | explicit AppLoadService(Profile* profile); |
| 47 | virtual ~AppLoadService(); |
| 48 | |
| 49 | // Reload the application with the given id and then send it the OnRestarted |
| 50 | // event. |
| 51 | void RestartApplication(const std::string& extension_id); |
| 52 | |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 53 | // Reload the application with the given id if it is currently running. |
| 54 | void RestartApplicationIfRunning(const std::string& extension_id); |
| 55 | |
Torne (Richard Coles) | 5e3f23d | 2013-06-11 16:24:11 +0100 | [diff] [blame] | 56 | // Loads (or reloads) the app with |extension_path|, then launches it. Any |
| 57 | // command line parameters from |command_line| will be passed along via |
| 58 | // launch parameters. Returns true if loading the extension has begun |
| 59 | // successfully. |
| 60 | bool LoadAndLaunch(const base::FilePath& extension_path, |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 61 | const base::CommandLine& command_line, |
Torne (Richard Coles) | 5e3f23d | 2013-06-11 16:24:11 +0100 | [diff] [blame] | 62 | const base::FilePath& current_dir); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 63 | |
| 64 | static AppLoadService* Get(Profile* profile); |
| 65 | |
| 66 | private: |
| 67 | // content::NotificationObserver. |
| 68 | virtual void Observe(int type, |
| 69 | const content::NotificationSource& source, |
| 70 | const content::NotificationDetails& details) OVERRIDE; |
| 71 | |
Primiano Tucci | 1320f92 | 2014-09-30 14:45:55 +0100 | [diff] [blame] | 72 | // extensions::ExtensionRegistryObserver. |
| 73 | virtual void OnExtensionUnloaded( |
| 74 | content::BrowserContext* browser_context, |
| 75 | const extensions::Extension* extension, |
| 76 | extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE; |
| 77 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 78 | bool WasUnloadedForReload( |
Primiano Tucci | 1320f92 | 2014-09-30 14:45:55 +0100 | [diff] [blame] | 79 | const extensions::ExtensionId& extension_id, |
| 80 | const extensions::UnloadedExtensionInfo::Reason reason); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 81 | bool HasPostReloadAction(const std::string& extension_id); |
| 82 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 83 | // Map of extension id to reload action. Absence from the map implies |
| 84 | // no action. |
Torne (Richard Coles) | 5e3f23d | 2013-06-11 16:24:11 +0100 | [diff] [blame] | 85 | std::map<std::string, PostReloadAction> post_reload_actions_; |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 86 | content::NotificationRegistrar registrar_; |
| 87 | Profile* profile_; |
| 88 | |
| 89 | DISALLOW_COPY_AND_ASSIGN(AppLoadService); |
| 90 | }; |
| 91 | |
| 92 | } // namespace apps |
| 93 | |
| 94 | #endif // APPS_APP_LOAD_SERVICE_H_ |