/extensions/browser/app_window/app_window_geometry_cache.h

https://gitlab.com/0072016/Facebook-SDK- · C Header · 159 lines · 97 code · 36 blank · 26 comment · 0 complexity · 0fa580ea380d27ec57067f7e63668075 MD5 · raw file

  1. // Copyright 2014 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 EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_GEOMETRY_CACHE_H_
  5. #define EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_GEOMETRY_CACHE_H_
  6. #include <stddef.h>
  7. #include <map>
  8. #include <set>
  9. #include <string>
  10. #include "base/memory/scoped_ptr.h"
  11. #include "base/memory/singleton.h"
  12. #include "base/observer_list.h"
  13. #include "base/scoped_observer.h"
  14. #include "base/time/time.h"
  15. #include "base/timer/timer.h"
  16. #include "base/values.h"
  17. #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
  18. #include "components/keyed_service/core/keyed_service.h"
  19. #include "extensions/browser/extension_registry_observer.h"
  20. #include "ui/base/ui_base_types.h"
  21. #include "ui/gfx/geometry/rect.h"
  22. namespace extensions {
  23. class ExtensionPrefs;
  24. class ExtensionRegistry;
  25. // A cache for persisted geometry of app windows, both to not have to wait
  26. // for IO when creating a new window, and to not cause IO on every window
  27. // geometry change.
  28. class AppWindowGeometryCache : public KeyedService,
  29. public ExtensionRegistryObserver {
  30. public:
  31. class Factory : public BrowserContextKeyedServiceFactory {
  32. public:
  33. static AppWindowGeometryCache* GetForContext(
  34. content::BrowserContext* context,
  35. bool create);
  36. static Factory* GetInstance();
  37. private:
  38. friend struct base::DefaultSingletonTraits<Factory>;
  39. Factory();
  40. ~Factory() override;
  41. // BrowserContextKeyedServiceFactory
  42. KeyedService* BuildServiceInstanceFor(
  43. content::BrowserContext* context) const override;
  44. bool ServiceIsNULLWhileTesting() const override;
  45. content::BrowserContext* GetBrowserContextToUse(
  46. content::BrowserContext* context) const override;
  47. };
  48. class Observer {
  49. public:
  50. virtual void OnGeometryCacheChanged(const std::string& extension_id,
  51. const std::string& window_id,
  52. const gfx::Rect& bounds) = 0;
  53. protected:
  54. virtual ~Observer() {}
  55. };
  56. AppWindowGeometryCache(content::BrowserContext* context,
  57. ExtensionPrefs* prefs);
  58. ~AppWindowGeometryCache() override;
  59. // Returns the instance for the given browsing context.
  60. static AppWindowGeometryCache* Get(content::BrowserContext* context);
  61. // Save the geometry and state associated with |extension_id| and |window_id|.
  62. void SaveGeometry(const std::string& extension_id,
  63. const std::string& window_id,
  64. const gfx::Rect& bounds,
  65. const gfx::Rect& screen_bounds,
  66. ui::WindowShowState state);
  67. // Get any saved geometry and state associated with |extension_id| and
  68. // |window_id|. If saved data exists, sets |bounds|, |screen_bounds| and
  69. // |state| if not NULL and returns true.
  70. bool GetGeometry(const std::string& extension_id,
  71. const std::string& window_id,
  72. gfx::Rect* bounds,
  73. gfx::Rect* screen_bounds,
  74. ui::WindowShowState* state);
  75. // KeyedService
  76. void Shutdown() override;
  77. void AddObserver(Observer* observer);
  78. void RemoveObserver(Observer* observer);
  79. // Maximum number of windows we'll cache the geometry for per app.
  80. static const size_t kMaxCachedWindows = 100;
  81. protected:
  82. friend class AppWindowGeometryCacheTest;
  83. // For tests, this modifies the timeout delay for saving changes from calls
  84. // to SaveGeometry. (Note that even if this is set to 0, you still need to
  85. // run the message loop to see the results of any SyncToStorage call).
  86. void SetSyncDelayForTests(int timeout_ms);
  87. private:
  88. // Data stored for each window.
  89. struct WindowData {
  90. WindowData();
  91. ~WindowData();
  92. gfx::Rect bounds;
  93. gfx::Rect screen_bounds;
  94. ui::WindowShowState window_state;
  95. base::Time last_change;
  96. };
  97. // Data stored for each extension.
  98. typedef std::map<std::string, WindowData> ExtensionData;
  99. // ExtensionRegistryObserver implementation.
  100. void OnExtensionLoaded(content::BrowserContext* browser_context,
  101. const Extension* extension) override;
  102. void OnExtensionUnloaded(content::BrowserContext* browser_context,
  103. const Extension* extension,
  104. UnloadedExtensionInfo::Reason reason) override;
  105. void LoadGeometryFromStorage(const std::string& extension_id);
  106. void SyncToStorage();
  107. // Preferences storage.
  108. ExtensionPrefs* prefs_;
  109. // Cached data.
  110. std::map<std::string, ExtensionData> cache_;
  111. // Data that still needs saving.
  112. std::set<std::string> unsynced_extensions_;
  113. // The timer used to save the data.
  114. base::OneShotTimer sync_timer_;
  115. // The timeout value we'll use for |sync_timer_|.
  116. base::TimeDelta sync_delay_;
  117. // Listen to extension load, unloaded notifications.
  118. ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
  119. extension_registry_observer_;
  120. base::ObserverList<Observer> observers_;
  121. };
  122. } // namespace extensions
  123. #endif // EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_GEOMETRY_CACHE_H_