summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorbenwells@chromium.org <benwells@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-05-17 09:26:15 +0000
committerbenwells@chromium.org <benwells@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-05-17 09:26:15 +0000
commit71e0c305e02ff19777b3cbd6972bf05e5347d81d (patch)
tree3b7184e370a17d7567fd76d4e8f3aa541ccbfaca /apps
parente3e4ca655aad6fac96081c8deca9070299ab90cf (diff)
downloadchromium_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')
-rw-r--r--apps/apps.gypi2
-rw-r--r--apps/shell_window_geometry_cache.cc275
-rw-r--r--apps/shell_window_geometry_cache.h137
-rw-r--r--apps/shell_window_geometry_cache_unittest.cc256
4 files changed, 670 insertions, 0 deletions
diff --git a/apps/apps.gypi b/apps/apps.gypi
index 4867f73..6ba1011 100644
--- a/apps/apps.gypi
+++ b/apps/apps.gypi
@@ -42,6 +42,8 @@
'pref_names.h',
'prefs.cc',
'prefs.h',
+ 'shell_window_geometry_cache.cc',
+ 'shell_window_geometry_cache.h',
'shortcut_manager.cc',
'shortcut_manager.h',
'shortcut_manager_factory.cc',
diff --git a/apps/shell_window_geometry_cache.cc b/apps/shell_window_geometry_cache.cc
new file mode 100644
index 0000000..8a785b8
--- /dev/null
+++ b/apps/shell_window_geometry_cache.cc
@@ -0,0 +1,275 @@
+// 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.
+
+#include "apps/shell_window_geometry_cache.h"
+
+#include "base/bind.h"
+#include "base/stl_util.h"
+#include "base/strings/string_number_conversions.h"
+#include "chrome/browser/extensions/extension_prefs.h"
+#include "chrome/browser/extensions/extension_prefs_factory.h"
+#include "chrome/browser/profiles/incognito_helpers.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/profiles/profile_dependency_manager.h"
+#include "chrome/common/chrome_notification_types.h"
+#include "chrome/common/extensions/extension.h"
+#include "content/public/browser/notification_service.h"
+#include "content/public/browser/notification_types.h"
+
+namespace {
+
+// The timeout in milliseconds before we'll persist window geometry to the
+// StateStore.
+const int kSyncTimeoutMilliseconds = 1000;
+
+} // namespace
+
+namespace apps {
+
+ShellWindowGeometryCache::ShellWindowGeometryCache(
+ Profile* profile, extensions::ExtensionPrefs* prefs)
+ : prefs_(prefs),
+ sync_delay_(base::TimeDelta::FromMilliseconds(kSyncTimeoutMilliseconds)) {
+ registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
+ content::Source<Profile>(profile));
+ registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
+ content::Source<Profile>(profile));
+}
+
+ShellWindowGeometryCache::~ShellWindowGeometryCache() {
+}
+
+// static
+ShellWindowGeometryCache* ShellWindowGeometryCache::Get(
+ content::BrowserContext* context) {
+ return Factory::GetForContext(context, true /* create */);
+}
+
+void ShellWindowGeometryCache::SaveGeometry(
+ const std::string& extension_id,
+ const std::string& window_id,
+ const gfx::Rect& bounds,
+ ui::WindowShowState window_state) {
+ ExtensionData& extension_data = cache_[extension_id];
+
+ // If we don't have any unsynced changes and this is a duplicate of what's
+ // already in the cache, just ignore it.
+ if (extension_data[window_id].bounds == bounds &&
+ extension_data[window_id].window_state == window_state &&
+ !ContainsKey(unsynced_extensions_, extension_id))
+ return;
+
+ base::Time now = base::Time::Now();
+
+ extension_data[window_id].bounds = bounds;
+ extension_data[window_id].window_state = window_state;
+ extension_data[window_id].last_change = now;
+
+ if (extension_data.size() > kMaxCachedWindows) {
+ ExtensionData::iterator oldest = extension_data.end();
+ // Too many windows in the cache, find the oldest one to remove.
+ for (ExtensionData::iterator it = extension_data.begin();
+ it != extension_data.end(); ++it) {
+ // Don't expunge the window that was just added.
+ if (it->first == window_id) continue;
+
+ // If time is in the future, reset it to now to minimize weirdness.
+ if (it->second.last_change > now)
+ it->second.last_change = now;
+
+ if (oldest == extension_data.end() ||
+ it->second.last_change < oldest->second.last_change)
+ oldest = it;
+ }
+ extension_data.erase(oldest);
+ }
+
+ unsynced_extensions_.insert(extension_id);
+
+ // We don't use Reset() because the timer may not yet be running.
+ // (In that case Stop() is a no-op.)
+ sync_timer_.Stop();
+ sync_timer_.Start(FROM_HERE, sync_delay_, this,
+ &ShellWindowGeometryCache::SyncToStorage);
+}
+
+void ShellWindowGeometryCache::SyncToStorage() {
+ std::set<std::string> tosync;
+ tosync.swap(unsynced_extensions_);
+ for (std::set<std::string>::const_iterator it = tosync.begin(),
+ eit = tosync.end(); it != eit; ++it) {
+ const std::string& extension_id = *it;
+ const ExtensionData& extension_data = cache_[extension_id];
+
+ scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
+ for (ExtensionData::const_iterator it = extension_data.begin(),
+ eit = extension_data.end(); it != eit; ++it) {
+ base::DictionaryValue* value = new base::DictionaryValue;
+ const gfx::Rect& bounds = it->second.bounds;
+ value->SetInteger("x", bounds.x());
+ value->SetInteger("y", bounds.y());
+ value->SetInteger("w", bounds.width());
+ value->SetInteger("h", bounds.height());
+ value->SetInteger("state", it->second.window_state);
+ value->SetString(
+ "ts", base::Int64ToString(it->second.last_change.ToInternalValue()));
+ dict->SetWithoutPathExpansion(it->first, value);
+ }
+ prefs_->SetGeometryCache(extension_id, dict.Pass());
+ }
+}
+
+bool ShellWindowGeometryCache::GetGeometry(
+ const std::string& extension_id,
+ const std::string& window_id,
+ gfx::Rect* bounds,
+ ui::WindowShowState* window_state) const {
+
+ std::map<std::string, ExtensionData>::const_iterator
+ extension_data_it = cache_.find(extension_id);
+
+ // Not in the map means loading data for the extension didn't finish yet.
+ if (extension_data_it == cache_.end())
+ return false;
+
+ ExtensionData::const_iterator window_data = extension_data_it->second.find(
+ window_id);
+
+ if (window_data == extension_data_it->second.end())
+ return false;
+
+ if (bounds)
+ *bounds = window_data->second.bounds;
+ if (window_state)
+ *window_state = window_data->second.window_state;
+ return true;
+}
+
+void ShellWindowGeometryCache::Shutdown() {
+ SyncToStorage();
+}
+
+void ShellWindowGeometryCache::Observe(
+ int type, const content::NotificationSource& source,
+ const content::NotificationDetails& details) {
+ switch (type) {
+ case chrome::NOTIFICATION_EXTENSION_LOADED: {
+ std::string extension_id =
+ content::Details<const extensions::Extension>(details).ptr()->id();
+ OnExtensionLoaded(extension_id);
+ break;
+ }
+ case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
+ std::string extension_id =
+ content::Details<const extensions::UnloadedExtensionInfo>(details).
+ ptr()->extension->id();
+ OnExtensionUnloaded(extension_id);
+ break;
+ }
+ default:
+ NOTREACHED();
+ return;
+ }
+}
+
+void ShellWindowGeometryCache::SetSyncDelayForTests(int timeout_ms) {
+ sync_delay_ = base::TimeDelta::FromMilliseconds(timeout_ms);
+}
+
+void ShellWindowGeometryCache::OnExtensionLoaded(
+ const std::string& extension_id) {
+ ExtensionData& extension_data = cache_[extension_id];
+
+ const base::DictionaryValue* stored_windows =
+ prefs_->GetGeometryCache(extension_id);
+ if (!stored_windows)
+ return;
+
+ for (base::DictionaryValue::Iterator it(*stored_windows); !it.IsAtEnd();
+ it.Advance()) {
+ // If the cache already contains geometry for this window, don't
+ // overwrite that information since it is probably the result of an
+ // application starting up very quickly.
+ const std::string& window_id = it.key();
+ ExtensionData::iterator cached_window = extension_data.find(window_id);
+ if (cached_window == extension_data.end()) {
+ const base::DictionaryValue* stored_window;
+ if (it.value().GetAsDictionary(&stored_window)) {
+ WindowData& window_data = extension_data[it.key()];
+
+ int i;
+ if (stored_window->GetInteger("x", &i))
+ window_data.bounds.set_x(i);
+ if (stored_window->GetInteger("y", &i))
+ window_data.bounds.set_y(i);
+ if (stored_window->GetInteger("w", &i))
+ window_data.bounds.set_width(i);
+ if (stored_window->GetInteger("h", &i))
+ window_data.bounds.set_height(i);
+ if (stored_window->GetInteger("state", &i)) {
+ window_data.window_state =
+ static_cast<ui::WindowShowState>(i);
+ }
+ std::string ts_as_string;
+ if (stored_window->GetString("ts", &ts_as_string)) {
+ int64 ts;
+ if (base::StringToInt64(ts_as_string, &ts)) {
+ window_data.last_change = base::Time::FromInternalValue(ts);
+ }
+ }
+ }
+ }
+ }
+}
+
+void ShellWindowGeometryCache::OnExtensionUnloaded(
+ const std::string& extension_id) {
+ SyncToStorage();
+ cache_.erase(extension_id);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// Factory boilerplate
+
+// static
+ShellWindowGeometryCache* ShellWindowGeometryCache::Factory::GetForContext(
+ content::BrowserContext* context, bool create) {
+ return static_cast<ShellWindowGeometryCache*>(
+ GetInstance()->GetServiceForProfile(context, create));
+}
+
+ShellWindowGeometryCache::Factory*
+ShellWindowGeometryCache::Factory::GetInstance() {
+ return Singleton<ShellWindowGeometryCache::Factory>::get();
+}
+
+ShellWindowGeometryCache::Factory::Factory()
+ : ProfileKeyedServiceFactory("ShellWindowGeometryCache",
+ ProfileDependencyManager::GetInstance()) {
+ DependsOn(extensions::ExtensionPrefsFactory::GetInstance());
+}
+
+ShellWindowGeometryCache::Factory::~Factory() {
+}
+
+ProfileKeyedService*
+ShellWindowGeometryCache::Factory::BuildServiceInstanceFor(
+ content::BrowserContext* context) const {
+ Profile* profile = Profile::FromBrowserContext(context);
+ return new ShellWindowGeometryCache(
+ profile,
+ extensions::ExtensionPrefs::Get(profile));
+}
+
+bool ShellWindowGeometryCache::Factory::ServiceIsNULLWhileTesting() const {
+ return false;
+}
+
+content::BrowserContext*
+ShellWindowGeometryCache::Factory::GetBrowserContextToUse(
+ content::BrowserContext* context) const {
+ return chrome::GetBrowserContextRedirectedInIncognito(context);
+}
+
+} // namespace apps
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_
diff --git a/apps/shell_window_geometry_cache_unittest.cc b/apps/shell_window_geometry_cache_unittest.cc
new file mode 100644
index 0000000..255c78c
--- /dev/null
+++ b/apps/shell_window_geometry_cache_unittest.cc
@@ -0,0 +1,256 @@
+// 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.
+
+#include "apps/shell_window_geometry_cache.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/prefs/mock_pref_change_callback.h"
+#include "base/strings/string_number_conversions.h"
+#include "chrome/browser/extensions/extension_prefs.h"
+#include "chrome/browser/extensions/test_extension_prefs.h"
+#include "chrome/test/base/testing_profile.h"
+#include "content/public/test/test_browser_thread.h"
+#include "content/public/test/test_utils.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+ static const char kWindowId[] = "windowid";
+ static const char kWindowId2[] = "windowid2";
+
+ const char kWindowGeometryKey[] = "window_geometry";
+} // namespace
+
+using content::BrowserThread;
+
+namespace apps {
+
+// Base class for tests.
+class ShellWindowGeometryCacheTest : public testing::Test {
+ public:
+ ShellWindowGeometryCacheTest() :
+ ui_thread_(BrowserThread::UI, &ui_message_loop_) {
+ prefs_.reset(new extensions::TestExtensionPrefs(
+ ui_message_loop_.message_loop_proxy()));
+ cache_.reset(
+ new ShellWindowGeometryCache(&profile_, prefs_->prefs()));
+ cache_->SetSyncDelayForTests(0);
+ }
+
+ void AddGeometryAndLoadExtension(
+ const std::string& extension_id,
+ const std::string& window_id,
+ const gfx::Rect& bounds,
+ ui::WindowShowState state);
+
+ // Spins the UI threads' message loops to make sure any task
+ // posted to sync the geometry to the value store gets a chance to run.
+ void WaitForSync();
+
+ void LoadExtension(const std::string& extension_id);
+ void UnloadExtension(const std::string& extension_id);
+
+ protected:
+ TestingProfile profile_;
+ MessageLoopForUI ui_message_loop_;
+ content::TestBrowserThread ui_thread_;
+ scoped_ptr<extensions::TestExtensionPrefs> prefs_;
+ scoped_ptr<ShellWindowGeometryCache> cache_;
+};
+
+void ShellWindowGeometryCacheTest::AddGeometryAndLoadExtension(
+ const std::string& extension_id,
+ const std::string& window_id,
+ const gfx::Rect& bounds,
+ ui::WindowShowState state) {
+ scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
+ base::DictionaryValue* value = new base::DictionaryValue;
+ value->SetInteger("x", bounds.x());
+ value->SetInteger("y", bounds.y());
+ value->SetInteger("w", bounds.width());
+ value->SetInteger("h", bounds.height());
+ value->SetInteger("state", state);
+ dict->SetWithoutPathExpansion(window_id, value);
+ prefs_->prefs()->SetGeometryCache(extension_id, dict.Pass());
+ LoadExtension(extension_id);
+}
+
+void ShellWindowGeometryCacheTest::WaitForSync() {
+ content::RunAllPendingInMessageLoop();
+}
+
+void ShellWindowGeometryCacheTest::LoadExtension(
+ const std::string& extension_id) {
+ cache_->OnExtensionLoaded(extension_id);
+ WaitForSync();
+}
+
+void ShellWindowGeometryCacheTest::UnloadExtension(
+ const std::string& extension_id) {
+ cache_->OnExtensionUnloaded(extension_id);
+ WaitForSync();
+}
+
+// Test getting geometry from an empty store.
+TEST_F(ShellWindowGeometryCacheTest, GetGeometryEmptyStore) {
+ const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
+ ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId, NULL, NULL));
+}
+
+// Test getting geometry for an unknown extension.
+TEST_F(ShellWindowGeometryCacheTest, GetGeometryUnkownExtension) {
+ const std::string extension_id1 = prefs_->AddExtensionAndReturnId("ext1");
+ const std::string extension_id2 = prefs_->AddExtensionAndReturnId("ext2");
+ AddGeometryAndLoadExtension(extension_id1, kWindowId,
+ gfx::Rect(4, 5, 31, 43),
+ ui::SHOW_STATE_DEFAULT);
+ ASSERT_FALSE(cache_->GetGeometry(extension_id2, kWindowId, NULL, NULL));
+}
+
+// Test getting geometry for an unknown window in a known extension.
+TEST_F(ShellWindowGeometryCacheTest, GetGeometryUnkownWindow) {
+ const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
+ AddGeometryAndLoadExtension(extension_id, kWindowId,
+ gfx::Rect(4, 5, 31, 43),
+ ui::SHOW_STATE_DEFAULT);
+ ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId2, NULL, NULL));
+}
+
+// Test that loading geometry and state from the store works correctly.
+TEST_F(ShellWindowGeometryCacheTest, GetGeometryAndStateFromStore) {
+ const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
+ gfx::Rect bounds(4, 5, 31, 43);
+ ui::WindowShowState state = ui::SHOW_STATE_NORMAL;
+ AddGeometryAndLoadExtension(extension_id, kWindowId, bounds, state);
+ gfx::Rect new_bounds;
+ ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
+ ASSERT_TRUE(cache_->GetGeometry(
+ extension_id, kWindowId, &new_bounds, &new_state));
+ ASSERT_EQ(bounds, new_bounds);
+ ASSERT_EQ(state, new_state);
+}
+
+// Test saving geometry and state to the cache and state store, and reading
+// it back.
+TEST_F(ShellWindowGeometryCacheTest, SaveGeometryAndStateToStore) {
+ const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
+ const std::string window_id(kWindowId);
+
+ // inform cache of extension
+ LoadExtension(extension_id);
+
+ // update geometry stored in cache
+ gfx::Rect bounds(4, 5, 31, 43);
+ ui::WindowShowState state = ui::SHOW_STATE_NORMAL;
+ cache_->SaveGeometry(extension_id, window_id, bounds, state);
+
+ // make sure that immediately reading back geometry works
+ gfx::Rect new_bounds;
+ ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
+ ASSERT_TRUE(cache_->GetGeometry(
+ extension_id, window_id, &new_bounds, &new_state));
+ ASSERT_EQ(bounds, new_bounds);
+ ASSERT_EQ(state, new_state);
+
+ // unload extension to force cache to save data to the state store
+ UnloadExtension(extension_id);
+
+ // check if geometry got stored correctly in the state store
+ const base::DictionaryValue* dict =
+ prefs_->prefs()->GetGeometryCache(extension_id);
+ ASSERT_TRUE(dict);
+
+ ASSERT_TRUE(dict->HasKey(window_id));
+ int v;
+ ASSERT_TRUE(dict->GetInteger(window_id + ".x", &v));
+ ASSERT_EQ(bounds.x(), v);
+ ASSERT_TRUE(dict->GetInteger(window_id + ".y", &v));
+ ASSERT_EQ(bounds.y(), v);
+ ASSERT_TRUE(dict->GetInteger(window_id + ".w", &v));
+ ASSERT_EQ(bounds.width(), v);
+ ASSERT_TRUE(dict->GetInteger(window_id + ".h", &v));
+ ASSERT_EQ(bounds.height(), v);
+ ASSERT_TRUE(dict->GetInteger(window_id + ".state", &v));
+ ASSERT_EQ(state, v);
+
+ // check to make sure cache indeed doesn't know about this extension anymore
+ ASSERT_FALSE(cache_->GetGeometry(
+ extension_id, window_id, &new_bounds, &new_state));
+
+ // reload extension
+ LoadExtension(extension_id);
+ // and make sure the geometry got reloaded properly too
+ ASSERT_TRUE(cache_->GetGeometry(
+ extension_id, window_id, &new_bounds, &new_state));
+ ASSERT_EQ(bounds, new_bounds);
+ ASSERT_EQ(state, new_state);
+}
+
+// Tests that we won't do writes to the state store for SaveGeometry calls
+// which don't change the state we already have.
+TEST_F(ShellWindowGeometryCacheTest, NoDuplicateWrites) {
+ using testing::_;
+ using testing::Mock;
+
+ const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
+ gfx::Rect bounds1(100, 200, 300, 400);
+ gfx::Rect bounds2(200, 400, 600, 800);
+ gfx::Rect bounds2_duplicate(200, 400, 600, 800);
+
+ MockPrefChangeCallback observer(prefs_->pref_service());
+ PrefChangeRegistrar registrar;
+ registrar.Init(prefs_->pref_service());
+ registrar.Add("extensions.settings", observer.GetCallback());
+
+ // Write the first bounds - it should do > 0 writes.
+ EXPECT_CALL(observer, OnPreferenceChanged(_));
+ cache_->SaveGeometry(extension_id, kWindowId, bounds1,
+ ui::SHOW_STATE_DEFAULT);
+ WaitForSync();
+ Mock::VerifyAndClearExpectations(&observer);
+
+ // Write a different bounds - it should also do > 0 writes.
+ EXPECT_CALL(observer, OnPreferenceChanged(_));
+ cache_->SaveGeometry(extension_id, kWindowId, bounds2,
+ ui::SHOW_STATE_DEFAULT);
+ WaitForSync();
+ Mock::VerifyAndClearExpectations(&observer);
+
+ // Write a different state - it should also do > 0 writes.
+ EXPECT_CALL(observer, OnPreferenceChanged(_));
+ cache_->SaveGeometry(extension_id, kWindowId, bounds2,
+ ui::SHOW_STATE_NORMAL);
+ WaitForSync();
+ Mock::VerifyAndClearExpectations(&observer);
+
+ // Write a bounds and state that's a duplicate of what we already have.
+ // This should not do any writes.
+ EXPECT_CALL(observer, OnPreferenceChanged(_)).Times(0);
+ cache_->SaveGeometry(extension_id, kWindowId, bounds2_duplicate,
+ ui::SHOW_STATE_NORMAL);
+ WaitForSync();
+ Mock::VerifyAndClearExpectations(&observer);
+}
+
+// Tests that no more than kMaxCachedWindows windows will be cached.
+TEST_F(ShellWindowGeometryCacheTest, MaxWindows) {
+ const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
+ // inform cache of extension
+ LoadExtension(extension_id);
+
+ gfx::Rect bounds(4, 5, 31, 43);
+ for (size_t i = 0; i < ShellWindowGeometryCache::kMaxCachedWindows + 1; ++i) {
+ std::string window_id = "window_" + base::IntToString(i);
+ cache_->SaveGeometry(extension_id, window_id, bounds,
+ ui::SHOW_STATE_DEFAULT);
+ }
+
+ // The first added window should no longer have cached geometry.
+ EXPECT_FALSE(cache_->GetGeometry(extension_id, "window_0", NULL, NULL));
+ // All other windows should still exist.
+ for (size_t i = 1; i < ShellWindowGeometryCache::kMaxCachedWindows + 1; ++i) {
+ std::string window_id = "window_" + base::IntToString(i);
+ EXPECT_TRUE(cache_->GetGeometry(extension_id, window_id, NULL, NULL));
+ }
+}
+
+} // namespace extensions