• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SHELL_WINDOW_REGISTRY_H_
6 #define APPS_SHELL_WINDOW_REGISTRY_H_
7 
8 #include <list>
9 
10 #include "base/callback.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/singleton.h"
13 #include "base/observer_list.h"
14 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
15 #include "components/browser_context_keyed_service/browser_context_keyed_service_factory.h"
16 #include "ui/gfx/native_widget_types.h"
17 
18 namespace content {
19 class BrowserContext;
20 class DevToolsAgentHost;
21 class RenderViewHost;
22 }
23 
24 namespace apps {
25 
26 class ShellWindow;
27 
28 // The ShellWindowRegistry tracks the ShellWindows for all platform apps for a
29 // particular browser context.
30 class ShellWindowRegistry : public BrowserContextKeyedService {
31  public:
32   class Observer {
33    public:
34     // Called just after a shell window was added.
35     virtual void OnShellWindowAdded(apps::ShellWindow* shell_window) = 0;
36     // Called when the window icon changes.
37     virtual void OnShellWindowIconChanged(apps::ShellWindow* shell_window) = 0;
38     // Called just after a shell window was removed.
39     virtual void OnShellWindowRemoved(apps::ShellWindow* shell_window) = 0;
40 
41    protected:
~Observer()42     virtual ~Observer() {}
43   };
44 
45   typedef std::list<apps::ShellWindow*> ShellWindowList;
46   typedef ShellWindowList::const_iterator const_iterator;
47   typedef std::set<std::string> InspectedWindowSet;
48 
49   explicit ShellWindowRegistry(content::BrowserContext* context);
50   virtual ~ShellWindowRegistry();
51 
52   // Returns the instance for the given browser context, or NULL if none. This
53   // is a convenience wrapper around
54   // ShellWindowRegistry::Factory::GetForBrowserContext().
55   static ShellWindowRegistry* Get(content::BrowserContext* context);
56 
57   void AddShellWindow(apps::ShellWindow* shell_window);
58   void ShellWindowIconChanged(apps::ShellWindow* shell_window);
59   // Called by |shell_window| when it is activated.
60   void ShellWindowActivated(apps::ShellWindow* shell_window);
61   void RemoveShellWindow(apps::ShellWindow* shell_window);
62 
63   void AddObserver(Observer* observer);
64   void RemoveObserver(Observer* observer);
65 
66   // Returns a set of windows owned by the application identified by app_id.
67   ShellWindowList GetShellWindowsForApp(const std::string& app_id) const;
shell_windows()68   const ShellWindowList& shell_windows() const { return shell_windows_; }
69 
70   // Close all shell windows associated with an app.
71   void CloseAllShellWindowsForApp(const std::string& app_id);
72 
73   // Helper functions to find shell windows with particular attributes.
74   apps::ShellWindow* GetShellWindowForRenderViewHost(
75       content::RenderViewHost* render_view_host) const;
76   apps::ShellWindow* GetShellWindowForNativeWindow(
77       gfx::NativeWindow window) const;
78   // Returns an app window for the given app, or NULL if no shell windows are
79   // open. If there is a window for the given app that is active, that one will
80   // be returned, otherwise an arbitrary window will be returned.
81   apps::ShellWindow* GetCurrentShellWindowForApp(
82       const std::string& app_id) const;
83   // Returns an app window for the given app and window key, or NULL if no shell
84   // window with the key are open. If there is a window for the given app and
85   // key that is active, that one will be returned, otherwise an arbitrary
86   // window will be returned.
87   apps::ShellWindow* GetShellWindowForAppAndKey(
88       const std::string& app_id,
89       const std::string& window_key) const;
90 
91   // Returns whether a ShellWindow's ID was last known to have a DevToolsAgent
92   // attached to it, which should be restored during a reload of a corresponding
93   // newly created |render_view_host|.
94   bool HadDevToolsAttached(content::RenderViewHost* render_view_host) const;
95 
96   // Returns the shell window for |window|, looking in all browser contexts.
97   static apps::ShellWindow* GetShellWindowForNativeWindowAnyProfile(
98       gfx::NativeWindow window);
99 
100   // Returns true if the number of shell windows registered across all browser
101   // contexts is non-zero. |window_type_mask| is a bitwise OR filter of
102   // ShellWindow::WindowType, or 0 for any window type.
103   static bool IsShellWindowRegisteredInAnyProfile(int window_type_mask);
104 
105   class Factory : public BrowserContextKeyedServiceFactory {
106    public:
107     static ShellWindowRegistry* GetForBrowserContext(
108         content::BrowserContext* context, bool create);
109 
110     static Factory* GetInstance();
111    private:
112     friend struct DefaultSingletonTraits<Factory>;
113 
114     Factory();
115     virtual ~Factory();
116 
117     // BrowserContextKeyedServiceFactory
118     virtual BrowserContextKeyedService* BuildServiceInstanceFor(
119         content::BrowserContext* context) const OVERRIDE;
120     virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE;
121     virtual bool ServiceIsNULLWhileTesting() const OVERRIDE;
122     virtual content::BrowserContext* GetBrowserContextToUse(
123         content::BrowserContext* context) const OVERRIDE;
124   };
125 
126  protected:
127   void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
128 
129  private:
130   // Ensures the specified |shell_window| is included in |shell_windows_|.
131   // Otherwise adds |shell_window| to the back of |shell_windows_|.
132   void AddShellWindowToList(apps::ShellWindow* shell_window);
133 
134   // Bring |shell_window| to the front of |shell_windows_|. If it is not in the
135   // list, add it first.
136   void BringToFront(apps::ShellWindow* shell_window);
137 
138   content::BrowserContext* context_;
139   ShellWindowList shell_windows_;
140   InspectedWindowSet inspected_windows_;
141   ObserverList<Observer> observers_;
142   base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
143 };
144 
145 }  // namespace extensions
146 
147 #endif  // APPS_SHELL_WINDOW_REGISTRY_H_
148