190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// found in the LICENSE file.
490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#ifndef CHROME_BROWSER_APPS_APP_SHIM_EXTENSION_APP_SHIM_HANDLER_MAC_H_
61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define CHROME_BROWSER_APPS_APP_SHIM_EXTENSION_APP_SHIM_HANDLER_MAC_H_
790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <map>
990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <string>
1058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include <vector>
1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
12eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "apps/app_lifetime_monitor.h"
13eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/memory/scoped_ptr.h"
147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "base/memory/weak_ptr.h"
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/apps/app_shim/app_shim_handler_mac.h"
1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/browser/notification_observer.h"
1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/browser/notification_registrar.h"
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/app_window/app_window_registry.h"
1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class Profile;
2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
22eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochnamespace base {
23eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass FilePath;
24eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
25eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
26eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochnamespace content {
27eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass WebContents;
28eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
29eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace extensions {
311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass AppWindow;
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class Extension;
33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace apps {
3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// This app shim handler that handles events for app shims that correspond to an
3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// extension.
3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class ExtensionAppShimHandler : public AppShimHandler,
40eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                public content::NotificationObserver,
41eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                public AppLifetimeMonitor::Observer {
4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public:
43eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  class Delegate {
44eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch   public:
45eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    virtual ~Delegate() {}
46eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    virtual bool ProfileExistsForPath(const base::FilePath& path);
48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    virtual Profile* ProfileForPath(const base::FilePath& path);
497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    virtual void LoadProfileAsync(const base::FilePath& path,
507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                  base::Callback<void(Profile*)> callback);
51eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    virtual extensions::AppWindowRegistry::AppWindowList GetWindows(
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        Profile* profile,
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        const std::string& extension_id);
55eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
56eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    virtual const extensions::Extension* GetAppExtension(
57eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        Profile* profile, const std::string& extension_id);
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    virtual void EnableExtension(Profile* profile,
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 const std::string& extension_id,
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 const base::Callback<void()>& callback);
61eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    virtual void LaunchApp(Profile* profile,
6258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                           const extensions::Extension* extension,
6358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                           const std::vector<base::FilePath>& files);
64eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    virtual void LaunchShim(Profile* profile,
65eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                            const extensions::Extension* extension);
66eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
67eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    virtual void MaybeTerminate();
68eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  };
69eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
7090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ExtensionAppShimHandler();
7190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual ~ExtensionAppShimHandler();
7290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
73a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  AppShimHandler::Host* FindHost(Profile* profile, const std::string& app_id);
74a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  static void QuitAppForWindow(extensions::AppWindow* app_window);
76a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  static void HideAppForWindow(extensions::AppWindow* app_window);
7858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  static void FocusAppForWindow(extensions::AppWindow* app_window);
80d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
81424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Brings the window to the front without showing it and instructs the shim to
8203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // request user attention. If there is no shim, show the app and return false.
831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  static bool ActivateAndRequestUserAttentionForWindow(
841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      extensions::AppWindow* app_window);
8503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
8603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // Instructs the shim to request user attention. Returns false if there is no
8703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // shim for this window.
8803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  static void RequestUserAttentionForWindow(
891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      extensions::AppWindow* app_window,
9003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      AppShimAttentionType attention_type);
91424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Called by AppControllerMac when Chrome hides.
935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  static void OnChromeWillHide();
945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
9590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // AppShimHandler overrides:
9658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual void OnShimLaunch(Host* host,
9758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                            AppShimLaunchType launch_type,
9858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                            const std::vector<base::FilePath>& files) OVERRIDE;
9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void OnShimClose(Host* host) OVERRIDE;
10058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual void OnShimFocus(Host* host,
10158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                           AppShimFocusType focus_type,
10258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                           const std::vector<base::FilePath>& files) OVERRIDE;
103eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual void OnShimSetHidden(Host* host, bool hidden) OVERRIDE;
104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void OnShimQuit(Host* host) OVERRIDE;
10590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
106eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // AppLifetimeMonitor::Observer overrides:
107eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual void OnAppStart(Profile* profile, const std::string& app_id) OVERRIDE;
108eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual void OnAppActivated(Profile* profile,
109eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                              const std::string& app_id) OVERRIDE;
110eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual void OnAppDeactivated(Profile* profile,
111eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                const std::string& app_id) OVERRIDE;
112eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual void OnAppStop(Profile* profile, const std::string& app_id) OVERRIDE;
113eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual void OnChromeTerminating() OVERRIDE;
114eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
115eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // content::NotificationObserver overrides:
116eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual void Observe(int type,
117eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                       const content::NotificationSource& source,
118eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                       const content::NotificationDetails& details) OVERRIDE;
119eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
12090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) protected:
12190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  typedef std::map<std::pair<Profile*, std::string>, AppShimHandler::Host*>
12290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      HostMap;
12390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
12490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Exposed for testing.
125eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  void set_delegate(Delegate* delegate);
12690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  HostMap& hosts() { return hosts_; }
12790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  content::NotificationRegistrar& registrar() { return registrar_; }
12890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
12990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) private:
1305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Helper function to get the instance on the browser process.
1315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  static ExtensionAppShimHandler* GetInstance();
1325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // This is passed to Delegate::LoadProfileAsync for shim-initiated launches
1347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // where the profile was not yet loaded.
1357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  void OnProfileLoaded(Host* host,
1367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                       AppShimLaunchType launch_type,
13758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                       const std::vector<base::FilePath>& files,
1387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                       Profile* profile);
1397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // This is passed to Delegate::EnableViaPrompt for shim-initiated launches
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // where the extension is disabled.
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void OnExtensionEnabled(const base::FilePath& profile_path,
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                          const std::string& app_id,
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                          const std::vector<base::FilePath>& files);
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
146eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  scoped_ptr<Delegate> delegate_;
14790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
14890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  HostMap hosts_;
14990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
15090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  content::NotificationRegistrar registrar_;
151eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  base::WeakPtrFactory<ExtensionAppShimHandler> weak_factory_;
1537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  DISALLOW_COPY_AND_ASSIGN(ExtensionAppShimHandler);
15590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)};
15690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
15790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}  // namespace apps
15890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif  // CHROME_BROWSER_APPS_APP_SHIM_EXTENSION_APP_SHIM_HANDLER_MAC_H_
160