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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 const char kTokenKey[] = "token"; | 45 const char kTokenKey[] = "token"; |
46 | 46 |
47 const char kCannotSpecifyIconDataAndUrlError[] = | 47 const char kCannotSpecifyIconDataAndUrlError[] = |
48 "You cannot specify both icon data and an icon url"; | 48 "You cannot specify both icon data and an icon url"; |
49 const char kInvalidIconUrlError[] = "Invalid icon url"; | 49 const char kInvalidIconUrlError[] = "Invalid icon url"; |
50 const char kInvalidIdError[] = "Invalid id"; | 50 const char kInvalidIdError[] = "Invalid id"; |
51 const char kInvalidManifestError[] = "Invalid manifest"; | 51 const char kInvalidManifestError[] = "Invalid manifest"; |
52 const char kNoPreviousBeginInstallWithManifestError[] = | 52 const char kNoPreviousBeginInstallWithManifestError[] = |
53 "* does not match a previous call to beginInstallWithManifest3"; | 53 "* does not match a previous call to beginInstallWithManifest3"; |
54 const char kUserCancelledError[] = "User cancelled install"; | 54 const char kUserCancelledError[] = "User cancelled install"; |
| 55 const char kPermissionDeniedError[] = |
| 56 "You do not have permission to use this method."; |
55 | 57 |
56 ProfileSyncService* test_sync_service = NULL; | 58 ProfileSyncService* test_sync_service = NULL; |
57 | 59 |
58 // Returns either the test sync service, or the real one from |profile|. | 60 // Returns either the test sync service, or the real one from |profile|. |
59 ProfileSyncService* GetSyncService(Profile* profile) { | 61 ProfileSyncService* GetSyncService(Profile* profile) { |
60 if (test_sync_service) | 62 if (test_sync_service) |
61 return test_sync_service; | 63 return test_sync_service; |
62 else | 64 else |
63 return profile->GetProfileSyncService(); | 65 return profile->GetProfileSyncService(); |
64 } | 66 } |
65 | 67 |
66 bool IsWebStoreURL(Profile* profile, const GURL& url) { | 68 bool IsWebStoreURL(Profile* profile, const GURL& url) { |
67 ExtensionService* service = profile->GetExtensionService(); | 69 ExtensionService* service = profile->GetExtensionService(); |
68 const Extension* store = service->GetWebStoreApp(); | 70 const Extension* store = service->GetWebStoreApp(); |
69 if (!store) { | 71 if (!store) { |
70 NOTREACHED(); | 72 NOTREACHED(); |
71 return false; | 73 return false; |
72 } | 74 } |
73 return (service->GetExtensionByWebExtent(url) == store); | 75 return (service->GetExtensionByWebExtent(url) == store); |
74 } | 76 } |
75 | 77 |
| 78 // Whitelists extension IDs for use by webstorePrivate.silentlyInstall. |
| 79 bool trust_test_ids = false; |
| 80 |
| 81 bool IsTrustedForSilentInstall(const std::string& id) { |
| 82 // Trust the extensions in api_test/webstore_private/bundle when the flag |
| 83 // is set. |
| 84 if (trust_test_ids && |
| 85 (id == "begfmnajjkbjdgmffnjaojchoncnmngg" || |
| 86 id == "bmfoocgfinpmkmlbjhcbofejhkhlbchk" || |
| 87 id == "mpneghmdnmaolkljkipbhaienajcflfe")) |
| 88 return true; |
| 89 |
| 90 return |
| 91 id == "jgoepmocgafhnchmokaimcmlojpnlkhp" || // +1 Extension |
| 92 id == "cpembckmhnjipbgbnfiocbgnkpjdokdd" || // +1 Extension - dev |
| 93 id == "boemmnepglcoinjcdlfcpcbmhiecichi" || // Notifications |
| 94 id == "flibmgiapaohcbondaoopaalfejliklp" || // Notifications - dev |
| 95 id == "dlppkpafhbajpcmmoheippocdidnckmm" || // Remaining are placeholders |
| 96 id == "hmglfmpefabcafaimbpldpambdfomanl" || |
| 97 id == "idfijlieiecpfcjckpkliefekpokhhnd" || |
| 98 id == "jaokjbijaokooelpahnlmbciccldmfla" || |
| 99 id == "kdjeommiakphmeionoojjljlecmpaldd" || |
| 100 id == "lpdeojkfhenboeibhkjhiancceeboknd"; |
| 101 } |
| 102 |
76 // Helper to create a dictionary with login and token properties set from | 103 // Helper to create a dictionary with login and token properties set from |
77 // the appropriate values in the passed-in |profile|. | 104 // the appropriate values in the passed-in |profile|. |
78 DictionaryValue* CreateLoginResult(Profile* profile) { | 105 DictionaryValue* CreateLoginResult(Profile* profile) { |
79 DictionaryValue* dictionary = new DictionaryValue(); | 106 DictionaryValue* dictionary = new DictionaryValue(); |
80 std::string username = profile->GetPrefs()->GetString( | 107 std::string username = profile->GetPrefs()->GetString( |
81 prefs::kGoogleServicesUsername); | 108 prefs::kGoogleServicesUsername); |
82 dictionary->SetString(kLoginKey, username); | 109 dictionary->SetString(kLoginKey, username); |
83 if (!username.empty()) { | 110 if (!username.empty()) { |
84 CommandLine* cmdline = CommandLine::ForCurrentProcess(); | 111 CommandLine* cmdline = CommandLine::ForCurrentProcess(); |
85 TokenService* token_service = profile->GetTokenService(); | 112 TokenService* token_service = profile->GetTokenService(); |
(...skipping 16 matching lines...) Expand all Loading... |
102 ProfileSyncService* service) { | 129 ProfileSyncService* service) { |
103 test_sync_service = service; | 130 test_sync_service = service; |
104 } | 131 } |
105 | 132 |
106 // static | 133 // static |
107 void WebstorePrivateApi::SetWebstoreInstallerDelegateForTesting( | 134 void WebstorePrivateApi::SetWebstoreInstallerDelegateForTesting( |
108 WebstoreInstaller::Delegate* delegate) { | 135 WebstoreInstaller::Delegate* delegate) { |
109 test_webstore_installer_delegate = delegate; | 136 test_webstore_installer_delegate = delegate; |
110 } | 137 } |
111 | 138 |
| 139 // static |
| 140 void WebstorePrivateApi::SetTrustTestIDsForTesting(bool allow) { |
| 141 trust_test_ids = allow; |
| 142 } |
| 143 |
112 BeginInstallWithManifestFunction::BeginInstallWithManifestFunction() | 144 BeginInstallWithManifestFunction::BeginInstallWithManifestFunction() |
113 : use_app_installed_bubble_(false) {} | 145 : use_app_installed_bubble_(false) {} |
114 | 146 |
115 BeginInstallWithManifestFunction::~BeginInstallWithManifestFunction() {} | 147 BeginInstallWithManifestFunction::~BeginInstallWithManifestFunction() {} |
116 | 148 |
117 bool BeginInstallWithManifestFunction::RunImpl() { | 149 bool BeginInstallWithManifestFunction::RunImpl() { |
118 if (!IsWebStoreURL(profile_, source_url())) { | 150 if (!IsWebStoreURL(profile_, source_url())) { |
119 SetResult(PERMISSION_DENIED); | 151 SetResult(PERMISSION_DENIED); |
120 return false; | 152 return false; |
121 } | 153 } |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 // permissions install dialog. | 370 // permissions install dialog. |
339 scoped_refptr<WebstoreInstaller> installer = new WebstoreInstaller( | 371 scoped_refptr<WebstoreInstaller> installer = new WebstoreInstaller( |
340 profile(), test_webstore_installer_delegate, | 372 profile(), test_webstore_installer_delegate, |
341 &(dispatcher()->delegate()->GetAssociatedTabContents()->controller()), | 373 &(dispatcher()->delegate()->GetAssociatedTabContents()->controller()), |
342 id, WebstoreInstaller::FLAG_NONE); | 374 id, WebstoreInstaller::FLAG_NONE); |
343 installer->Start(); | 375 installer->Start(); |
344 | 376 |
345 return true; | 377 return true; |
346 } | 378 } |
347 | 379 |
| 380 SilentlyInstallFunction::SilentlyInstallFunction() {} |
| 381 SilentlyInstallFunction::~SilentlyInstallFunction() {} |
| 382 |
| 383 bool SilentlyInstallFunction::RunImpl() { |
| 384 if (!IsWebStoreURL(profile_, source_url())) { |
| 385 error_ = kPermissionDeniedError; |
| 386 return false; |
| 387 } |
| 388 |
| 389 DictionaryValue* details = NULL; |
| 390 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); |
| 391 CHECK(details); |
| 392 |
| 393 EXTENSION_FUNCTION_VALIDATE(details->GetString(kIdKey, &id_)); |
| 394 if (!IsTrustedForSilentInstall(id_)) { |
| 395 error_ = kInvalidIdError; |
| 396 return false; |
| 397 } |
| 398 |
| 399 EXTENSION_FUNCTION_VALIDATE(details->GetString(kManifestKey, &manifest_)); |
| 400 |
| 401 // Matched in OnWebstoreParseFailure, OnExtensionInstall{Success,Failure}. |
| 402 AddRef(); |
| 403 |
| 404 scoped_refptr<WebstoreInstallHelper> helper = new WebstoreInstallHelper( |
| 405 this, id_, manifest_, std::string(), GURL(), NULL); |
| 406 helper->Start(); |
| 407 |
| 408 return true; |
| 409 } |
| 410 |
| 411 void SilentlyInstallFunction::OnWebstoreParseSuccess( |
| 412 const std::string& id, |
| 413 const SkBitmap& icon, |
| 414 base::DictionaryValue* parsed_manifest) { |
| 415 CHECK_EQ(id_, id); |
| 416 |
| 417 // This lets CrxInstaller bypass the permission confirmation UI for the |
| 418 // extension. The whitelist entry gets cleared in |
| 419 // CrxInstaller::ConfirmInstall. |
| 420 CrxInstaller::WhitelistEntry* entry = new CrxInstaller::WhitelistEntry; |
| 421 entry->parsed_manifest.reset(parsed_manifest); |
| 422 entry->use_app_installed_bubble = false; |
| 423 entry->skip_post_install_ui = true; |
| 424 CrxInstaller::SetWhitelistEntry(id_, entry); |
| 425 |
| 426 scoped_refptr<WebstoreInstaller> installer = new WebstoreInstaller( |
| 427 profile(), this, |
| 428 &(dispatcher()->delegate()->GetAssociatedTabContents()->controller()), |
| 429 id_, WebstoreInstaller::FLAG_NONE); |
| 430 installer->Start(); |
| 431 } |
| 432 |
| 433 void SilentlyInstallFunction::OnWebstoreParseFailure( |
| 434 const std::string& id, |
| 435 InstallHelperResultCode result_code, |
| 436 const std::string& error_message) { |
| 437 CHECK_EQ(id_, id); |
| 438 |
| 439 error_ = error_message; |
| 440 SendResponse(false); |
| 441 |
| 442 Release(); // Matches the AddRef() in RunImpl(). |
| 443 } |
| 444 |
| 445 void SilentlyInstallFunction::OnExtensionInstallSuccess(const std::string& id) { |
| 446 CHECK_EQ(id_, id); |
| 447 |
| 448 SendResponse(true); |
| 449 |
| 450 Release(); // Matches the AddRef() in RunImpl(). |
| 451 } |
| 452 |
| 453 void SilentlyInstallFunction::OnExtensionInstallFailure( |
| 454 const std::string& id, const std::string& error) { |
| 455 CHECK_EQ(id_, id); |
| 456 |
| 457 error_ = error; |
| 458 SendResponse(false); |
| 459 |
| 460 Release(); // Matches the AddRef() in RunImpl(). |
| 461 } |
| 462 |
348 bool GetBrowserLoginFunction::RunImpl() { | 463 bool GetBrowserLoginFunction::RunImpl() { |
349 if (!IsWebStoreURL(profile_, source_url())) | 464 if (!IsWebStoreURL(profile_, source_url())) |
350 return false; | 465 return false; |
351 result_.reset(CreateLoginResult(profile_->GetOriginalProfile())); | 466 result_.reset(CreateLoginResult(profile_->GetOriginalProfile())); |
352 return true; | 467 return true; |
353 } | 468 } |
354 | 469 |
355 bool GetStoreLoginFunction::RunImpl() { | 470 bool GetStoreLoginFunction::RunImpl() { |
356 if (!IsWebStoreURL(profile_, source_url())) | 471 if (!IsWebStoreURL(profile_, source_url())) |
357 return false; | 472 return false; |
(...skipping 11 matching lines...) Expand all Loading... |
369 bool SetStoreLoginFunction::RunImpl() { | 484 bool SetStoreLoginFunction::RunImpl() { |
370 if (!IsWebStoreURL(profile_, source_url())) | 485 if (!IsWebStoreURL(profile_, source_url())) |
371 return false; | 486 return false; |
372 std::string login; | 487 std::string login; |
373 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &login)); | 488 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &login)); |
374 ExtensionService* service = profile_->GetExtensionService(); | 489 ExtensionService* service = profile_->GetExtensionService(); |
375 ExtensionPrefs* prefs = service->extension_prefs(); | 490 ExtensionPrefs* prefs = service->extension_prefs(); |
376 prefs->SetWebStoreLogin(login); | 491 prefs->SetWebStoreLogin(login); |
377 return true; | 492 return true; |
378 } | 493 } |
OLD | NEW |