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

Side by Side Diff: chrome/browser/extensions/api/management/management_api_browsertest.cc

Issue 10832237: Allow chrome.management.setEnabled() to re-enable crashed extensions. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Navigate to about:crash instead of using base::KillProcess Created 8 years, 3 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 "base/command_line.h" 5 #include "base/command_line.h"
6 #include "base/file_path.h" 6 #include "base/file_path.h"
7 #include "base/scoped_temp_dir.h" 7 #include "base/scoped_temp_dir.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/stringprintf.h" 9 #include "base/stringprintf.h"
10 #include "chrome/browser/extensions/api/management/management_api.h" 10 #include "chrome/browser/extensions/api/management/management_api.h"
11 #include "chrome/browser/extensions/api/management/management_api_constants.h" 11 #include "chrome/browser/extensions/api/management/management_api_constants.h"
12 #include "chrome/browser/extensions/extension_browsertest.h" 12 #include "chrome/browser/extensions/extension_browsertest.h"
13 #include "chrome/browser/extensions/extension_function_test_utils.h" 13 #include "chrome/browser/extensions/extension_function_test_utils.h"
14 #include "chrome/browser/extensions/extension_host.h"
14 #include "chrome/browser/extensions/extension_install_dialog.h" 15 #include "chrome/browser/extensions/extension_install_dialog.h"
15 #include "chrome/browser/extensions/extension_service.h" 16 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/extensions/extension_test_message_listener.h" 17 #include "chrome/browser/extensions/extension_test_message_listener.h"
17 #include "chrome/browser/profiles/profile.h" 18 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/browser.h" 19 #include "chrome/browser/ui/browser.h"
19 #include "chrome/common/chrome_notification_types.h" 20 #include "chrome/common/chrome_notification_types.h"
20 #include "chrome/common/chrome_switches.h" 21 #include "chrome/common/chrome_switches.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/common/url_constants.h"
24 #include "content/public/test/test_utils.h"
21 25
22 namespace keys = extension_management_api_constants; 26 namespace keys = extension_management_api_constants;
23 namespace util = extension_function_test_utils; 27 namespace util = extension_function_test_utils;
24 28
25 class ExtensionManagementApiBrowserTest : public ExtensionBrowserTest {}; 29 class ExtensionManagementApiBrowserTest : public ExtensionBrowserTest {};
26 30
27 // We test this here instead of in an ExtensionApiTest because normal extensions 31 // We test this here instead of in an ExtensionApiTest because normal extensions
28 // are not allowed to call the install function. 32 // are not allowed to call the install function.
29 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, InstallEvent) { 33 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, InstallEvent) {
30 ExtensionTestMessageListener listener1("ready", false); 34 ExtensionTestMessageListener listener1("ready", false);
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 EXPECT_TRUE(service->GetExtensionById(kId, false) != NULL); 141 EXPECT_TRUE(service->GetExtensionById(kId, false) != NULL);
138 142
139 // Update to a high-permission version - it should get disabled. 143 // Update to a high-permission version - it should get disabled.
140 EXPECT_FALSE(UpdateExtension(kId, path_v2, -1)); 144 EXPECT_FALSE(UpdateExtension(kId, path_v2, -1));
141 EXPECT_TRUE(service->GetExtensionById(kId, false) == NULL); 145 EXPECT_TRUE(service->GetExtensionById(kId, false) == NULL);
142 EXPECT_TRUE(service->GetExtensionById(kId, true) != NULL); 146 EXPECT_TRUE(service->GetExtensionById(kId, true) != NULL);
143 EXPECT_TRUE( 147 EXPECT_TRUE(
144 service->extension_prefs()->DidExtensionEscalatePermissions(kId)); 148 service->extension_prefs()->DidExtensionEscalatePermissions(kId));
145 } 149 }
146 150
147 void ReEnable(bool user_gesture, const std::string& expected_error) { 151 void SetEnabled(bool enabled, bool user_gesture,
152 const std::string& expected_error) {
148 scoped_refptr<SetEnabledFunction> function(new SetEnabledFunction); 153 scoped_refptr<SetEnabledFunction> function(new SetEnabledFunction);
154 const char* enabled_string = enabled ? "true" : "false";
149 if (user_gesture) 155 if (user_gesture)
150 function->set_user_gesture(true); 156 function->set_user_gesture(true);
151 bool response = util::RunFunction( 157 bool response = util::RunFunction(
152 function.get(), 158 function.get(),
153 base::StringPrintf("[\"%s\", true]", kId), 159 base::StringPrintf("[\"%s\", %s]", kId, enabled_string),
154 browser(), 160 browser(),
155 util::NONE); 161 util::NONE);
156 if (expected_error.empty()) { 162 if (expected_error.empty()) {
157 EXPECT_EQ(true, response); 163 EXPECT_EQ(true, response);
158 } else { 164 } else {
159 EXPECT_TRUE(response == false); 165 EXPECT_TRUE(response == false);
160 EXPECT_EQ(expected_error, function->GetError()); 166 EXPECT_EQ(expected_error, function->GetError());
161 } 167 }
162 } 168 }
163 169
170 bool CrashEnabledExtension(const std::string& extension_id) {
171 content::WindowedNotificationObserver extension_crash_observer(
172 chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
173 content::NotificationService::AllSources());
174 extensions::ExtensionHost* background_host =
175 browser()->profile()->GetExtensionProcessManager()->
176 GetBackgroundHostForExtension(extension_id);
177 if (!background_host)
178 return false;
179 background_host->host_contents()->GetController().LoadURL(
180 GURL(chrome::kChromeUICrashURL), content::Referrer(),
181 content::PAGE_TRANSITION_LINK, std::string());
182 extension_crash_observer.Wait();
183 return true;
184 }
185
164 private: 186 private:
165 ScopedTempDir scoped_temp_dir_; 187 ScopedTempDir scoped_temp_dir_;
166 }; 188 };
167 189
168 const char ExtensionManagementApiEscalationTest::kId[] = 190 const char ExtensionManagementApiEscalationTest::kId[] =
169 "pgdpcfcocojkjfbgpiianjngphoopgmo"; 191 "pgdpcfcocojkjfbgpiianjngphoopgmo";
170 192
171 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiEscalationTest, 193 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiEscalationTest,
172 DisabledReason) { 194 DisabledReason) {
173 scoped_refptr<GetExtensionByIdFunction> function = 195 scoped_refptr<GetExtensionByIdFunction> function =
174 new GetExtensionByIdFunction(); 196 new GetExtensionByIdFunction();
175 scoped_ptr<base::Value> result(util::RunFunctionAndReturnSingleResult( 197 scoped_ptr<base::Value> result(util::RunFunctionAndReturnSingleResult(
176 function.get(), 198 function.get(),
177 base::StringPrintf("[\"%s\"]", kId), 199 base::StringPrintf("[\"%s\"]", kId),
178 browser())); 200 browser()));
179 ASSERT_TRUE(result.get() != NULL); 201 ASSERT_TRUE(result.get() != NULL);
180 ASSERT_TRUE(result->IsType(base::Value::TYPE_DICTIONARY)); 202 ASSERT_TRUE(result->IsType(base::Value::TYPE_DICTIONARY));
181 base::DictionaryValue* dict = 203 base::DictionaryValue* dict =
182 static_cast<base::DictionaryValue*>(result.get()); 204 static_cast<base::DictionaryValue*>(result.get());
183 std::string reason; 205 std::string reason;
184 EXPECT_TRUE(dict->GetStringASCII(keys::kDisabledReasonKey, &reason)); 206 EXPECT_TRUE(dict->GetStringASCII(keys::kDisabledReasonKey, &reason));
185 EXPECT_EQ(reason, std::string(keys::kDisabledReasonPermissionsIncrease)); 207 EXPECT_EQ(reason, std::string(keys::kDisabledReasonPermissionsIncrease));
186 } 208 }
187 209
188 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiEscalationTest, 210 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiEscalationTest,
189 ReEnable) { 211 SetEnabled) {
190 // Expect an error about no gesture. 212 // Expect an error about no gesture.
191 ReEnable(false, keys::kGestureNeededForEscalationError); 213 SetEnabled(true, false, keys::kGestureNeededForEscalationError);
192 214
193 // Expect an error that user cancelled the dialog. 215 // Expect an error that user cancelled the dialog.
194 CommandLine::ForCurrentProcess()->AppendSwitchASCII( 216 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
195 switches::kAppsGalleryInstallAutoConfirmForTests, "cancel"); 217 switches::kAppsGalleryInstallAutoConfirmForTests, "cancel");
196 ReEnable(true, keys::kUserDidNotReEnableError); 218 SetEnabled(true, true, keys::kUserDidNotReEnableError);
197 219
198 // This should succeed when user accepts dialog. 220 // This should succeed when user accepts dialog.
199 CommandLine::ForCurrentProcess()->AppendSwitchASCII( 221 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
200 switches::kAppsGalleryInstallAutoConfirmForTests, "accept"); 222 switches::kAppsGalleryInstallAutoConfirmForTests, "accept");
201 ReEnable(true, ""); 223 SetEnabled(true, true, "");
224
225 // Crash the extension and try to enable. The extension should be reloaded and
226 // enabled.
227 ASSERT_TRUE(CrashEnabledExtension(kId));
228 SetEnabled(true, true, "");
229
230 // Crash the extension again and try to disable. This call should be a no-op
231 // and the extension should remain unloaded.
232 ASSERT_TRUE(CrashEnabledExtension(kId));
233 SetEnabled(false, true, "");
234 const extensions::Extension* extension = browser()->profile()->
235 GetExtensionService()->GetTerminatedExtension(kId);
236 ASSERT_TRUE(extension);
202 } 237 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698