Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / apps / app_shim / extension_app_shim_handler_mac.h
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_SHIM_EXTENSION_APP_SHIM_HANDLER_MAC_H_
6 #define APPS_APP_SHIM_EXTENSION_APP_SHIM_HANDLER_MAC_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
12 #include "apps/app_lifetime_monitor.h"
13 #include "apps/app_shim/app_shim_handler_mac.h"
14 #include "apps/app_window_registry.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19
20 class Profile;
21
22 namespace base {
23 class FilePath;
24 }
25
26 namespace content {
27 class WebContents;
28 }
29
30 namespace extensions {
31 class Extension;
32 }
33
34 namespace apps {
35
36 class AppWindow;
37
38 // This app shim handler that handles events for app shims that correspond to an
39 // extension.
40 class ExtensionAppShimHandler : public AppShimHandler,
41                                 public content::NotificationObserver,
42                                 public AppLifetimeMonitor::Observer {
43  public:
44   class Delegate {
45    public:
46     virtual ~Delegate() {}
47
48     virtual bool ProfileExistsForPath(const base::FilePath& path);
49     virtual Profile* ProfileForPath(const base::FilePath& path);
50     virtual void LoadProfileAsync(const base::FilePath& path,
51                                   base::Callback<void(Profile*)> callback);
52
53     virtual AppWindowRegistry::AppWindowList GetWindows(
54         Profile* profile,
55         const std::string& extension_id);
56
57     virtual const extensions::Extension* GetAppExtension(
58         Profile* profile, const std::string& extension_id);
59     virtual void EnableExtension(Profile* profile,
60                                  const std::string& extension_id,
61                                  const base::Callback<void()>& callback);
62     virtual void LaunchApp(Profile* profile,
63                            const extensions::Extension* extension,
64                            const std::vector<base::FilePath>& files);
65     virtual void LaunchShim(Profile* profile,
66                             const extensions::Extension* extension);
67
68     virtual void MaybeTerminate();
69   };
70
71   ExtensionAppShimHandler();
72   virtual ~ExtensionAppShimHandler();
73
74   AppShimHandler::Host* FindHost(Profile* profile, const std::string& app_id);
75
76   static void QuitAppForWindow(AppWindow* app_window);
77
78   static void HideAppForWindow(AppWindow* app_window);
79
80   static void FocusAppForWindow(AppWindow* app_window);
81
82   // Brings the window to the front without showing it and instructs the shim to
83   // request user attention. Returns false if there is no shim for this window.
84   static bool RequestUserAttentionForWindow(AppWindow* app_window);
85
86   // Called by AppControllerMac when Chrome hides.
87   static void OnChromeWillHide();
88
89   // AppShimHandler overrides:
90   virtual void OnShimLaunch(Host* host,
91                             AppShimLaunchType launch_type,
92                             const std::vector<base::FilePath>& files) OVERRIDE;
93   virtual void OnShimClose(Host* host) OVERRIDE;
94   virtual void OnShimFocus(Host* host,
95                            AppShimFocusType focus_type,
96                            const std::vector<base::FilePath>& files) OVERRIDE;
97   virtual void OnShimSetHidden(Host* host, bool hidden) OVERRIDE;
98   virtual void OnShimQuit(Host* host) OVERRIDE;
99
100   // AppLifetimeMonitor::Observer overrides:
101   virtual void OnAppStart(Profile* profile, const std::string& app_id) OVERRIDE;
102   virtual void OnAppActivated(Profile* profile,
103                               const std::string& app_id) OVERRIDE;
104   virtual void OnAppDeactivated(Profile* profile,
105                                 const std::string& app_id) OVERRIDE;
106   virtual void OnAppStop(Profile* profile, const std::string& app_id) OVERRIDE;
107   virtual void OnChromeTerminating() OVERRIDE;
108
109   // content::NotificationObserver overrides:
110   virtual void Observe(int type,
111                        const content::NotificationSource& source,
112                        const content::NotificationDetails& details) OVERRIDE;
113
114  protected:
115   typedef std::map<std::pair<Profile*, std::string>, AppShimHandler::Host*>
116       HostMap;
117
118   // Exposed for testing.
119   void set_delegate(Delegate* delegate);
120   HostMap& hosts() { return hosts_; }
121   content::NotificationRegistrar& registrar() { return registrar_; }
122
123  private:
124   // Helper function to get the instance on the browser process.
125   static ExtensionAppShimHandler* GetInstance();
126
127   // This is passed to Delegate::LoadProfileAsync for shim-initiated launches
128   // where the profile was not yet loaded.
129   void OnProfileLoaded(Host* host,
130                        AppShimLaunchType launch_type,
131                        const std::vector<base::FilePath>& files,
132                        Profile* profile);
133
134   // This is passed to Delegate::EnableViaPrompt for shim-initiated launches
135   // where the extension is disabled.
136   void OnExtensionEnabled(const base::FilePath& profile_path,
137                           const std::string& app_id,
138                           const std::vector<base::FilePath>& files);
139
140   scoped_ptr<Delegate> delegate_;
141
142   HostMap hosts_;
143
144   content::NotificationRegistrar registrar_;
145
146   base::WeakPtrFactory<ExtensionAppShimHandler> weak_factory_;
147
148   DISALLOW_COPY_AND_ASSIGN(ExtensionAppShimHandler);
149 };
150
151 }  // namespace apps
152
153 #endif  // APPS_APP_SHIM_EXTENSION_APP_SHIM_HANDLER_MAC_H_