/external/chromium_org/apps/shell_window_geometry_cache.h

https://gitlab.com/brian0218/rk3188_rk3066_r-box_android4.4.2_sdk · C Header · 141 lines · 85 code · 31 blank · 25 comment · 0 complexity · e7a409e4508b3650a38fa013dd65c344 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_GEOMETRY_CACHE_H_
  5. #define CHROME_BROWSER_EXTENSIONS_SHELL_WINDOW_GEOMETRY_CACHE_H_
  6. #include <map>
  7. #include <set>
  8. #include <string>
  9. #include "base/memory/scoped_ptr.h"
  10. #include "base/memory/singleton.h"
  11. #include "base/time/time.h"
  12. #include "base/timer/timer.h"
  13. #include "base/values.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 "content/public/browser/notification_observer.h"
  17. #include "content/public/browser/notification_registrar.h"
  18. #include "ui/base/ui_base_types.h"
  19. #include "ui/gfx/rect.h"
  20. class Profile;
  21. namespace extensions {
  22. class ExtensionPrefs;
  23. }
  24. namespace apps {
  25. // A cache for persisted geometry of shell 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 ShellWindowGeometryCache
  29. : public BrowserContextKeyedService,
  30. public content::NotificationObserver {
  31. public:
  32. class Factory : public BrowserContextKeyedServiceFactory {
  33. public:
  34. static ShellWindowGeometryCache* GetForContext(
  35. content::BrowserContext* context,
  36. bool create);
  37. static Factory* GetInstance();
  38. private:
  39. friend struct DefaultSingletonTraits<Factory>;
  40. Factory();
  41. virtual ~Factory();
  42. // BrowserContextKeyedServiceFactory
  43. virtual BrowserContextKeyedService* BuildServiceInstanceFor(
  44. content::BrowserContext* context) const OVERRIDE;
  45. virtual bool ServiceIsNULLWhileTesting() const OVERRIDE;
  46. virtual content::BrowserContext* GetBrowserContextToUse(
  47. content::BrowserContext* context) const OVERRIDE;
  48. };
  49. ShellWindowGeometryCache(Profile* profile,
  50. extensions::ExtensionPrefs* prefs);
  51. virtual ~ShellWindowGeometryCache();
  52. // Returns the instance for the given browsing context.
  53. static ShellWindowGeometryCache* Get(content::BrowserContext* context);
  54. // Save the geometry and state associated with |extension_id| and |window_id|.
  55. void SaveGeometry(const std::string& extension_id,
  56. const std::string& window_id,
  57. const gfx::Rect& bounds,
  58. const gfx::Rect& screen_bounds,
  59. ui::WindowShowState state);
  60. // Get any saved geometry and state associated with |extension_id| and
  61. // |window_id|. If saved data exists, sets |bounds|, |screen_bounds| and
  62. // |state| if not NULL and returns true.
  63. bool GetGeometry(const std::string& extension_id,
  64. const std::string& window_id,
  65. gfx::Rect* bounds,
  66. gfx::Rect* screen_bounds,
  67. ui::WindowShowState* state);
  68. // BrowserContextKeyedService
  69. virtual void Shutdown() OVERRIDE;
  70. // Maximum number of windows we'll cache the geometry for per app.
  71. static const size_t kMaxCachedWindows = 100;
  72. protected:
  73. friend class ShellWindowGeometryCacheTest;
  74. // For tests, this modifies the timeout delay for saving changes from calls
  75. // to SaveGeometry. (Note that even if this is set to 0, you still need to
  76. // run the message loop to see the results of any SyncToStorage call).
  77. void SetSyncDelayForTests(int timeout_ms);
  78. private:
  79. // Data stored for each window.
  80. struct WindowData {
  81. WindowData();
  82. ~WindowData();
  83. gfx::Rect bounds;
  84. gfx::Rect screen_bounds;
  85. ui::WindowShowState window_state;
  86. base::Time last_change;
  87. };
  88. // Data stored for each extension.
  89. typedef std::map<std::string, WindowData> ExtensionData;
  90. // content::NotificationObserver
  91. virtual void Observe(int type,
  92. const content::NotificationSource& source,
  93. const content::NotificationDetails& details) OVERRIDE;
  94. void LoadGeometryFromStorage(const std::string& extension_id);
  95. void OnExtensionUnloaded(const std::string& extension_id);
  96. void SyncToStorage();
  97. // Preferences storage.
  98. extensions::ExtensionPrefs* prefs_;
  99. // Cached data
  100. std::map<std::string, ExtensionData> cache_;
  101. // Data that still needs saving
  102. std::set<std::string> unsynced_extensions_;
  103. // The timer used to save the data
  104. base::OneShotTimer<ShellWindowGeometryCache> sync_timer_;
  105. // The timeout value we'll use for |sync_timer_|.
  106. base::TimeDelta sync_delay_;
  107. content::NotificationRegistrar registrar_;
  108. };
  109. } // namespace apps
  110. #endif // CHROME_BROWSER_EXTENSIONS_SHELL_WINDOW_GEOMETRY_CACHE_H_