Commit 829f0462 authored by limasdf@gmail.com's avatar limasdf@gmail.com

Use ExtensionRegistryObserver instead of DEPRECATED extension notify from app_window_geometry_cache

BUG=354046

Review URL: https://codereview.chromium.org/270703003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@269523 0039d316-1c4b-4281-b951-d872f2087c98
parent ec8423cd
...@@ -7,14 +7,12 @@ ...@@ -7,14 +7,12 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/incognito_helpers.h" #include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h" #include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "extensions/browser/extension_prefs.h" #include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_prefs_factory.h" #include "extensions/browser/extension_prefs_factory.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extensions_browser_client.h" #include "extensions/browser/extensions_browser_client.h"
#include "extensions/common/extension.h" #include "extensions/common/extension.h"
...@@ -32,13 +30,9 @@ AppWindowGeometryCache::AppWindowGeometryCache( ...@@ -32,13 +30,9 @@ AppWindowGeometryCache::AppWindowGeometryCache(
Profile* profile, Profile* profile,
extensions::ExtensionPrefs* prefs) extensions::ExtensionPrefs* prefs)
: prefs_(prefs), : prefs_(prefs),
sync_delay_(base::TimeDelta::FromMilliseconds(kSyncTimeoutMilliseconds)) { sync_delay_(base::TimeDelta::FromMilliseconds(kSyncTimeoutMilliseconds)),
registrar_.Add(this, extension_registry_observer_(this) {
chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, extension_registry_observer_.Add(extensions::ExtensionRegistry::Get(profile));
content::Source<Profile>(profile));
registrar_.Add(this,
chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
content::Source<Profile>(profile));
} }
AppWindowGeometryCache::~AppWindowGeometryCache() {} AppWindowGeometryCache::~AppWindowGeometryCache() {}
...@@ -192,29 +186,18 @@ AppWindowGeometryCache::WindowData::WindowData() ...@@ -192,29 +186,18 @@ AppWindowGeometryCache::WindowData::WindowData()
AppWindowGeometryCache::WindowData::~WindowData() {} AppWindowGeometryCache::WindowData::~WindowData() {}
void AppWindowGeometryCache::Observe( void AppWindowGeometryCache::OnExtensionLoaded(
int type, content::BrowserContext* browser_context,
const content::NotificationSource& source, const extensions::Extension* extension) {
const content::NotificationDetails& details) { LoadGeometryFromStorage(extension->id());
switch (type) { }
case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: {
std::string extension_id = void AppWindowGeometryCache::OnExtensionUnloaded(
content::Details<const extensions::Extension>(details).ptr()->id(); content::BrowserContext* browser_context,
LoadGeometryFromStorage(extension_id); const extensions::Extension* extension,
break; extensions::UnloadedExtensionInfo::Reason reason) {
} SyncToStorage();
case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { cache_.erase(extension->id());
std::string extension_id =
content::Details<const extensions::UnloadedExtensionInfo>(details)
.ptr()
->extension->id();
OnExtensionUnloaded(extension_id);
break;
}
default:
NOTREACHED();
return;
}
} }
void AppWindowGeometryCache::SetSyncDelayForTests(int timeout_ms) { void AppWindowGeometryCache::SetSyncDelayForTests(int timeout_ms) {
...@@ -274,12 +257,6 @@ void AppWindowGeometryCache::LoadGeometryFromStorage( ...@@ -274,12 +257,6 @@ void AppWindowGeometryCache::LoadGeometryFromStorage(
} }
} }
void AppWindowGeometryCache::OnExtensionUnloaded(
const std::string& extension_id) {
SyncToStorage();
cache_.erase(extension_id);
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Factory boilerplate // Factory boilerplate
......
...@@ -12,13 +12,13 @@ ...@@ -12,13 +12,13 @@
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/memory/singleton.h" #include "base/memory/singleton.h"
#include "base/observer_list.h" #include "base/observer_list.h"
#include "base/scoped_observer.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "base/timer/timer.h" #include "base/timer/timer.h"
#include "base/values.h" #include "base/values.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h" #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
#include "components/keyed_service/core/keyed_service.h" #include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/notification_observer.h" #include "extensions/browser/extension_registry_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "ui/base/ui_base_types.h" #include "ui/base/ui_base_types.h"
#include "ui/gfx/rect.h" #include "ui/gfx/rect.h"
...@@ -26,6 +26,7 @@ class Profile; ...@@ -26,6 +26,7 @@ class Profile;
namespace extensions { namespace extensions {
class ExtensionPrefs; class ExtensionPrefs;
class ExtensionRegistry;
} }
namespace apps { namespace apps {
...@@ -34,7 +35,7 @@ namespace apps { ...@@ -34,7 +35,7 @@ namespace apps {
// for IO when creating a new window, and to not cause IO on every window // for IO when creating a new window, and to not cause IO on every window
// geometry change. // geometry change.
class AppWindowGeometryCache : public KeyedService, class AppWindowGeometryCache : public KeyedService,
public content::NotificationObserver { public extensions::ExtensionRegistryObserver {
public: public:
class Factory : public BrowserContextKeyedServiceFactory { class Factory : public BrowserContextKeyedServiceFactory {
public: public:
...@@ -122,31 +123,38 @@ class AppWindowGeometryCache : public KeyedService, ...@@ -122,31 +123,38 @@ class AppWindowGeometryCache : public KeyedService,
// Data stored for each extension. // Data stored for each extension.
typedef std::map<std::string, WindowData> ExtensionData; typedef std::map<std::string, WindowData> ExtensionData;
// content::NotificationObserver // ExtensionRegistryObserver implementation.
virtual void Observe(int type, virtual void OnExtensionLoaded(
const content::NotificationSource& source, content::BrowserContext* browser_context,
const content::NotificationDetails& details) OVERRIDE; const extensions::Extension* extension) OVERRIDE;
virtual void OnExtensionUnloaded(
content::BrowserContext* browser_context,
const extensions::Extension* extension,
extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE;
void LoadGeometryFromStorage(const std::string& extension_id); void LoadGeometryFromStorage(const std::string& extension_id);
void OnExtensionUnloaded(const std::string& extension_id);
void SyncToStorage(); void SyncToStorage();
// Preferences storage. // Preferences storage.
extensions::ExtensionPrefs* prefs_; extensions::ExtensionPrefs* prefs_;
// Cached data // Cached data.
std::map<std::string, ExtensionData> cache_; std::map<std::string, ExtensionData> cache_;
// Data that still needs saving // Data that still needs saving.
std::set<std::string> unsynced_extensions_; std::set<std::string> unsynced_extensions_;
// The timer used to save the data // The timer used to save the data.
base::OneShotTimer<AppWindowGeometryCache> sync_timer_; base::OneShotTimer<AppWindowGeometryCache> sync_timer_;
// The timeout value we'll use for |sync_timer_|. // The timeout value we'll use for |sync_timer_|.
base::TimeDelta sync_delay_; base::TimeDelta sync_delay_;
content::NotificationRegistrar registrar_; // Listen to extension load, unloaded notifications.
ScopedObserver<extensions::ExtensionRegistry,
extensions::ExtensionRegistryObserver>
extension_registry_observer_;
ObserverList<Observer> observers_; ObserverList<Observer> observers_;
}; };
......
...@@ -11,23 +11,38 @@ ...@@ -11,23 +11,38 @@
#include "content/public/test/test_browser_thread.h" #include "content/public/test/test_browser_thread.h"
#include "content/public/test/test_utils.h" #include "content/public/test/test_utils.h"
#include "extensions/browser/extension_prefs.h" #include "extensions/browser/extension_prefs.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/value_builder.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
using content::BrowserThread;
namespace apps {
namespace {
const char kWindowId[] = "windowid"; const char kWindowId[] = "windowid";
const char kWindowId2[] = "windowid2"; const char kWindowId2[] = "windowid2";
using content::BrowserThread; // Create a very simple extension with id.
scoped_refptr<extensions::Extension> CreateExtension(const std::string& id) {
return extensions::ExtensionBuilder()
.SetManifest(extensions::DictionaryBuilder().Set("name", "test").Set(
"version", "0.1"))
.SetID(id)
.Build();
}
namespace apps { } // namespace
// Base class for tests. // Base class for tests.
class AppWindowGeometryCacheTest : public testing::Test { class AppWindowGeometryCacheTest : public testing::Test {
public: public:
AppWindowGeometryCacheTest() AppWindowGeometryCacheTest()
: ui_thread_(BrowserThread::UI, &ui_message_loop_) { : profile_(new TestingProfile),
ui_thread_(BrowserThread::UI, &ui_message_loop_) {
prefs_.reset(new extensions::TestExtensionPrefs( prefs_.reset(new extensions::TestExtensionPrefs(
ui_message_loop_.message_loop_proxy().get())); ui_message_loop_.message_loop_proxy().get()));
cache_.reset(new AppWindowGeometryCache(&profile_, prefs_->prefs())); cache_.reset(new AppWindowGeometryCache(profile_.get(), prefs_->prefs()));
cache_->SetSyncDelayForTests(0); cache_->SetSyncDelayForTests(0);
} }
...@@ -45,7 +60,7 @@ class AppWindowGeometryCacheTest : public testing::Test { ...@@ -45,7 +60,7 @@ class AppWindowGeometryCacheTest : public testing::Test {
void UnloadExtension(const std::string& extension_id); void UnloadExtension(const std::string& extension_id);
protected: protected:
TestingProfile profile_; scoped_ptr<TestingProfile> profile_;
base::MessageLoopForUI ui_message_loop_; base::MessageLoopForUI ui_message_loop_;
content::TestBrowserThread ui_thread_; content::TestBrowserThread ui_thread_;
scoped_ptr<extensions::TestExtensionPrefs> prefs_; scoped_ptr<extensions::TestExtensionPrefs> prefs_;
...@@ -86,7 +101,12 @@ void AppWindowGeometryCacheTest::LoadExtension( ...@@ -86,7 +101,12 @@ void AppWindowGeometryCacheTest::LoadExtension(
void AppWindowGeometryCacheTest::UnloadExtension( void AppWindowGeometryCacheTest::UnloadExtension(
const std::string& extension_id) { const std::string& extension_id) {
cache_->OnExtensionUnloaded(extension_id); scoped_refptr<extensions::Extension> extension =
CreateExtension(extension_id);
cache_->OnExtensionUnloaded(
profile_.get(),
extension.get(),
extensions::UnloadedExtensionInfo::REASON_DISABLE);
WaitForSync(); WaitForSync();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment