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
11#include "base/command_line.h"
12#include "base/files/file_path.h"
13#include "components/browser_context_keyed_service/browser_context_keyed_service.h"
14#include "content/public/browser/notification_observer.h"
15#include "content/public/browser/notification_registrar.h"
16
17class Profile;
18
19namespace extensions {
20struct UnloadedExtensionInfo;
21}
22
23namespace apps {
24
25// Monitors apps being reloaded and performs app specific actions (like launch
26// or restart) on them. Also provides an interface to schedule these actions.
27class AppLoadService : public BrowserContextKeyedService,
28                       public content::NotificationObserver {
29 public:
30  enum PostReloadActionType {
31    LAUNCH,
32    RESTART,
33    LAUNCH_WITH_COMMAND_LINE,
34  };
35
36  struct PostReloadAction {
37    PostReloadAction();
38
39    PostReloadActionType action_type;
40    CommandLine command_line;
41    base::FilePath current_dir;
42  };
43
44  explicit AppLoadService(Profile* profile);
45  virtual ~AppLoadService();
46
47  // Reload the application with the given id and then send it the OnRestarted
48  // event.
49  void RestartApplication(const std::string& extension_id);
50
51  // Loads (or reloads) the app with |extension_path|, then launches it. Any
52  // command line parameters from |command_line| will be passed along via
53  // launch parameters. Returns true if loading the extension has begun
54  // successfully.
55  bool LoadAndLaunch(const base::FilePath& extension_path,
56                     const CommandLine& command_line,
57                     const base::FilePath& current_dir);
58
59  static AppLoadService* Get(Profile* profile);
60
61 private:
62  // content::NotificationObserver.
63  virtual void Observe(int type,
64                       const content::NotificationSource& source,
65                       const content::NotificationDetails& details) OVERRIDE;
66
67  bool HasShellWindows(const std::string& extension_id);
68  bool WasUnloadedForReload(
69      const extensions::UnloadedExtensionInfo& unload_info);
70  bool HasPostReloadAction(const std::string& extension_id);
71
72  // Map of extension id to reload action. Absence from the map implies
73  // no action.
74  std::map<std::string, PostReloadAction> post_reload_actions_;
75  content::NotificationRegistrar registrar_;
76  Profile* profile_;
77
78  DISALLOW_COPY_AND_ASSIGN(AppLoadService);
79};
80
81}  // namespace apps
82
83#endif  // APPS_APP_LOAD_SERVICE_H_
84