Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / management / management_browsertest.cc
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
3 // found in the LICENSE file.
4
5 #include "base/bind.h"
6 #include "base/bind_helpers.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/prefs/scoped_user_pref_update.h"
10 #include "base/stl_util.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/extensions/extension_browsertest.h"
13 #include "chrome/browser/extensions/extension_host.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/extension_system.h"
16 #include "chrome/browser/extensions/extension_test_message_listener.h"
17 #include "chrome/browser/extensions/external_policy_loader.h"
18 #include "chrome/browser/extensions/updater/extension_downloader.h"
19 #include "chrome/browser/extensions/updater/extension_updater.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/ui/browser.h"
22 #include "chrome/common/pref_names.h"
23 #include "chrome/common/url_constants.h"
24 #include "chrome/test/base/ui_test_utils.h"
25 #include "content/public/browser/notification_service.h"
26 #include "content/public/browser/render_view_host.h"
27 #include "content/public/test/browser_test_utils.h"
28 #include "content/test/net/url_request_prepackaged_interceptor.h"
29 #include "extensions/browser/pref_names.h"
30 #include "net/url_request/url_fetcher.h"
31
32 using extensions::Extension;
33 using extensions::Manifest;
34
35 class ExtensionManagementTest : public ExtensionBrowserTest {
36  protected:
37   // Helper method that returns whether the extension is at the given version.
38   // This calls version(), which must be defined in the extension's bg page,
39   // as well as asking the extension itself.
40   //
41   // Note that 'version' here means something different than the version field
42   // in the extension's manifest. We use the version as reported by the
43   // background page to test how overinstalling crx files with the same
44   // manifest version works.
45   bool IsExtensionAtVersion(const Extension* extension,
46                             const std::string& expected_version) {
47     // Test that the extension's version from the manifest and reported by the
48     // background page is correct.  This is to ensure that the processes are in
49     // sync with the Extension.
50     extensions::ProcessManager* manager =
51         extensions::ExtensionSystem::Get(browser()->profile())->
52             process_manager();
53     extensions::ExtensionHost* ext_host =
54         manager->GetBackgroundHostForExtension(extension->id());
55     EXPECT_TRUE(ext_host);
56     if (!ext_host)
57       return false;
58
59     std::string version_from_bg;
60     bool exec = content::ExecuteScriptAndExtractString(
61         ext_host->render_view_host(), "version()", &version_from_bg);
62     EXPECT_TRUE(exec);
63     if (!exec)
64       return false;
65
66     if (version_from_bg != expected_version ||
67         extension->VersionString() != expected_version)
68       return false;
69     return true;
70   }
71 };
72
73 #if defined(OS_LINUX)
74 // Times out sometimes on Linux.  http://crbug.com/89727
75 #define MAYBE_InstallSameVersion DISABLED_InstallSameVersion
76 #else
77 #define MAYBE_InstallSameVersion InstallSameVersion
78 #endif
79
80 // Tests that installing the same version overwrites.
81 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, MAYBE_InstallSameVersion) {
82   const Extension* extension = InstallExtension(
83       test_data_dir_.AppendASCII("install/install.crx"), 1);
84   ASSERT_TRUE(extension);
85   base::FilePath old_path = extension->path();
86
87   // Install an extension with the same version. The previous install should be
88   // overwritten.
89   extension = InstallExtension(
90       test_data_dir_.AppendASCII("install/install_same_version.crx"), 0);
91   ASSERT_TRUE(extension);
92   base::FilePath new_path = extension->path();
93
94   EXPECT_FALSE(IsExtensionAtVersion(extension, "1.0"));
95   EXPECT_NE(old_path.value(), new_path.value());
96 }
97
98 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, InstallOlderVersion) {
99   const Extension* extension = InstallExtension(
100       test_data_dir_.AppendASCII("install/install.crx"), 1);
101   ASSERT_TRUE(extension);
102   ASSERT_FALSE(InstallExtension(
103       test_data_dir_.AppendASCII("install/install_older_version.crx"), 0));
104   EXPECT_TRUE(IsExtensionAtVersion(extension, "1.0"));
105 }
106
107 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, InstallThenCancel) {
108   const Extension* extension = InstallExtension(
109       test_data_dir_.AppendASCII("install/install.crx"), 1);
110   ASSERT_TRUE(extension);
111
112   // Cancel this install.
113   ASSERT_FALSE(StartInstallButCancel(
114       test_data_dir_.AppendASCII("install/install_v2.crx")));
115   EXPECT_TRUE(IsExtensionAtVersion(extension, "1.0"));
116 }
117
118 #if defined(OS_WIN)
119 // http://crbug.com/141913
120 #define MAYBE_InstallRequiresConfirm DISABLED_InstallRequiresConfirm
121 #else
122 #define MAYBE_InstallRequiresConfirm InstallRequiresConfirm
123 #endif
124 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, MAYBE_InstallRequiresConfirm) {
125   // Installing the extension without an auto confirming UI should result in
126   // it being disabled, since good.crx has permissions that require approval.
127   ExtensionService* service = extensions::ExtensionSystem::Get(
128       browser()->profile())->extension_service();
129   std::string id = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
130   ASSERT_FALSE(InstallExtension(test_data_dir_.AppendASCII("good.crx"), 0));
131   ASSERT_TRUE(service->GetExtensionById(id, true));
132   UninstallExtension(id);
133
134   // And the install should succeed when the permissions are accepted.
135   ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(
136       test_data_dir_.AppendASCII("good.crx"), 1, browser()));
137   UninstallExtension(id);
138 }
139
140 // Tests that disabling and re-enabling an extension works.
141 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, DisableEnable) {
142   extensions::ProcessManager* manager =
143       extensions::ExtensionSystem::Get(browser()->profile())->process_manager();
144   ExtensionService* service = extensions::ExtensionSystem::Get(
145       browser()->profile())->extension_service();
146   const size_t size_before = service->extensions()->size();
147
148   // Load an extension, expect the background page to be available.
149   std::string extension_id = "bjafgdebaacbbbecmhlhpofkepfkgcpa";
150   ASSERT_TRUE(LoadExtension(
151       test_data_dir_.AppendASCII("good").AppendASCII("Extensions")
152                     .AppendASCII(extension_id)
153                     .AppendASCII("1.0")));
154   ASSERT_EQ(size_before + 1, service->extensions()->size());
155   EXPECT_EQ(0u, service->disabled_extensions()->size());
156   EXPECT_TRUE(manager->GetBackgroundHostForExtension(extension_id));
157
158   // After disabling, the background page should go away.
159   DisableExtension(extension_id);
160   EXPECT_EQ(size_before, service->extensions()->size());
161   EXPECT_EQ(1u, service->disabled_extensions()->size());
162   EXPECT_FALSE(manager->GetBackgroundHostForExtension(extension_id));
163
164   // And bring it back.
165   EnableExtension(extension_id);
166   EXPECT_EQ(size_before + 1, service->extensions()->size());
167   EXPECT_EQ(0u, service->disabled_extensions()->size());
168   EXPECT_TRUE(manager->GetBackgroundHostForExtension(extension_id));
169 }
170
171 // Used for testing notifications sent during extension updates.
172 class NotificationListener : public content::NotificationObserver {
173  public:
174   NotificationListener() : started_(false), finished_(false) {
175     int types[] = {
176       chrome::NOTIFICATION_EXTENSION_UPDATING_STARTED,
177       chrome::NOTIFICATION_EXTENSION_UPDATE_FOUND
178     };
179     for (size_t i = 0; i < arraysize(types); i++) {
180       registrar_.Add(
181           this, types[i], content::NotificationService::AllSources());
182     }
183   }
184   virtual ~NotificationListener() {}
185
186   bool started() { return started_; }
187
188   bool finished() { return finished_; }
189
190   const std::set<std::string>& updates() { return updates_; }
191
192   void Reset() {
193     started_ = false;
194     finished_ = false;
195     updates_.clear();
196   }
197
198   // Implements content::NotificationObserver interface.
199   virtual void Observe(int type,
200                        const content::NotificationSource& source,
201                        const content::NotificationDetails& details) OVERRIDE {
202     switch (type) {
203       case chrome::NOTIFICATION_EXTENSION_UPDATING_STARTED: {
204         EXPECT_FALSE(started_);
205         started_ = true;
206         break;
207       }
208       case chrome::NOTIFICATION_EXTENSION_UPDATE_FOUND: {
209         const std::string& id =
210             content::Details<extensions::UpdateDetails>(details)->id;
211         updates_.insert(id);
212         break;
213       }
214       default:
215         NOTREACHED();
216     }
217   }
218
219   void OnFinished() {
220     EXPECT_FALSE(finished_);
221     finished_ = true;
222   }
223
224  private:
225   content::NotificationRegistrar registrar_;
226
227   // Did we see EXTENSION_UPDATING_STARTED?
228   bool started_;
229
230   // Did we see EXTENSION_UPDATING_FINISHED?
231   bool finished_;
232
233   // The set of extension id's we've seen via EXTENSION_UPDATE_FOUND.
234   std::set<std::string> updates_;
235 };
236
237 #if defined(OS_WIN)
238 // Fails consistently on Windows XP, see: http://crbug.com/120640.
239 #define MAYBE_AutoUpdate DISABLED_AutoUpdate
240 #else
241 // See http://crbug.com/103371 and http://crbug.com/120640.
242 #if defined(ADDRESS_SANITIZER)
243 #define MAYBE_AutoUpdate DISABLED_AutoUpdate
244 #else
245 #define MAYBE_AutoUpdate AutoUpdate
246 #endif
247 #endif
248
249 // Tests extension autoupdate.
250 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, MAYBE_AutoUpdate) {
251   NotificationListener notification_listener;
252   base::FilePath basedir = test_data_dir_.AppendASCII("autoupdate");
253   // Note: This interceptor gets requests on the IO thread.
254   content::URLLocalHostRequestPrepackagedInterceptor interceptor;
255   net::URLFetcher::SetEnableInterceptionForTests(true);
256
257   interceptor.SetResponseIgnoreQuery(
258       GURL("http://localhost/autoupdate/manifest"),
259       basedir.AppendASCII("manifest_v2.xml"));
260   interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
261                                           basedir.AppendASCII("v2.crx"));
262
263   // Install version 1 of the extension.
264   ExtensionTestMessageListener listener1("v1 installed", false);
265   ExtensionService* service = extensions::ExtensionSystem::Get(
266       browser()->profile())->extension_service();
267   const size_t size_before = service->extensions()->size();
268   ASSERT_TRUE(service->disabled_extensions()->is_empty());
269   const Extension* extension =
270       InstallExtension(basedir.AppendASCII("v1.crx"), 1);
271   ASSERT_TRUE(extension);
272   listener1.WaitUntilSatisfied();
273   ASSERT_EQ(size_before + 1, service->extensions()->size());
274   ASSERT_EQ("ogjcoiohnmldgjemafoockdghcjciccf", extension->id());
275   ASSERT_EQ("1.0", extension->VersionString());
276
277   extensions::ExtensionUpdater::CheckParams params;
278   params.callback =
279       base::Bind(&NotificationListener::OnFinished,
280                  base::Unretained(&notification_listener));
281
282   // Run autoupdate and make sure version 2 of the extension was installed.
283   ExtensionTestMessageListener listener2("v2 installed", false);
284   service->updater()->CheckNow(params);
285   ASSERT_TRUE(WaitForExtensionInstall());
286   listener2.WaitUntilSatisfied();
287   ASSERT_EQ(size_before + 1, service->extensions()->size());
288   extension = service->GetExtensionById(
289       "ogjcoiohnmldgjemafoockdghcjciccf", false);
290   ASSERT_TRUE(extension);
291   ASSERT_EQ("2.0", extension->VersionString());
292   ASSERT_TRUE(notification_listener.started());
293   ASSERT_TRUE(notification_listener.finished());
294   ASSERT_TRUE(ContainsKey(notification_listener.updates(),
295                           "ogjcoiohnmldgjemafoockdghcjciccf"));
296   notification_listener.Reset();
297
298   // Now try doing an update to version 3, which has been incorrectly
299   // signed. This should fail.
300   interceptor.SetResponseIgnoreQuery(
301       GURL("http://localhost/autoupdate/manifest"),
302       basedir.AppendASCII("manifest_v3.xml"));
303   interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v3.crx"),
304                                      basedir.AppendASCII("v3.crx"));
305
306   service->updater()->CheckNow(params);
307   ASSERT_TRUE(WaitForExtensionInstallError());
308   ASSERT_TRUE(notification_listener.started());
309   ASSERT_TRUE(notification_listener.finished());
310   ASSERT_TRUE(ContainsKey(notification_listener.updates(),
311                           "ogjcoiohnmldgjemafoockdghcjciccf"));
312
313   // Make sure the extension state is the same as before.
314   ASSERT_EQ(size_before + 1, service->extensions()->size());
315   extension = service->GetExtensionById(
316       "ogjcoiohnmldgjemafoockdghcjciccf", false);
317   ASSERT_TRUE(extension);
318   ASSERT_EQ("2.0", extension->VersionString());
319 }
320
321 #if defined(OS_WIN)
322 // Fails consistently on Windows XP, see: http://crbug.com/120640.
323 #define MAYBE_AutoUpdateDisabledExtensions DISABLED_AutoUpdateDisabledExtensions
324 #else
325 #if defined(ADDRESS_SANITIZER)
326 #define MAYBE_AutoUpdateDisabledExtensions DISABLED_AutoUpdateDisabledExtensions
327 #else
328 #define MAYBE_AutoUpdateDisabledExtensions AutoUpdateDisabledExtensions
329 #endif
330 #endif
331
332 // Tests extension autoupdate.
333 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest,
334                        MAYBE_AutoUpdateDisabledExtensions) {
335   NotificationListener notification_listener;
336   base::FilePath basedir = test_data_dir_.AppendASCII("autoupdate");
337   // Note: This interceptor gets requests on the IO thread.
338   content::URLLocalHostRequestPrepackagedInterceptor interceptor;
339   net::URLFetcher::SetEnableInterceptionForTests(true);
340
341   interceptor.SetResponseIgnoreQuery(
342       GURL("http://localhost/autoupdate/manifest"),
343       basedir.AppendASCII("manifest_v2.xml"));
344   interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
345                                      basedir.AppendASCII("v2.crx"));
346
347   // Install version 1 of the extension.
348   ExtensionTestMessageListener listener1("v1 installed", false);
349   ExtensionService* service = extensions::ExtensionSystem::Get(
350       browser()->profile())->extension_service();
351   const size_t enabled_size_before = service->extensions()->size();
352   const size_t disabled_size_before = service->disabled_extensions()->size();
353   const Extension* extension =
354       InstallExtension(basedir.AppendASCII("v1.crx"), 1);
355   ASSERT_TRUE(extension);
356   listener1.WaitUntilSatisfied();
357   DisableExtension(extension->id());
358   ASSERT_EQ(disabled_size_before + 1, service->disabled_extensions()->size());
359   ASSERT_EQ(enabled_size_before, service->extensions()->size());
360   ASSERT_EQ("ogjcoiohnmldgjemafoockdghcjciccf", extension->id());
361   ASSERT_EQ("1.0", extension->VersionString());
362
363   extensions::ExtensionUpdater::CheckParams params;
364   params.callback =
365       base::Bind(&NotificationListener::OnFinished,
366                  base::Unretained(&notification_listener));
367
368   ExtensionTestMessageListener listener2("v2 installed", false);
369   // Run autoupdate and make sure version 2 of the extension was installed but
370   // is still disabled.
371   service->updater()->CheckNow(params);
372   ASSERT_TRUE(WaitForExtensionInstall());
373   ASSERT_EQ(disabled_size_before + 1, service->disabled_extensions()->size());
374   ASSERT_EQ(enabled_size_before, service->extensions()->size());
375   extension = service->GetExtensionById(
376       "ogjcoiohnmldgjemafoockdghcjciccf", true);
377   ASSERT_TRUE(extension);
378   ASSERT_FALSE(service->GetExtensionById(
379       "ogjcoiohnmldgjemafoockdghcjciccf", false));
380   ASSERT_EQ("2.0", extension->VersionString());
381
382   // The extension should have not made the callback because it is disabled.
383   // When we enabled it, it should then make the callback.
384   ASSERT_FALSE(listener2.was_satisfied());
385   EnableExtension(extension->id());
386   listener2.WaitUntilSatisfied();
387   ASSERT_TRUE(notification_listener.started());
388   ASSERT_TRUE(notification_listener.finished());
389   ASSERT_TRUE(ContainsKey(notification_listener.updates(),
390                           "ogjcoiohnmldgjemafoockdghcjciccf"));
391   notification_listener.Reset();
392 }
393
394 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, ExternalUrlUpdate) {
395   ExtensionService* service = extensions::ExtensionSystem::Get(
396       browser()->profile())->extension_service();
397   const char* kExtensionId = "ogjcoiohnmldgjemafoockdghcjciccf";
398   extensions::ExtensionUpdater::CheckParams params;
399
400   base::FilePath basedir = test_data_dir_.AppendASCII("autoupdate");
401
402   // Note: This interceptor gets requests on the IO thread.
403   content::URLLocalHostRequestPrepackagedInterceptor interceptor;
404   net::URLFetcher::SetEnableInterceptionForTests(true);
405
406   interceptor.SetResponseIgnoreQuery(
407       GURL("http://localhost/autoupdate/manifest"),
408       basedir.AppendASCII("manifest_v2.xml"));
409   interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
410                                      basedir.AppendASCII("v2.crx"));
411
412   const size_t size_before = service->extensions()->size();
413   ASSERT_TRUE(service->disabled_extensions()->is_empty());
414
415   extensions::PendingExtensionManager* pending_extension_manager =
416       service->pending_extension_manager();
417
418   // The code that reads external_extensions.json uses this method to inform
419   // the ExtensionService of an extension to download.  Using the real code
420   // is race-prone, because instantating the ExtensionService starts a read
421   // of external_extensions.json before this test function starts.
422
423   EXPECT_TRUE(pending_extension_manager->AddFromExternalUpdateUrl(
424       kExtensionId, GURL("http://localhost/autoupdate/manifest"),
425       Manifest::EXTERNAL_PREF_DOWNLOAD, Extension::NO_FLAGS, false));
426
427   // Run autoupdate and make sure version 2 of the extension was installed.
428   service->updater()->CheckNow(params);
429   ASSERT_TRUE(WaitForExtensionInstall());
430   ASSERT_EQ(size_before + 1, service->extensions()->size());
431   const Extension* extension = service->GetExtensionById(kExtensionId, false);
432   ASSERT_TRUE(extension);
433   ASSERT_EQ("2.0", extension->VersionString());
434
435   // Uninstalling the extension should set a pref that keeps the extension from
436   // being installed again the next time external_extensions.json is read.
437
438   UninstallExtension(kExtensionId);
439
440   extensions::ExtensionPrefs* extension_prefs = service->extension_prefs();
441   EXPECT_TRUE(extension_prefs->IsExternalExtensionUninstalled(kExtensionId))
442       << "Uninstalling should set kill bit on externaly installed extension.";
443
444   // Try to install the extension again from an external source. It should fail
445   // because of the killbit.
446   EXPECT_FALSE(pending_extension_manager->AddFromExternalUpdateUrl(
447       kExtensionId, GURL("http://localhost/autoupdate/manifest"),
448       Manifest::EXTERNAL_PREF_DOWNLOAD, Extension::NO_FLAGS, false));
449   EXPECT_FALSE(pending_extension_manager->IsIdPending(kExtensionId))
450       << "External reinstall of a killed extension shouldn't work.";
451   EXPECT_TRUE(extension_prefs->IsExternalExtensionUninstalled(kExtensionId))
452       << "External reinstall of a killed extension should leave it killed.";
453
454   // Installing from non-external source.
455   ASSERT_TRUE(InstallExtension(basedir.AppendASCII("v2.crx"), 1));
456
457   EXPECT_FALSE(extension_prefs->IsExternalExtensionUninstalled(kExtensionId))
458       << "Reinstalling should clear the kill bit.";
459
460   // Uninstalling from a non-external source should not set the kill bit.
461   UninstallExtension(kExtensionId);
462
463   EXPECT_FALSE(extension_prefs->IsExternalExtensionUninstalled(kExtensionId))
464       << "Uninstalling non-external extension should not set kill bit.";
465 }
466
467 namespace {
468
469 const char* kForceInstallNotEmptyHelp =
470     "A policy may already be controlling the list of force-installed "
471     "extensions. Please remove all policy settings from your computer "
472     "before running tests. E.g. from /etc/chromium/policies Linux or "
473     "from the registry on Windows, etc.";
474
475 }
476
477 // See http://crbug.com/57378 for flakiness details.
478 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, ExternalPolicyRefresh) {
479   ExtensionService* service = extensions::ExtensionSystem::Get(
480       browser()->profile())->extension_service();
481   const char* kExtensionId = "ogjcoiohnmldgjemafoockdghcjciccf";
482
483   base::FilePath basedir = test_data_dir_.AppendASCII("autoupdate");
484
485   // Note: This interceptor gets requests on the IO thread.
486   content::URLLocalHostRequestPrepackagedInterceptor interceptor;
487   net::URLFetcher::SetEnableInterceptionForTests(true);
488
489   interceptor.SetResponseIgnoreQuery(
490       GURL("http://localhost/autoupdate/manifest"),
491       basedir.AppendASCII("manifest_v2.xml"));
492   interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
493                                      basedir.AppendASCII("v2.crx"));
494
495   const size_t size_before = service->extensions()->size();
496   ASSERT_TRUE(service->disabled_extensions()->is_empty());
497
498   PrefService* prefs = browser()->profile()->GetPrefs();
499   const base::DictionaryValue* forcelist =
500       prefs->GetDictionary(extensions::pref_names::kInstallForceList);
501   ASSERT_TRUE(forcelist->empty()) << kForceInstallNotEmptyHelp;
502
503   {
504     // Set the policy as a user preference and fire notification observers.
505     DictionaryPrefUpdate pref_update(prefs,
506                                      extensions::pref_names::kInstallForceList);
507     base::DictionaryValue* forcelist = pref_update.Get();
508     extensions::ExternalPolicyLoader::AddExtension(
509         forcelist, kExtensionId, "http://localhost/autoupdate/manifest");
510   }
511
512   // Check if the extension got installed.
513   ASSERT_TRUE(WaitForExtensionInstall());
514   ASSERT_EQ(size_before + 1, service->extensions()->size());
515   const Extension* extension = service->GetExtensionById(kExtensionId, false);
516   ASSERT_TRUE(extension);
517   ASSERT_EQ("2.0", extension->VersionString());
518   EXPECT_EQ(Manifest::EXTERNAL_POLICY_DOWNLOAD, extension->location());
519
520   // Try to disable and uninstall the extension which should fail.
521   DisableExtension(kExtensionId);
522   EXPECT_EQ(size_before + 1, service->extensions()->size());
523   EXPECT_EQ(0u, service->disabled_extensions()->size());
524   UninstallExtension(kExtensionId);
525   EXPECT_EQ(size_before + 1, service->extensions()->size());
526   EXPECT_EQ(0u, service->disabled_extensions()->size());
527
528   // Now try to disable it through the management api, again failing.
529   ExtensionTestMessageListener listener1("ready", false);
530   ASSERT_TRUE(LoadExtension(
531       test_data_dir_.AppendASCII("management/uninstall_extension")));
532   ASSERT_TRUE(listener1.WaitUntilSatisfied());
533   EXPECT_EQ(size_before + 2, service->extensions()->size());
534   EXPECT_EQ(0u, service->disabled_extensions()->size());
535
536   // Check that emptying the list triggers uninstall.
537   prefs->ClearPref(extensions::pref_names::kInstallForceList);
538   EXPECT_EQ(size_before + 1, service->extensions()->size());
539   EXPECT_FALSE(service->GetExtensionById(kExtensionId, true));
540 }
541
542 // See http://crbug.com/103371 and http://crbug.com/120640.
543 #if defined(ADDRESS_SANITIZER) || defined(OS_WIN)
544 #define MAYBE_PolicyOverridesUserInstall DISABLED_PolicyOverridesUserInstall
545 #else
546 #define MAYBE_PolicyOverridesUserInstall PolicyOverridesUserInstall
547 #endif
548
549 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest,
550                        MAYBE_PolicyOverridesUserInstall) {
551   ExtensionService* service = extensions::ExtensionSystem::Get(
552       browser()->profile())->extension_service();
553   const char* kExtensionId = "ogjcoiohnmldgjemafoockdghcjciccf";
554   extensions::ExtensionUpdater::CheckParams params;
555   service->updater()->set_default_check_params(params);
556   const size_t size_before = service->extensions()->size();
557   base::FilePath basedir = test_data_dir_.AppendASCII("autoupdate");
558   ASSERT_TRUE(service->disabled_extensions()->is_empty());
559
560   // Note: This interceptor gets requests on the IO thread.
561   content::URLLocalHostRequestPrepackagedInterceptor interceptor;
562   net::URLFetcher::SetEnableInterceptionForTests(true);
563
564   interceptor.SetResponseIgnoreQuery(
565       GURL("http://localhost/autoupdate/manifest"),
566       basedir.AppendASCII("manifest_v2.xml"));
567   interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
568                                      basedir.AppendASCII("v2.crx"));
569
570   // Check that the policy is initially empty.
571   PrefService* prefs = browser()->profile()->GetPrefs();
572   const base::DictionaryValue* forcelist =
573       prefs->GetDictionary(extensions::pref_names::kInstallForceList);
574   ASSERT_TRUE(forcelist->empty()) << kForceInstallNotEmptyHelp;
575
576   // User install of the extension.
577   ASSERT_TRUE(InstallExtension(basedir.AppendASCII("v2.crx"), 1));
578   ASSERT_EQ(size_before + 1, service->extensions()->size());
579   const Extension* extension = service->GetExtensionById(kExtensionId, false);
580   ASSERT_TRUE(extension);
581   EXPECT_EQ(Manifest::INTERNAL, extension->location());
582   EXPECT_TRUE(service->IsExtensionEnabled(kExtensionId));
583
584   // Setup the force install policy. It should override the location.
585   {
586     DictionaryPrefUpdate pref_update(prefs,
587                                      extensions::pref_names::kInstallForceList);
588     extensions::ExternalPolicyLoader::AddExtension(
589         pref_update.Get(), kExtensionId,
590         "http://localhost/autoupdate/manifest");
591   }
592   ASSERT_TRUE(WaitForExtensionInstall());
593   ASSERT_EQ(size_before + 1, service->extensions()->size());
594   extension = service->GetExtensionById(kExtensionId, false);
595   ASSERT_TRUE(extension);
596   EXPECT_EQ(Manifest::EXTERNAL_POLICY_DOWNLOAD, extension->location());
597   EXPECT_TRUE(service->IsExtensionEnabled(kExtensionId));
598
599   // Remove the policy, and verify that the extension was uninstalled.
600   // TODO(joaodasilva): it would be nicer if the extension was kept instead,
601   // and reverted location to INTERNAL or whatever it was before the policy
602   // was applied.
603   prefs->ClearPref(extensions::pref_names::kInstallForceList);
604   ASSERT_EQ(size_before, service->extensions()->size());
605   extension = service->GetExtensionById(kExtensionId, true);
606   EXPECT_FALSE(extension);
607
608   // User install again, but have it disabled too before setting the policy.
609   ASSERT_TRUE(InstallExtension(basedir.AppendASCII("v2.crx"), 1));
610   ASSERT_EQ(size_before + 1, service->extensions()->size());
611   extension = service->GetExtensionById(kExtensionId, false);
612   ASSERT_TRUE(extension);
613   EXPECT_EQ(Manifest::INTERNAL, extension->location());
614   EXPECT_TRUE(service->IsExtensionEnabled(kExtensionId));
615   EXPECT_TRUE(service->disabled_extensions()->is_empty());
616
617   DisableExtension(kExtensionId);
618   EXPECT_EQ(1u, service->disabled_extensions()->size());
619   extension = service->GetExtensionById(kExtensionId, true);
620   EXPECT_TRUE(extension);
621   EXPECT_FALSE(service->IsExtensionEnabled(kExtensionId));
622
623   // Install the policy again. It should overwrite the extension's location,
624   // and force enable it too.
625   {
626     DictionaryPrefUpdate pref_update(prefs,
627                                      extensions::pref_names::kInstallForceList);
628     base::DictionaryValue* forcelist = pref_update.Get();
629     extensions::ExternalPolicyLoader::AddExtension(
630         forcelist, kExtensionId, "http://localhost/autoupdate/manifest");
631   }
632   ASSERT_TRUE(WaitForExtensionInstall());
633   ASSERT_EQ(size_before + 1, service->extensions()->size());
634   extension = service->GetExtensionById(kExtensionId, false);
635   ASSERT_TRUE(extension);
636   EXPECT_EQ(Manifest::EXTERNAL_POLICY_DOWNLOAD, extension->location());
637   EXPECT_TRUE(service->IsExtensionEnabled(kExtensionId));
638   EXPECT_TRUE(service->disabled_extensions()->is_empty());
639 }