| OLD | NEW |
| 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 "chrome/browser/extensions/extension_management_api.h" | 5 #include "chrome/browser/extensions/extension_management_api.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
| 15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 16 #include "chrome/browser/extensions/extension_event_names.h" | 16 #include "chrome/browser/extensions/extension_event_names.h" |
| 17 #include "chrome/browser/extensions/extension_event_router.h" | 17 #include "chrome/browser/extensions/extension_event_router.h" |
| 18 #include "chrome/browser/extensions/extension_management_api_constants.h" | 18 #include "chrome/browser/extensions/extension_management_api_constants.h" |
| 19 #include "chrome/browser/extensions/extension_service.h" | 19 #include "chrome/browser/extensions/extension_service.h" |
| 20 #include "chrome/browser/extensions/extension_system.h" |
| 21 #include "chrome/browser/extensions/management_policy.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/ui/browser.h" | 23 #include "chrome/browser/ui/browser.h" |
| 22 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" | 24 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" |
| 23 #include "chrome/common/chrome_notification_types.h" | 25 #include "chrome/common/chrome_notification_types.h" |
| 24 #include "chrome/common/chrome_utility_messages.h" | 26 #include "chrome/common/chrome_utility_messages.h" |
| 25 #include "chrome/common/extensions/extension.h" | 27 #include "chrome/common/extensions/extension.h" |
| 26 #include "chrome/common/extensions/extension_constants.h" | 28 #include "chrome/common/extensions/extension_constants.h" |
| 27 #include "chrome/common/extensions/extension_error_utils.h" | 29 #include "chrome/common/extensions/extension_error_utils.h" |
| 28 #include "chrome/common/extensions/extension_icon_set.h" | 30 #include "chrome/common/extensions/extension_icon_set.h" |
| 29 #include "chrome/common/extensions/url_pattern.h" | 31 #include "chrome/common/extensions/url_pattern.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 52 ExtensionService* AsyncExtensionManagementFunction::service() { | 54 ExtensionService* AsyncExtensionManagementFunction::service() { |
| 53 return profile()->GetExtensionService(); | 55 return profile()->GetExtensionService(); |
| 54 } | 56 } |
| 55 | 57 |
| 56 static DictionaryValue* CreateExtensionInfo(const Extension& extension, | 58 static DictionaryValue* CreateExtensionInfo(const Extension& extension, |
| 57 ExtensionService* service) { | 59 ExtensionService* service) { |
| 58 DictionaryValue* info = new DictionaryValue(); | 60 DictionaryValue* info = new DictionaryValue(); |
| 59 bool enabled = service->IsExtensionEnabled(extension.id()); | 61 bool enabled = service->IsExtensionEnabled(extension.id()); |
| 60 extension.GetBasicInfo(enabled, info); | 62 extension.GetBasicInfo(enabled, info); |
| 61 | 63 |
| 64 const extensions::ManagementPolicy* policy = ExtensionSystem::Get( |
| 65 service->profile())->management_policy(); |
| 66 info->SetBoolean(keys::kMayDisableKey, |
| 67 policy->UserMayModifySettings(&extension, NULL)); |
| 68 |
| 62 info->SetBoolean(keys::kIsAppKey, extension.is_app()); | 69 info->SetBoolean(keys::kIsAppKey, extension.is_app()); |
| 63 | 70 |
| 64 if (!enabled) { | 71 if (!enabled) { |
| 65 bool permissions_escalated = service->extension_prefs()-> | 72 ExtensionPrefs* prefs = service->extension_prefs(); |
| 66 DidExtensionEscalatePermissions(extension.id()); | 73 bool permissions_escalated = |
| 74 prefs->DidExtensionEscalatePermissions(extension.id()); |
| 67 const char* reason = permissions_escalated ? | 75 const char* reason = permissions_escalated ? |
| 68 keys::kDisabledReasonPermissionsIncrease : keys::kDisabledReasonUnknown; | 76 keys::kDisabledReasonPermissionsIncrease : keys::kDisabledReasonUnknown; |
| 69 info->SetString(keys::kDisabledReasonKey, reason); | 77 info->SetString(keys::kDisabledReasonKey, reason); |
| 70 } | 78 } |
| 71 | 79 |
| 72 if (!extension.update_url().is_empty()) | 80 if (!extension.update_url().is_empty()) |
| 73 info->SetString(keys::kUpdateUrlKey, | 81 info->SetString(keys::kUpdateUrlKey, |
| 74 extension.update_url().possibly_invalid_spec()); | 82 extension.update_url().possibly_invalid_spec()); |
| 75 if (extension.is_app()) | 83 if (extension.is_app()) |
| 76 info->SetString(keys::kAppLaunchUrlKey, | 84 info->SetString(keys::kAppLaunchUrlKey, |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 return true; | 171 return true; |
| 164 } | 172 } |
| 165 | 173 |
| 166 bool GetPermissionWarningsByIdFunction::RunImpl() { | 174 bool GetPermissionWarningsByIdFunction::RunImpl() { |
| 167 std::string ext_id; | 175 std::string ext_id; |
| 168 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &ext_id)); | 176 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &ext_id)); |
| 169 | 177 |
| 170 const Extension* extension = service()->GetExtensionById(ext_id, true); | 178 const Extension* extension = service()->GetExtensionById(ext_id, true); |
| 171 if (!extension) { | 179 if (!extension) { |
| 172 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kNoExtensionError, | 180 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kNoExtensionError, |
| 173 ext_id); | 181 ext_id); |
| 174 return false; | 182 return false; |
| 175 } | 183 } |
| 176 | 184 |
| 177 ExtensionPermissionMessages warnings = extension->GetPermissionMessages(); | 185 ExtensionPermissionMessages warnings = extension->GetPermissionMessages(); |
| 178 ListValue* result = new ListValue(); | 186 ListValue* result = new ListValue(); |
| 179 for (ExtensionPermissionMessages::const_iterator i = warnings.begin(); | 187 for (ExtensionPermissionMessages::const_iterator i = warnings.begin(); |
| 180 i < warnings.end(); ++i) | 188 i < warnings.end(); ++i) |
| 181 result->Append(Value::CreateStringValue(i->message())); | 189 result->Append(Value::CreateStringValue(i->message())); |
| 182 result_.reset(result); | 190 result_.reset(result); |
| 183 return true; | 191 return true; |
| 184 } | 192 } |
| 185 | 193 |
| 186 namespace { | 194 namespace { |
| 187 | 195 |
| 188 // This class helps GetPermissionWarningsByManifestFunction manage | 196 // This class helps GetPermissionWarningsByManifestFunction manage |
| 189 // sending manifest JSON strings to the utility process for parsing. | 197 // sending manifest JSON strings to the utility process for parsing. |
| 190 class SafeManifestJSONParser : public UtilityProcessHostClient { | 198 class SafeManifestJSONParser : public UtilityProcessHostClient { |
| 191 public: | 199 public: |
| 192 SafeManifestJSONParser(GetPermissionWarningsByManifestFunction* client, | 200 SafeManifestJSONParser(GetPermissionWarningsByManifestFunction* client, |
| 193 const std::string& manifest) | 201 const std::string& manifest) |
| 194 : client_(client), | 202 : client_(client), |
| 195 manifest_(manifest) {} | 203 manifest_(manifest) {} |
| 196 | 204 |
| 197 void Start() { | 205 void Start() { |
| 198 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 206 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 199 BrowserThread::PostTask( | 207 BrowserThread::PostTask( |
| 200 BrowserThread::IO, | 208 BrowserThread::IO, |
| 201 FROM_HERE, | 209 FROM_HERE, |
| 202 base::Bind(&SafeManifestJSONParser::StartWorkOnIOThread, this)); | 210 base::Bind(&SafeManifestJSONParser::StartWorkOnIOThread, this)); |
| 203 } | 211 } |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &extension_id_)); | 367 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &extension_id_)); |
| 360 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(1, &enable)); | 368 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(1, &enable)); |
| 361 | 369 |
| 362 const Extension* extension = service()->GetExtensionById(extension_id_, true); | 370 const Extension* extension = service()->GetExtensionById(extension_id_, true); |
| 363 if (!extension) { | 371 if (!extension) { |
| 364 error_ = ExtensionErrorUtils::FormatErrorMessage( | 372 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 365 keys::kNoExtensionError, extension_id_); | 373 keys::kNoExtensionError, extension_id_); |
| 366 return false; | 374 return false; |
| 367 } | 375 } |
| 368 | 376 |
| 369 if (!Extension::UserMayDisable(extension->location())) { | 377 const extensions::ManagementPolicy* policy = ExtensionSystem::Get( |
| 378 profile())->management_policy(); |
| 379 if (!policy->UserMayModifySettings(extension, NULL)) { |
| 370 error_ = ExtensionErrorUtils::FormatErrorMessage( | 380 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 371 keys::kUserCantDisableError, extension_id_); | 381 keys::kUserCantModifyError, extension_id_); |
| 372 return false; | 382 return false; |
| 373 } | 383 } |
| 374 | 384 |
| 375 bool currently_enabled = service()->IsExtensionEnabled(extension_id_); | 385 bool currently_enabled = service()->IsExtensionEnabled(extension_id_); |
| 376 | 386 |
| 377 if (!currently_enabled && enable) { | 387 if (!currently_enabled && enable) { |
| 378 ExtensionPrefs* prefs = service()->extension_prefs(); | 388 ExtensionPrefs* prefs = service()->extension_prefs(); |
| 379 if (prefs->DidExtensionEscalatePermissions(extension_id_)) { | 389 if (prefs->DidExtensionEscalatePermissions(extension_id_)) { |
| 380 if (!user_gesture()) { | 390 if (!user_gesture()) { |
| 381 error_ = keys::kGestureNeededForEscalationError; | 391 error_ = keys::kGestureNeededForEscalationError; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 bool UninstallFunction::RunImpl() { | 424 bool UninstallFunction::RunImpl() { |
| 415 std::string extension_id; | 425 std::string extension_id; |
| 416 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &extension_id)); | 426 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &extension_id)); |
| 417 | 427 |
| 418 if (!service()->GetExtensionById(extension_id, true)) { | 428 if (!service()->GetExtensionById(extension_id, true)) { |
| 419 error_ = ExtensionErrorUtils::FormatErrorMessage( | 429 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 420 keys::kNoExtensionError, extension_id); | 430 keys::kNoExtensionError, extension_id); |
| 421 return false; | 431 return false; |
| 422 } | 432 } |
| 423 | 433 |
| 424 ExtensionPrefs* prefs = service()->extension_prefs(); | 434 const Extension* extension = service()->GetExtensionById(extension_id, true); |
| 425 | 435 if (!ExtensionSystem::Get( |
| 426 if (!Extension::UserMayDisable( | 436 profile())->management_policy()->UserMayModifySettings(extension, NULL)) { |
| 427 prefs->GetInstalledExtensionInfo(extension_id)->extension_location)) { | |
| 428 error_ = ExtensionErrorUtils::FormatErrorMessage( | 437 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 429 keys::kUserCantDisableError, extension_id); | 438 keys::kUserCantModifyError, extension_id); |
| 430 return false; | 439 return false; |
| 431 } | 440 } |
| 432 | 441 |
| 433 service()->UninstallExtension(extension_id, false /* external_uninstall */, | 442 service()->UninstallExtension(extension_id, false /* external_uninstall */, |
| 434 NULL); | 443 NULL); |
| 435 return true; | 444 return true; |
| 436 } | 445 } |
| 437 | 446 |
| 438 ExtensionManagementEventRouter::ExtensionManagementEventRouter(Profile* profile) | 447 ExtensionManagementEventRouter::ExtensionManagementEventRouter(Profile* profile) |
| 439 : profile_(profile) {} | 448 : profile_(profile) {} |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 ExtensionService* service = profile->GetExtensionService(); | 508 ExtensionService* service = profile->GetExtensionService(); |
| 500 args.Append(CreateExtensionInfo(*extension, service)); | 509 args.Append(CreateExtensionInfo(*extension, service)); |
| 501 } | 510 } |
| 502 | 511 |
| 503 std::string args_json; | 512 std::string args_json; |
| 504 base::JSONWriter::Write(&args, &args_json); | 513 base::JSONWriter::Write(&args, &args_json); |
| 505 | 514 |
| 506 profile->GetExtensionEventRouter()->DispatchEventToRenderers( | 515 profile->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 507 event_name, args_json, NULL, GURL()); | 516 event_name, args_json, NULL, GURL()); |
| 508 } | 517 } |
| OLD | NEW |