/chromium-webcl/src/chrome/browser/extensions/shell_window_registry.h

https://bitbucket.org/peixuan/chromium_r197479_base · C Header · 132 lines · 74 code · 26 blank · 32 comment · 0 complexity · 46e290b0c0bffefcf5954833a134f549 MD5 · raw file

  1. // Copyright (c) 2012 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. #ifndef CHROME_BROWSER_EXTENSIONS_SHELL_WINDOW_REGISTRY_H_
  5. #define CHROME_BROWSER_EXTENSIONS_SHELL_WINDOW_REGISTRY_H_
  6. #include <set>
  7. #include "base/callback.h"
  8. #include "base/compiler_specific.h"
  9. #include "base/memory/singleton.h"
  10. #include "base/observer_list.h"
  11. #include "chrome/browser/profiles/profile_keyed_service.h"
  12. #include "chrome/browser/profiles/profile_keyed_service_factory.h"
  13. #include "ui/gfx/native_widget_types.h"
  14. class Profile;
  15. class ShellWindow;
  16. namespace content {
  17. class DevToolsAgentHost;
  18. class RenderViewHost;
  19. }
  20. namespace extensions {
  21. // The ShellWindowRegistry tracks the ShellWindows for all platform apps for a
  22. // particular profile.
  23. // This class is planned to evolve into tracking all PlatformApps for a
  24. // particular profile, with a PlatformApp encapsulating all views (background
  25. // page, shell windows, tray view, panels etc.) and other app level behaviour
  26. // (e.g. notifications the app is interested in, lifetime of the background
  27. // page).
  28. class ShellWindowRegistry : public ProfileKeyedService {
  29. public:
  30. class Observer {
  31. public:
  32. // Called just after a shell window was added.
  33. virtual void OnShellWindowAdded(ShellWindow* shell_window) = 0;
  34. // Called when the window icon changes.
  35. virtual void OnShellWindowIconChanged(ShellWindow* shell_window) = 0;
  36. // Called just after a shell window was removed.
  37. virtual void OnShellWindowRemoved(ShellWindow* shell_window) = 0;
  38. protected:
  39. virtual ~Observer() {}
  40. };
  41. typedef std::set<ShellWindow*> ShellWindowSet;
  42. typedef ShellWindowSet::const_iterator const_iterator;
  43. typedef std::set<std::string> InspectedWindowSet;
  44. explicit ShellWindowRegistry(Profile* profile);
  45. virtual ~ShellWindowRegistry();
  46. // Returns the instance for the given profile, or NULL if none. This is
  47. // a convenience wrapper around ShellWindowRegistry::Factory::GetForProfile.
  48. static ShellWindowRegistry* Get(Profile* profile);
  49. void AddShellWindow(ShellWindow* shell_window);
  50. void ShellWindowIconChanged(ShellWindow* shell_window);
  51. void RemoveShellWindow(ShellWindow* shell_window);
  52. void AddObserver(Observer* observer);
  53. void RemoveObserver(Observer* observer);
  54. // Returns a set of windows owned by the application identified by app_id.
  55. ShellWindowSet GetShellWindowsForApp(const std::string& app_id) const;
  56. const ShellWindowSet& shell_windows() const { return shell_windows_; }
  57. // Helper functions to find shell windows with particular attributes.
  58. ShellWindow* GetShellWindowForRenderViewHost(
  59. content::RenderViewHost* render_view_host) const;
  60. ShellWindow* GetShellWindowForNativeWindow(gfx::NativeWindow window) const;
  61. // Returns an app window for the given app, or NULL if no shell windows are
  62. // open. If there is a window for the given app that is active, that one will
  63. // be returned, otherwise an arbitrary window will be returned.
  64. ShellWindow* GetCurrentShellWindowForApp(const std::string& app_id) const;
  65. // Returns an app window for the given app and window key, or NULL if no shell
  66. // window with the key are open. If there is a window for the given app and
  67. // key that is active, that one will be returned, otherwise an arbitrary
  68. // window will be returned.
  69. ShellWindow* GetShellWindowForAppAndKey(const std::string& app_id,
  70. const std::string& window_key) const;
  71. // Returns whether a ShellWindow's ID was last known to have a DevToolsAgent
  72. // attached to it, which should be restored during a reload of a corresponding
  73. // newly created |render_view_host|.
  74. bool HadDevToolsAttached(content::RenderViewHost* render_view_host) const;
  75. // Returns the shell window for |window|, looking in all profiles.
  76. static ShellWindow* GetShellWindowForNativeWindowAnyProfile(
  77. gfx::NativeWindow window);
  78. // Returns true if the number of shell windows registered across all profiles
  79. // is non-zero. |window_type_mask| is a bitwise OR filter of
  80. // ShellWindow::WindowType, or 0 for any window type.
  81. static bool IsShellWindowRegisteredInAnyProfile(int window_type_mask);
  82. protected:
  83. void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
  84. private:
  85. class Factory : public ProfileKeyedServiceFactory {
  86. public:
  87. static ShellWindowRegistry* GetForProfile(Profile* profile, bool create);
  88. static Factory* GetInstance();
  89. private:
  90. friend struct DefaultSingletonTraits<Factory>;
  91. Factory();
  92. virtual ~Factory();
  93. // ProfileKeyedServiceFactory
  94. virtual ProfileKeyedService* BuildServiceInstanceFor(
  95. content::BrowserContext* profile) const OVERRIDE;
  96. virtual bool ServiceIsCreatedWithProfile() const OVERRIDE;
  97. virtual bool ServiceIsNULLWhileTesting() const OVERRIDE;
  98. };
  99. Profile* profile_;
  100. ShellWindowSet shell_windows_;
  101. InspectedWindowSet inspected_windows_;
  102. ObserverList<Observer> observers_;
  103. base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
  104. };
  105. } // namespace extensions
  106. #endif // CHROME_BROWSER_EXTENSIONS_SHELL_WINDOW_REGISTRY_H_