Make castv2 performance test work.
[chromium-blink-merge.git] / chrome / browser / apps / app_shim / extension_app_shim_handler_mac.h
blob32cebc13aba3741b5b655e3d7ada31ccc3fff181
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.
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_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "apps/app_lifetime_monitor.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "chrome/browser/apps/app_shim/app_shim_handler_mac.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_list_observer.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
21 #include "extensions/browser/app_window/app_window_registry.h"
23 class Profile;
25 namespace base {
26 class FilePath;
29 namespace content {
30 class WebContents;
33 namespace extensions {
34 class AppWindow;
35 class Extension;
38 namespace apps {
40 // This app shim handler that handles events for app shims that correspond to an
41 // extension.
42 class ExtensionAppShimHandler : public AppShimHandler,
43 public content::NotificationObserver,
44 public AppLifetimeMonitor::Observer,
45 public chrome::BrowserListObserver {
46 public:
47 class Delegate {
48 public:
49 virtual ~Delegate() {}
51 virtual bool ProfileExistsForPath(const base::FilePath& path);
52 virtual Profile* ProfileForPath(const base::FilePath& path);
53 virtual void LoadProfileAsync(const base::FilePath& path,
54 base::Callback<void(Profile*)> callback);
56 virtual extensions::AppWindowRegistry::AppWindowList GetWindows(
57 Profile* profile,
58 const std::string& extension_id);
60 virtual const extensions::Extension* GetAppExtension(
61 Profile* profile, const std::string& extension_id);
62 virtual void EnableExtension(Profile* profile,
63 const std::string& extension_id,
64 const base::Callback<void()>& callback);
65 virtual void LaunchApp(Profile* profile,
66 const extensions::Extension* extension,
67 const std::vector<base::FilePath>& files);
68 virtual void LaunchShim(Profile* profile,
69 const extensions::Extension* extension);
71 virtual void MaybeTerminate();
74 ExtensionAppShimHandler();
75 ~ExtensionAppShimHandler() override;
77 AppShimHandler::Host* FindHost(Profile* profile, const std::string& app_id);
79 void SetHostedAppHidden(Profile* profile,
80 const std::string& app_id,
81 bool hidden);
83 static const extensions::Extension* GetAppExtension(
84 Profile* profile,
85 const std::string& extension_id);
87 static const extensions::Extension* GetAppForBrowser(Browser* browser);
89 static void QuitAppForWindow(extensions::AppWindow* app_window);
91 static void QuitHostedAppForWindow(Profile* profile,
92 const std::string& app_id);
94 static void HideAppForWindow(extensions::AppWindow* app_window);
96 static void HideHostedApp(Profile* profile, const std::string& app_id);
98 static void FocusAppForWindow(extensions::AppWindow* app_window);
100 // Brings the window to the front without showing it and instructs the shim to
101 // request user attention. If there is no shim, show the app and return false.
102 static bool ActivateAndRequestUserAttentionForWindow(
103 extensions::AppWindow* app_window);
105 // Instructs the shim to request user attention. Returns false if there is no
106 // shim for this window.
107 static void RequestUserAttentionForWindow(
108 extensions::AppWindow* app_window,
109 AppShimAttentionType attention_type);
111 // Called by AppControllerMac when Chrome hides.
112 static void OnChromeWillHide();
114 // AppShimHandler overrides:
115 void OnShimLaunch(Host* host,
116 AppShimLaunchType launch_type,
117 const std::vector<base::FilePath>& files) override;
118 void OnShimClose(Host* host) override;
119 void OnShimFocus(Host* host,
120 AppShimFocusType focus_type,
121 const std::vector<base::FilePath>& files) override;
122 void OnShimSetHidden(Host* host, bool hidden) override;
123 void OnShimQuit(Host* host) override;
125 // AppLifetimeMonitor::Observer overrides:
126 void OnAppStart(Profile* profile, const std::string& app_id) override;
127 void OnAppActivated(Profile* profile, const std::string& app_id) override;
128 void OnAppDeactivated(Profile* profile, const std::string& app_id) override;
129 void OnAppStop(Profile* profile, const std::string& app_id) override;
130 void OnChromeTerminating() override;
132 // content::NotificationObserver overrides:
133 void Observe(int type,
134 const content::NotificationSource& source,
135 const content::NotificationDetails& details) override;
137 // chrome::BrowserListObserver overrides;
138 void OnBrowserAdded(Browser* browser) override;
139 void OnBrowserRemoved(Browser* browser) override;
141 protected:
142 typedef std::map<std::pair<Profile*, std::string>, AppShimHandler::Host*>
143 HostMap;
144 typedef std::set<Browser*> BrowserSet;
145 typedef std::map<std::string, BrowserSet> AppBrowserMap;
147 // Exposed for testing.
148 void set_delegate(Delegate* delegate);
149 HostMap& hosts() { return hosts_; }
150 content::NotificationRegistrar& registrar() { return registrar_; }
152 private:
153 // Helper function to get the instance on the browser process.
154 static ExtensionAppShimHandler* GetInstance();
156 // Closes all browsers associated with an app.
157 void CloseBrowsersForApp(const std::string& app_id);
159 // This is passed to Delegate::LoadProfileAsync for shim-initiated launches
160 // where the profile was not yet loaded.
161 void OnProfileLoaded(Host* host,
162 AppShimLaunchType launch_type,
163 const std::vector<base::FilePath>& files,
164 Profile* profile);
166 // This is passed to Delegate::EnableViaPrompt for shim-initiated launches
167 // where the extension is disabled.
168 void OnExtensionEnabled(const base::FilePath& profile_path,
169 const std::string& app_id,
170 const std::vector<base::FilePath>& files);
172 scoped_ptr<Delegate> delegate_;
174 HostMap hosts_;
176 // A map of app ids to associated browser windows.
177 AppBrowserMap app_browser_windows_;
179 content::NotificationRegistrar registrar_;
181 base::WeakPtrFactory<ExtensionAppShimHandler> weak_factory_;
183 DISALLOW_COPY_AND_ASSIGN(ExtensionAppShimHandler);
186 } // namespace apps
188 #endif // CHROME_BROWSER_APPS_APP_SHIM_EXTENSION_APP_SHIM_HANDLER_MAC_H_