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