Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(231)

Unified Diff: chrome/browser/policy/policy_browsertest.cc

Issue 14238037: Made it possible to tell whether an extension is being installed or updated. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: policy_browsertest: Added InstalledExtensionInfoObserver class to get around dangling pointer issue. Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/policy/policy_browsertest.cc
diff --git a/chrome/browser/policy/policy_browsertest.cc b/chrome/browser/policy/policy_browsertest.cc
index 1c0dddfa1673098ee5d118ef3e3a7224ce985900..123c2c0a3c0fe9d10ddcd44c75e7872753d79731 100644
--- a/chrome/browser/policy/policy_browsertest.cc
+++ b/chrome/browser/policy/policy_browsertest.cc
@@ -225,6 +225,72 @@ class MakeRequestFail {
const std::string host_;
};
+// Based on content::WindowedNotificationObserver, but specially written to
+// watch for an extensions::InstalledExtensionInfo and store its fields.
+// This is necessary because a normal WindowedNotificationObserver will store
+// the pointer to the InstalledExtensionInfo, which will immediately become
+// invalid after the call to Observe().
Matt Giuca 2013/05/02 09:51:44 This class is pretty complicated and duplicates a
Mattias Nissler (ping if slow) 2013/05/02 11:00:33 Given that we're mostly only using the notificatio
Joao da Silva 2013/05/02 11:36:29 I'd guess that other notifications have the same i
Joao da Silva 2013/05/02 11:42:03 Discussed a bit more offline with Mattias. We agre
Matt Giuca 2013/05/03 02:02:09 OK thanks for discussing and coming to a consensus
+class InstalledExtensionInfoObserver : public content::NotificationObserver {
+ public:
Mattias Nissler (ping if slow) 2013/05/02 11:00:33 indentation
Matt Giuca 2013/05/03 02:02:09 Done. (Not relevant any more.)
+ // Register to listen for notifications of the given type from either a
+ // specific source, or from all sources if |source| is
+ // NotificationService::AllSources().
+ InstalledExtensionInfoObserver(int notification_type,
+ const content::NotificationSource& source)
+ : seen_(false),
+ running_(false) {
+ registrar_.Add(this, notification_type, source);
+ }
+
+ // Wait until the specified notification occurs. If the notification was
+ // emitted between the construction of this object and this call then it
+ // returns immediately.
+ void Wait() {
+ if (seen_)
+ return;
+
+ running_ = true;
+ message_loop_runner_ = new content::MessageLoopRunner;
+ message_loop_runner_->Run();
+ EXPECT_TRUE(seen_);
+ }
+
+ const extensions::Extension* extension() const {
+ return extension_;
+ }
+
+ bool is_update() const {
+ return is_update_;
+ }
+
+ // NotificationObserver:
+ virtual void Observe(int type,
+ const content::NotificationSource& source,
+ const content::NotificationDetails& details) OVERRIDE {
+ content::Details<const extensions::InstalledExtensionInfo> installed_info(
+ details);
+ extension_ = installed_info->extension;
+ is_update_ = installed_info->is_update;
+ seen_ = true;
+ if (!running_)
+ return;
+
+ message_loop_runner_->Quit();
+ running_ = false;
+ }
+
+ private:
+ bool seen_;
+ bool running_;
+ content::NotificationRegistrar registrar_;
+
+ const extensions::Extension* extension_;
Joao da Silva 2013/05/02 11:36:29 Doesn't this pointer have the same lifetime issue?
Matt Giuca 2013/05/03 02:02:09 No, because the extension itself is longer lived (
+ bool is_update_;
+ scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
+
+ DISALLOW_COPY_AND_ASSIGN(InstalledExtensionInfoObserver);
+};
+
// Verifies that the given url |spec| can be opened. This assumes that |spec|
// points at empty.html in the test data dir.
void CheckCanOpenURL(Browser* browser, const char* spec) {
@@ -1298,14 +1364,14 @@ IN_PROC_BROWSER_TEST_F(PolicyTest, ExtensionInstallForcelist) {
PolicyMap policies;
policies.Set(key::kExtensionInstallForcelist, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, forcelist.DeepCopy());
- content::WindowedNotificationObserver observer(
+ InstalledExtensionInfoObserver observer(
chrome::NOTIFICATION_EXTENSION_INSTALLED,
content::NotificationService::AllSources());
UpdateProviderPolicy(policies);
observer.Wait();
- content::Details<const extensions::Extension> details = observer.details();
- EXPECT_EQ(kGoodCrxId, details->id());
- EXPECT_EQ(details.ptr(), service->GetExtensionById(kGoodCrxId, true));
+ const extensions::Extension* extension = observer.extension();
+ EXPECT_EQ(kGoodCrxId, extension->id());
+ EXPECT_EQ(extension, service->GetExtensionById(kGoodCrxId, true));
// The user is not allowed to uninstall force-installed extensions.
UninstallExtension(kGoodCrxId, false);
}
@@ -1370,14 +1436,13 @@ IN_PROC_BROWSER_TEST_F(PolicyTest, ExtensionInstallSources) {
POLICY_SCOPE_USER, install_sources.DeepCopy());
UpdateProviderPolicy(policies);
- content::WindowedNotificationObserver observer(
+ InstalledExtensionInfoObserver observer(
chrome::NOTIFICATION_EXTENSION_INSTALLED,
content::NotificationService::AllSources());
PerformClick(1, 0);
observer.Wait();
- content::Details<const extensions::Extension> details = observer.details();
- EXPECT_EQ(kAdBlockCrxId, details->id());
+ EXPECT_EQ(kAdBlockCrxId, observer.extension()->id());
// The first extension shouldn't be present, the second should be there.
EXPECT_FALSE(extension_service()->GetExtensionById(kGoodCrxId, true));
« no previous file with comments | « chrome/browser/performance_monitor/performance_monitor.cc ('k') | chrome/browser/sync_file_system/sync_file_system_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698