Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_webstore_private_api.h" | 5 #include "chrome/browser/extensions/extension_webstore_private_api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 66 bool IsWebStoreURL(Profile* profile, const GURL& url) { | 66 bool IsWebStoreURL(Profile* profile, const GURL& url) { |
| 67 ExtensionService* service = profile->GetExtensionService(); | 67 ExtensionService* service = profile->GetExtensionService(); |
| 68 const Extension* store = service->GetWebStoreApp(); | 68 const Extension* store = service->GetWebStoreApp(); |
| 69 if (!store) { | 69 if (!store) { |
| 70 NOTREACHED(); | 70 NOTREACHED(); |
| 71 return false; | 71 return false; |
| 72 } | 72 } |
| 73 return (service->GetExtensionByWebExtent(url) == store); | 73 return (service->GetExtensionByWebExtent(url) == store); |
| 74 } | 74 } |
| 75 | 75 |
| 76 // Whitelists extension IDs for use by webstorePrivate.silentlyInstall. | |
| 77 const char* test_trusted_id = NULL; | |
| 78 | |
| 79 bool IsTrustedForSilentInstall(const std::string& id) { | |
| 80 return (test_trusted_id && id == std::string(test_trusted_id)) || | |
| 81 id == std::string("jgoepmocgafhnchmokaimcmlojpnlkhp") || | |
|
Aaron Boodman
2011/11/01 17:24:34
Nit: I don't think you need the std::string wrappe
jstritar
2011/11/01 19:43:05
Done.
| |
| 82 id == std::string("cpembckmhnjipbgbnfiocbgnkpjdokdd") || | |
| 83 id == std::string("boemmnepglcoinjcdlfcpcbmhiecichi") || | |
| 84 id == std::string("flibmgiapaohcbondaoopaalfejliklp") || | |
| 85 id == std::string("dlppkpafhbajpcmmoheippocdidnckmm") || | |
| 86 id == std::string("hmglfmpefabcafaimbpldpambdfomanl") || | |
| 87 id == std::string("idfijlieiecpfcjckpkliefekpokhhnd") || | |
| 88 id == std::string("jaokjbijaokooelpahnlmbciccldmfla") || | |
| 89 id == std::string("kdjeommiakphmeionoojjljlecmpaldd") || | |
| 90 id == std::string("lpdeojkfhenboeibhkjhiancceeboknd"); | |
| 91 } | |
| 92 | |
| 76 // Helper to create a dictionary with login and token properties set from | 93 // Helper to create a dictionary with login and token properties set from |
| 77 // the appropriate values in the passed-in |profile|. | 94 // the appropriate values in the passed-in |profile|. |
| 78 DictionaryValue* CreateLoginResult(Profile* profile) { | 95 DictionaryValue* CreateLoginResult(Profile* profile) { |
| 79 DictionaryValue* dictionary = new DictionaryValue(); | 96 DictionaryValue* dictionary = new DictionaryValue(); |
| 80 std::string username = profile->GetPrefs()->GetString( | 97 std::string username = profile->GetPrefs()->GetString( |
| 81 prefs::kGoogleServicesUsername); | 98 prefs::kGoogleServicesUsername); |
| 82 dictionary->SetString(kLoginKey, username); | 99 dictionary->SetString(kLoginKey, username); |
| 83 if (!username.empty()) { | 100 if (!username.empty()) { |
| 84 CommandLine* cmdline = CommandLine::ForCurrentProcess(); | 101 CommandLine* cmdline = CommandLine::ForCurrentProcess(); |
| 85 TokenService* token_service = profile->GetTokenService(); | 102 TokenService* token_service = profile->GetTokenService(); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 102 ProfileSyncService* service) { | 119 ProfileSyncService* service) { |
| 103 test_sync_service = service; | 120 test_sync_service = service; |
| 104 } | 121 } |
| 105 | 122 |
| 106 // static | 123 // static |
| 107 void WebstorePrivateApi::SetWebstoreInstallerDelegateForTesting( | 124 void WebstorePrivateApi::SetWebstoreInstallerDelegateForTesting( |
| 108 WebstoreInstaller::Delegate* delegate) { | 125 WebstoreInstaller::Delegate* delegate) { |
| 109 test_webstore_installer_delegate = delegate; | 126 test_webstore_installer_delegate = delegate; |
| 110 } | 127 } |
| 111 | 128 |
| 129 // static | |
| 130 void WebstorePrivateApi::SetTrustedIDForTesting(const char* extension_id) { | |
| 131 test_trusted_id = extension_id; | |
| 132 } | |
| 133 | |
| 112 BeginInstallWithManifestFunction::BeginInstallWithManifestFunction() | 134 BeginInstallWithManifestFunction::BeginInstallWithManifestFunction() |
| 113 : use_app_installed_bubble_(false) {} | 135 : use_app_installed_bubble_(false) {} |
| 114 | 136 |
| 115 BeginInstallWithManifestFunction::~BeginInstallWithManifestFunction() {} | 137 BeginInstallWithManifestFunction::~BeginInstallWithManifestFunction() {} |
| 116 | 138 |
| 117 bool BeginInstallWithManifestFunction::RunImpl() { | 139 bool BeginInstallWithManifestFunction::RunImpl() { |
| 118 if (!IsWebStoreURL(profile_, source_url())) { | 140 if (!IsWebStoreURL(profile_, source_url())) { |
| 119 SetResult(PERMISSION_DENIED); | 141 SetResult(PERMISSION_DENIED); |
| 120 return false; | 142 return false; |
| 121 } | 143 } |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 338 // permissions install dialog. | 360 // permissions install dialog. |
| 339 scoped_refptr<WebstoreInstaller> installer = new WebstoreInstaller( | 361 scoped_refptr<WebstoreInstaller> installer = new WebstoreInstaller( |
| 340 profile(), test_webstore_installer_delegate, | 362 profile(), test_webstore_installer_delegate, |
| 341 &(dispatcher()->delegate()->GetAssociatedTabContents()->controller()), | 363 &(dispatcher()->delegate()->GetAssociatedTabContents()->controller()), |
| 342 id, WebstoreInstaller::FLAG_NONE); | 364 id, WebstoreInstaller::FLAG_NONE); |
| 343 installer->Start(); | 365 installer->Start(); |
| 344 | 366 |
| 345 return true; | 367 return true; |
| 346 } | 368 } |
| 347 | 369 |
| 370 SilentlyInstallFunction::SilentlyInstallFunction() {} | |
| 371 SilentlyInstallFunction::~SilentlyInstallFunction() {} | |
| 372 | |
| 373 bool SilentlyInstallFunction::RunImpl() { | |
| 374 if (!IsWebStoreURL(profile_, source_url())) { | |
| 375 SetResult(PERMISSION_DENIED); | |
| 376 return false; | |
| 377 } | |
| 378 | |
| 379 DictionaryValue* details = NULL; | |
| 380 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); | |
| 381 CHECK(details); | |
| 382 | |
| 383 EXTENSION_FUNCTION_VALIDATE(details->GetString(kIdKey, &id_)); | |
| 384 if (!IsTrustedForSilentInstall(id_)) { | |
| 385 SetResult(INVALID_ID); | |
| 386 error_ = kInvalidIdError; | |
| 387 return false; | |
| 388 } | |
| 389 | |
| 390 EXTENSION_FUNCTION_VALIDATE(details->GetString(kManifestKey, &manifest_)); | |
| 391 | |
| 392 // Matched in OnWebstoreParseFailure, OnExtensionInstall{Success,Failure}. | |
| 393 AddRef(); | |
| 394 | |
| 395 scoped_refptr<WebstoreInstallHelper> helper = new WebstoreInstallHelper( | |
| 396 this, id_, manifest_, std::string(), GURL(), NULL); | |
| 397 helper->Start(); | |
| 398 | |
| 399 return true; | |
| 400 } | |
| 401 | |
| 402 void SilentlyInstallFunction::OnWebstoreParseSuccess( | |
| 403 const std::string& id, | |
| 404 const SkBitmap& icon, | |
| 405 base::DictionaryValue* parsed_manifest) { | |
| 406 CHECK_EQ(id_, id); | |
| 407 | |
| 408 // This lets CrxInstaller bypass the permission confirmation UI for the | |
| 409 // extension. The whitelist entry gets cleared in | |
| 410 // CrxInstaller::ConfirmInstall. | |
| 411 CrxInstaller::WhitelistEntry* entry = new CrxInstaller::WhitelistEntry; | |
| 412 entry->parsed_manifest.reset(parsed_manifest); | |
| 413 entry->use_app_installed_bubble = false; | |
| 414 entry->skip_post_install_ui = true; | |
| 415 CrxInstaller::SetWhitelistEntry(id_, entry); | |
| 416 | |
| 417 scoped_refptr<WebstoreInstaller> installer = new WebstoreInstaller( | |
| 418 profile(), this, | |
| 419 &(dispatcher()->delegate()->GetAssociatedTabContents()->controller()), | |
| 420 id_, WebstoreInstaller::FLAG_NONE); | |
| 421 installer->Start(); | |
| 422 } | |
| 423 | |
| 424 void SilentlyInstallFunction::OnWebstoreParseFailure( | |
| 425 const std::string& id, | |
| 426 InstallHelperResultCode result_code, | |
| 427 const std::string& error_message) { | |
| 428 CHECK_EQ(id_, id); | |
| 429 | |
| 430 // Map from WebstoreInstallHelper's result codes to ours. | |
| 431 if (WebstoreInstallHelper::Delegate::MANIFEST_ERROR) | |
|
Aaron Boodman
2011/11/01 17:24:34
I think this will always be true.
jstritar
2011/11/01 19:43:05
Oops, I meant to compare that to result_code. Done
| |
| 432 SetResult(MANIFEST_ERROR); | |
| 433 else | |
| 434 SetResult(UNKNOWN_ERROR); | |
| 435 | |
| 436 error_ = error_message; | |
| 437 SendResponse(false); | |
| 438 | |
| 439 Release(); // Matches the AddRef() in RunImpl(). | |
| 440 } | |
| 441 | |
| 442 void SilentlyInstallFunction::OnExtensionInstallSuccess(const std::string& id) { | |
| 443 CHECK_EQ(id_, id); | |
| 444 SetResult(ERROR_NONE); | |
| 445 SendResponse(true); | |
| 446 | |
| 447 Release(); // Matches the AddRef() in RunImpl(). | |
| 448 } | |
| 449 | |
| 450 void SilentlyInstallFunction::OnExtensionInstallFailure( | |
| 451 const std::string& id, const std::string& error) { | |
| 452 CHECK_EQ(id_, id); | |
| 453 SetResult(UNKNOWN_ERROR); | |
| 454 error_ = error; | |
| 455 SendResponse(false); | |
| 456 | |
| 457 Release(); // Matches the AddRef() in RunImpl(). | |
| 458 } | |
| 459 | |
| 460 void SilentlyInstallFunction::SetResult(ResultCode code) { | |
| 461 switch (code) { | |
| 462 case ERROR_NONE: | |
| 463 result_.reset(Value::CreateStringValue("")); | |
| 464 break; | |
| 465 case UNKNOWN_ERROR: | |
| 466 result_.reset(Value::CreateStringValue("unknown_error")); | |
|
Aaron Boodman
2011/11/01 17:24:34
We don't usually send both a result and an error.
jstritar
2011/11/01 19:43:05
I did this to be consistent with BeginInstallWithM
Aaron Boodman
2011/11/02 04:16:55
Less code sgtm
| |
| 467 break; | |
| 468 case INVALID_ID: | |
| 469 result_.reset(Value::CreateStringValue("invalid_id")); | |
| 470 break; | |
| 471 case PERMISSION_DENIED: | |
| 472 result_.reset(Value::CreateStringValue("permission_denied")); | |
| 473 break; | |
| 474 default: | |
| 475 CHECK(false); | |
| 476 } | |
| 477 } | |
| 478 | |
| 348 bool GetBrowserLoginFunction::RunImpl() { | 479 bool GetBrowserLoginFunction::RunImpl() { |
| 349 if (!IsWebStoreURL(profile_, source_url())) | 480 if (!IsWebStoreURL(profile_, source_url())) |
| 350 return false; | 481 return false; |
| 351 result_.reset(CreateLoginResult(profile_->GetOriginalProfile())); | 482 result_.reset(CreateLoginResult(profile_->GetOriginalProfile())); |
| 352 return true; | 483 return true; |
| 353 } | 484 } |
| 354 | 485 |
| 355 bool GetStoreLoginFunction::RunImpl() { | 486 bool GetStoreLoginFunction::RunImpl() { |
| 356 if (!IsWebStoreURL(profile_, source_url())) | 487 if (!IsWebStoreURL(profile_, source_url())) |
| 357 return false; | 488 return false; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 369 bool SetStoreLoginFunction::RunImpl() { | 500 bool SetStoreLoginFunction::RunImpl() { |
| 370 if (!IsWebStoreURL(profile_, source_url())) | 501 if (!IsWebStoreURL(profile_, source_url())) |
| 371 return false; | 502 return false; |
| 372 std::string login; | 503 std::string login; |
| 373 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &login)); | 504 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &login)); |
| 374 ExtensionService* service = profile_->GetExtensionService(); | 505 ExtensionService* service = profile_->GetExtensionService(); |
| 375 ExtensionPrefs* prefs = service->extension_prefs(); | 506 ExtensionPrefs* prefs = service->extension_prefs(); |
| 376 prefs->SetWebStoreLogin(login); | 507 prefs->SetWebStoreLogin(login); |
| 377 return true; | 508 return true; |
| 378 } | 509 } |
| OLD | NEW |