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

Side by Side 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, 7 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <algorithm> 5 #include <algorithm>
6 #include <string> 6 #include <string>
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 // Remove filters for requests to the |host|. Run on IO thread. 218 // Remove filters for requests to the |host|. Run on IO thread.
219 static void UndoMakeRequestFailOnIO(const std::string& host) { 219 static void UndoMakeRequestFailOnIO(const std::string& host) {
220 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); 220 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
221 filter->RemoveHostnameHandler("http", host); 221 filter->RemoveHostnameHandler("http", host);
222 filter->RemoveHostnameHandler("https", host); 222 filter->RemoveHostnameHandler("https", host);
223 } 223 }
224 224
225 const std::string host_; 225 const std::string host_;
226 }; 226 };
227 227
228 // Based on content::WindowedNotificationObserver, but specially written to
229 // watch for an extensions::InstalledExtensionInfo and store its fields.
230 // This is necessary because a normal WindowedNotificationObserver will store
231 // the pointer to the InstalledExtensionInfo, which will immediately become
232 // 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
233 class InstalledExtensionInfoObserver : public content::NotificationObserver {
234 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.)
235 // Register to listen for notifications of the given type from either a
236 // specific source, or from all sources if |source| is
237 // NotificationService::AllSources().
238 InstalledExtensionInfoObserver(int notification_type,
239 const content::NotificationSource& source)
240 : seen_(false),
241 running_(false) {
242 registrar_.Add(this, notification_type, source);
243 }
244
245 // Wait until the specified notification occurs. If the notification was
246 // emitted between the construction of this object and this call then it
247 // returns immediately.
248 void Wait() {
249 if (seen_)
250 return;
251
252 running_ = true;
253 message_loop_runner_ = new content::MessageLoopRunner;
254 message_loop_runner_->Run();
255 EXPECT_TRUE(seen_);
256 }
257
258 const extensions::Extension* extension() const {
259 return extension_;
260 }
261
262 bool is_update() const {
263 return is_update_;
264 }
265
266 // NotificationObserver:
267 virtual void Observe(int type,
268 const content::NotificationSource& source,
269 const content::NotificationDetails& details) OVERRIDE {
270 content::Details<const extensions::InstalledExtensionInfo> installed_info(
271 details);
272 extension_ = installed_info->extension;
273 is_update_ = installed_info->is_update;
274 seen_ = true;
275 if (!running_)
276 return;
277
278 message_loop_runner_->Quit();
279 running_ = false;
280 }
281
282 private:
283 bool seen_;
284 bool running_;
285 content::NotificationRegistrar registrar_;
286
287 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 (
288 bool is_update_;
289 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
290
291 DISALLOW_COPY_AND_ASSIGN(InstalledExtensionInfoObserver);
292 };
293
228 // Verifies that the given url |spec| can be opened. This assumes that |spec| 294 // Verifies that the given url |spec| can be opened. This assumes that |spec|
229 // points at empty.html in the test data dir. 295 // points at empty.html in the test data dir.
230 void CheckCanOpenURL(Browser* browser, const char* spec) { 296 void CheckCanOpenURL(Browser* browser, const char* spec) {
231 GURL url(spec); 297 GURL url(spec);
232 ui_test_utils::NavigateToURL(browser, url); 298 ui_test_utils::NavigateToURL(browser, url);
233 content::WebContents* contents = 299 content::WebContents* contents =
234 browser->tab_strip_model()->GetActiveWebContents(); 300 browser->tab_strip_model()->GetActiveWebContents();
235 EXPECT_EQ(url, contents->GetURL()); 301 EXPECT_EQ(url, contents->GetURL());
236 EXPECT_EQ(net::FormatUrl(url, std::string()), contents->GetTitle()); 302 EXPECT_EQ(net::FormatUrl(url, std::string()), contents->GetTitle());
237 } 303 }
(...skipping 1053 matching lines...) Expand 10 before | Expand all | Expand 10 after
1291 base::FilePath(kTestExtensionsDir).Append(kGoodCrxManifestName); 1357 base::FilePath(kTestExtensionsDir).Append(kGoodCrxManifestName);
1292 GURL url(URLRequestMockHTTPJob::GetMockUrl(path)); 1358 GURL url(URLRequestMockHTTPJob::GetMockUrl(path));
1293 1359
1294 // Setting the forcelist extension should install "good.crx". 1360 // Setting the forcelist extension should install "good.crx".
1295 base::ListValue forcelist; 1361 base::ListValue forcelist;
1296 forcelist.Append(base::Value::CreateStringValue(base::StringPrintf( 1362 forcelist.Append(base::Value::CreateStringValue(base::StringPrintf(
1297 "%s;%s", kGoodCrxId, url.spec().c_str()))); 1363 "%s;%s", kGoodCrxId, url.spec().c_str())));
1298 PolicyMap policies; 1364 PolicyMap policies;
1299 policies.Set(key::kExtensionInstallForcelist, POLICY_LEVEL_MANDATORY, 1365 policies.Set(key::kExtensionInstallForcelist, POLICY_LEVEL_MANDATORY,
1300 POLICY_SCOPE_USER, forcelist.DeepCopy()); 1366 POLICY_SCOPE_USER, forcelist.DeepCopy());
1301 content::WindowedNotificationObserver observer( 1367 InstalledExtensionInfoObserver observer(
1302 chrome::NOTIFICATION_EXTENSION_INSTALLED, 1368 chrome::NOTIFICATION_EXTENSION_INSTALLED,
1303 content::NotificationService::AllSources()); 1369 content::NotificationService::AllSources());
1304 UpdateProviderPolicy(policies); 1370 UpdateProviderPolicy(policies);
1305 observer.Wait(); 1371 observer.Wait();
1306 content::Details<const extensions::Extension> details = observer.details(); 1372 const extensions::Extension* extension = observer.extension();
1307 EXPECT_EQ(kGoodCrxId, details->id()); 1373 EXPECT_EQ(kGoodCrxId, extension->id());
1308 EXPECT_EQ(details.ptr(), service->GetExtensionById(kGoodCrxId, true)); 1374 EXPECT_EQ(extension, service->GetExtensionById(kGoodCrxId, true));
1309 // The user is not allowed to uninstall force-installed extensions. 1375 // The user is not allowed to uninstall force-installed extensions.
1310 UninstallExtension(kGoodCrxId, false); 1376 UninstallExtension(kGoodCrxId, false);
1311 } 1377 }
1312 1378
1313 IN_PROC_BROWSER_TEST_F(PolicyTest, ExtensionAllowedTypes) { 1379 IN_PROC_BROWSER_TEST_F(PolicyTest, ExtensionAllowedTypes) {
1314 // Verifies that extensions are blocked if policy specifies an allowed types 1380 // Verifies that extensions are blocked if policy specifies an allowed types
1315 // list and the extension's type is not on that list. 1381 // list and the extension's type is not on that list.
1316 ExtensionService* service = extension_service(); 1382 ExtensionService* service = extension_service();
1317 ASSERT_FALSE(service->GetExtensionById(kGoodCrxId, true)); 1383 ASSERT_FALSE(service->GetExtensionById(kGoodCrxId, true));
1318 ASSERT_FALSE(service->GetExtensionById(kHostedAppCrxId, true)); 1384 ASSERT_FALSE(service->GetExtensionById(kHostedAppCrxId, true));
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1363 1429
1364 // Install the policy and trigger another download. 1430 // Install the policy and trigger another download.
1365 base::ListValue install_sources; 1431 base::ListValue install_sources;
1366 install_sources.AppendString(install_source_url.spec()); 1432 install_sources.AppendString(install_source_url.spec());
1367 install_sources.AppendString(referrer_url.spec()); 1433 install_sources.AppendString(referrer_url.spec());
1368 PolicyMap policies; 1434 PolicyMap policies;
1369 policies.Set(key::kExtensionInstallSources, POLICY_LEVEL_MANDATORY, 1435 policies.Set(key::kExtensionInstallSources, POLICY_LEVEL_MANDATORY,
1370 POLICY_SCOPE_USER, install_sources.DeepCopy()); 1436 POLICY_SCOPE_USER, install_sources.DeepCopy());
1371 UpdateProviderPolicy(policies); 1437 UpdateProviderPolicy(policies);
1372 1438
1373 content::WindowedNotificationObserver observer( 1439 InstalledExtensionInfoObserver observer(
1374 chrome::NOTIFICATION_EXTENSION_INSTALLED, 1440 chrome::NOTIFICATION_EXTENSION_INSTALLED,
1375 content::NotificationService::AllSources()); 1441 content::NotificationService::AllSources());
1376 PerformClick(1, 0); 1442 PerformClick(1, 0);
1377 observer.Wait(); 1443 observer.Wait();
1378 1444
1379 content::Details<const extensions::Extension> details = observer.details(); 1445 EXPECT_EQ(kAdBlockCrxId, observer.extension()->id());
1380 EXPECT_EQ(kAdBlockCrxId, details->id());
1381 1446
1382 // The first extension shouldn't be present, the second should be there. 1447 // The first extension shouldn't be present, the second should be there.
1383 EXPECT_FALSE(extension_service()->GetExtensionById(kGoodCrxId, true)); 1448 EXPECT_FALSE(extension_service()->GetExtensionById(kGoodCrxId, true));
1384 EXPECT_TRUE(extension_service()->GetExtensionById(kAdBlockCrxId, false)); 1449 EXPECT_TRUE(extension_service()->GetExtensionById(kAdBlockCrxId, false));
1385 } 1450 }
1386 1451
1387 IN_PROC_BROWSER_TEST_F(PolicyTest, HomepageLocation) { 1452 IN_PROC_BROWSER_TEST_F(PolicyTest, HomepageLocation) {
1388 // Verifies that the homepage can be configured with policies. 1453 // Verifies that the homepage can be configured with policies.
1389 // Set a default, and check that the home button navigates there. 1454 // Set a default, and check that the home button navigates there.
1390 browser()->profile()->GetPrefs()->SetString( 1455 browser()->profile()->GetPrefs()->SetString(
(...skipping 684 matching lines...) Expand 10 before | Expand all | Expand 10 after
2075 GetDefaultVariationsServerURLForTesting(); 2140 GetDefaultVariationsServerURLForTesting();
2076 2141
2077 // Policy is applied and pref is already updated in local state. 2142 // Policy is applied and pref is already updated in local state.
2078 EXPECT_EQ(default_variations_url + "?restrict=restricted", 2143 EXPECT_EQ(default_variations_url + "?restrict=restricted",
2079 chrome_variations::VariationsService::GetVariationsServerURL( 2144 chrome_variations::VariationsService::GetVariationsServerURL(
2080 g_browser_process->local_state()).spec()); 2145 g_browser_process->local_state()).spec());
2081 } 2146 }
2082 #endif 2147 #endif
2083 2148
2084 } // namespace policy 2149 } // namespace policy
OLDNEW
« 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