diff options
author | benwells@chromium.org <benwells@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-17 09:26:15 +0000 |
---|---|---|
committer | benwells@chromium.org <benwells@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-17 09:26:15 +0000 |
commit | 71e0c305e02ff19777b3cbd6972bf05e5347d81d (patch) | |
tree | 3b7184e370a17d7567fd76d4e8f3aa541ccbfaca /apps/shell_window_geometry_cache.h | |
parent | e3e4ca655aad6fac96081c8deca9070299ab90cf (diff) | |
download | chromium_src-71e0c305e02ff19777b3cbd6972bf05e5347d81d.zip chromium_src-71e0c305e02ff19777b3cbd6972bf05e5347d81d.tar.gz chromium_src-71e0c305e02ff19777b3cbd6972bf05e5347d81d.tar.bz2 |
Move ShellWindowGeometryCache into apps
BUG=159366
Review URL: https://chromiumcodereview.appspot.com/14636012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@200770 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'apps/shell_window_geometry_cache.h')
-rw-r--r-- | apps/shell_window_geometry_cache.h | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/apps/shell_window_geometry_cache.h b/apps/shell_window_geometry_cache.h new file mode 100644 index 0000000..5b5bb1d --- /dev/null +++ b/apps/shell_window_geometry_cache.h @@ -0,0 +1,137 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_EXTENSIONS_SHELL_WINDOW_GEOMETRY_CACHE_H_ +#define CHROME_BROWSER_EXTENSIONS_SHELL_WINDOW_GEOMETRY_CACHE_H_ + +#include <map> +#include <set> +#include <string> + +#include "base/memory/scoped_ptr.h" +#include "base/memory/singleton.h" +#include "base/time.h" +#include "base/timer.h" +#include "base/values.h" +#include "chrome/browser/profiles/profile_keyed_service.h" +#include "chrome/browser/profiles/profile_keyed_service_factory.h" +#include "content/public/browser/notification_observer.h" +#include "content/public/browser/notification_registrar.h" +#include "ui/base/ui_base_types.h" +#include "ui/gfx/rect.h" + +class Profile; + +namespace extensions { +class ExtensionPrefs; +} + +namespace apps { + +// A cache for persisted geometry of shell windows, both to not have to wait +// for IO when creating a new window, and to not cause IO on every window +// geometry change. +class ShellWindowGeometryCache + : public ProfileKeyedService, + public content::NotificationObserver { + public: + class Factory : public ProfileKeyedServiceFactory { + public: + static ShellWindowGeometryCache* GetForContext( + content::BrowserContext* context, + bool create); + + static Factory* GetInstance(); + private: + friend struct DefaultSingletonTraits<Factory>; + + Factory(); + virtual ~Factory(); + + // ProfileKeyedServiceFactory + virtual ProfileKeyedService* BuildServiceInstanceFor( + content::BrowserContext* context) const OVERRIDE; + virtual bool ServiceIsNULLWhileTesting() const OVERRIDE; + virtual content::BrowserContext* GetBrowserContextToUse( + content::BrowserContext* context) const OVERRIDE; + }; + + ShellWindowGeometryCache(Profile* profile, + extensions::ExtensionPrefs* prefs); + + virtual ~ShellWindowGeometryCache(); + + // Returns the instance for the given browsing context. + static ShellWindowGeometryCache* Get(content::BrowserContext* context); + + // Save the geometry and state associated with |extension_id| and |window_id|. + void SaveGeometry(const std::string& extension_id, + const std::string& window_id, + const gfx::Rect& bounds, + ui::WindowShowState state); + + // Get any saved geometry and state associated with |extension_id| and + // |window_id|. If saved data exists, sets |bounds| and |state| if not NULL + // and returns true. + bool GetGeometry(const std::string& extension_id, + const std::string& window_id, + gfx::Rect* bounds, + ui::WindowShowState* state) const; + + // ProfileKeyedService + virtual void Shutdown() OVERRIDE; + + // Maximum number of windows we'll cache the geometry for per app. + static const size_t kMaxCachedWindows = 100; + + protected: + friend class ShellWindowGeometryCacheTest; + + // For tests, this modifies the timeout delay for saving changes from calls + // to SaveGeometry. (Note that even if this is set to 0, you still need to + // run the message loop to see the results of any SyncToStorage call). + void SetSyncDelayForTests(int timeout_ms); + + private: + // Data stored for each window. + struct WindowData { + WindowData() : window_state(ui::SHOW_STATE_DEFAULT) {} + gfx::Rect bounds; + ui::WindowShowState window_state; + base::Time last_change; + }; + + // Data stored for each extension. + typedef std::map<std::string, WindowData> ExtensionData; + + // content::NotificationObserver + virtual void Observe(int type, + const content::NotificationSource& source, + const content::NotificationDetails& details) OVERRIDE; + + void OnExtensionLoaded(const std::string& extension_id); + void OnExtensionUnloaded(const std::string& extension_id); + void SyncToStorage(); + + // Preferences storage. + extensions::ExtensionPrefs* prefs_; + + // Cached data + std::map<std::string, ExtensionData> cache_; + + // Data that still needs saving + std::set<std::string> unsynced_extensions_; + + // The timer used to save the data + base::OneShotTimer<ShellWindowGeometryCache> sync_timer_; + + // The timeout value we'll use for |sync_timer_|. + base::TimeDelta sync_delay_; + + content::NotificationRegistrar registrar_; +}; + +} // namespace apps + +#endif // CHROME_BROWSER_EXTENSIONS_SHELL_WINDOW_GEOMETRY_CACHE_H_ |