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

Side by Side Diff: chrome/browser/extensions/extension_webstore_private_api.cc

Issue 8430033: Adds a webstorePrivate method for silently installing extensions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remove dcheck Created 9 years, 1 month 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 | Annotate | Revision Log
OLDNEW
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
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 if (trust_test_ids &&
83 (id == "begfmnajjkbjdgmffnjaojchoncnmngg" ||
Mihai Parparita -not on Chrome 2011/11/02 21:05:09 Nit: Have you considered adding a comment for each
jstritar 2011/11/02 21:20:49 Done.
84 id == "bmfoocgfinpmkmlbjhcbofejhkhlbchk" ||
85 id == "mpneghmdnmaolkljkipbhaienajcflfe"))
86 return true;
87
88 return
89 id == "jgoepmocgafhnchmokaimcmlojpnlkhp" ||
90 id == "cpembckmhnjipbgbnfiocbgnkpjdokdd" ||
91 id == "boemmnepglcoinjcdlfcpcbmhiecichi" ||
92 id == "flibmgiapaohcbondaoopaalfejliklp" ||
93 id == "dlppkpafhbajpcmmoheippocdidnckmm" ||
94 id == "hmglfmpefabcafaimbpldpambdfomanl" ||
95 id == "idfijlieiecpfcjckpkliefekpokhhnd" ||
96 id == "jaokjbijaokooelpahnlmbciccldmfla" ||
97 id == "kdjeommiakphmeionoojjljlecmpaldd" ||
98 id == "lpdeojkfhenboeibhkjhiancceeboknd";
99 }
100
76 // Helper to create a dictionary with login and token properties set from 101 // Helper to create a dictionary with login and token properties set from
77 // the appropriate values in the passed-in |profile|. 102 // the appropriate values in the passed-in |profile|.
78 DictionaryValue* CreateLoginResult(Profile* profile) { 103 DictionaryValue* CreateLoginResult(Profile* profile) {
79 DictionaryValue* dictionary = new DictionaryValue(); 104 DictionaryValue* dictionary = new DictionaryValue();
80 std::string username = profile->GetPrefs()->GetString( 105 std::string username = profile->GetPrefs()->GetString(
81 prefs::kGoogleServicesUsername); 106 prefs::kGoogleServicesUsername);
82 dictionary->SetString(kLoginKey, username); 107 dictionary->SetString(kLoginKey, username);
83 if (!username.empty()) { 108 if (!username.empty()) {
84 CommandLine* cmdline = CommandLine::ForCurrentProcess(); 109 CommandLine* cmdline = CommandLine::ForCurrentProcess();
85 TokenService* token_service = profile->GetTokenService(); 110 TokenService* token_service = profile->GetTokenService();
(...skipping 16 matching lines...) Expand all
102 ProfileSyncService* service) { 127 ProfileSyncService* service) {
103 test_sync_service = service; 128 test_sync_service = service;
104 } 129 }
105 130
106 // static 131 // static
107 void WebstorePrivateApi::SetWebstoreInstallerDelegateForTesting( 132 void WebstorePrivateApi::SetWebstoreInstallerDelegateForTesting(
108 WebstoreInstaller::Delegate* delegate) { 133 WebstoreInstaller::Delegate* delegate) {
109 test_webstore_installer_delegate = delegate; 134 test_webstore_installer_delegate = delegate;
110 } 135 }
111 136
137 // static
138 void WebstorePrivateApi::SetTrustTestIDsForTesting(bool allow) {
139 trust_test_ids = allow;
140 }
141
112 BeginInstallWithManifestFunction::BeginInstallWithManifestFunction() 142 BeginInstallWithManifestFunction::BeginInstallWithManifestFunction()
113 : use_app_installed_bubble_(false) {} 143 : use_app_installed_bubble_(false) {}
114 144
115 BeginInstallWithManifestFunction::~BeginInstallWithManifestFunction() {} 145 BeginInstallWithManifestFunction::~BeginInstallWithManifestFunction() {}
116 146
117 bool BeginInstallWithManifestFunction::RunImpl() { 147 bool BeginInstallWithManifestFunction::RunImpl() {
118 if (!IsWebStoreURL(profile_, source_url())) { 148 if (!IsWebStoreURL(profile_, source_url())) {
119 SetResult(PERMISSION_DENIED); 149 SetResult(PERMISSION_DENIED);
120 return false; 150 return false;
121 } 151 }
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 // permissions install dialog. 368 // permissions install dialog.
339 scoped_refptr<WebstoreInstaller> installer = new WebstoreInstaller( 369 scoped_refptr<WebstoreInstaller> installer = new WebstoreInstaller(
340 profile(), test_webstore_installer_delegate, 370 profile(), test_webstore_installer_delegate,
341 &(dispatcher()->delegate()->GetAssociatedTabContents()->controller()), 371 &(dispatcher()->delegate()->GetAssociatedTabContents()->controller()),
342 id, WebstoreInstaller::FLAG_NONE); 372 id, WebstoreInstaller::FLAG_NONE);
343 installer->Start(); 373 installer->Start();
344 374
345 return true; 375 return true;
346 } 376 }
347 377
378 SilentlyInstallFunction::SilentlyInstallFunction() {}
379 SilentlyInstallFunction::~SilentlyInstallFunction() {}
380
381 bool SilentlyInstallFunction::RunImpl() {
382 if (!IsWebStoreURL(profile_, source_url())) {
383 error_ = kPermissionDeniedError;
384 return false;
385 }
386
387 DictionaryValue* details = NULL;
388 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details));
389 CHECK(details);
390
391 EXTENSION_FUNCTION_VALIDATE(details->GetString(kIdKey, &id_));
392 if (!IsTrustedForSilentInstall(id_)) {
393 error_ = kInvalidIdError;
394 return false;
395 }
396
397 EXTENSION_FUNCTION_VALIDATE(details->GetString(kManifestKey, &manifest_));
398
399 // Matched in OnWebstoreParseFailure, OnExtensionInstall{Success,Failure}.
400 AddRef();
401
402 scoped_refptr<WebstoreInstallHelper> helper = new WebstoreInstallHelper(
403 this, id_, manifest_, std::string(), GURL(), NULL);
404 helper->Start();
405
406 return true;
407 }
408
409 void SilentlyInstallFunction::OnWebstoreParseSuccess(
410 const std::string& id,
411 const SkBitmap& icon,
412 base::DictionaryValue* parsed_manifest) {
413 CHECK_EQ(id_, id);
414
415 // This lets CrxInstaller bypass the permission confirmation UI for the
416 // extension. The whitelist entry gets cleared in
417 // CrxInstaller::ConfirmInstall.
418 CrxInstaller::WhitelistEntry* entry = new CrxInstaller::WhitelistEntry;
419 entry->parsed_manifest.reset(parsed_manifest);
420 entry->use_app_installed_bubble = false;
421 entry->skip_post_install_ui = true;
422 CrxInstaller::SetWhitelistEntry(id_, entry);
423
424 scoped_refptr<WebstoreInstaller> installer = new WebstoreInstaller(
425 profile(), this,
426 &(dispatcher()->delegate()->GetAssociatedTabContents()->controller()),
427 id_, WebstoreInstaller::FLAG_NONE);
428 installer->Start();
429 }
430
431 void SilentlyInstallFunction::OnWebstoreParseFailure(
432 const std::string& id,
433 InstallHelperResultCode result_code,
434 const std::string& error_message) {
435 CHECK_EQ(id_, id);
436
437 error_ = error_message;
438 SendResponse(false);
439
440 Release(); // Matches the AddRef() in RunImpl().
441 }
442
443 void SilentlyInstallFunction::OnExtensionInstallSuccess(const std::string& id) {
444 CHECK_EQ(id_, id);
445
446 SendResponse(true);
447
448 Release(); // Matches the AddRef() in RunImpl().
449 }
450
451 void SilentlyInstallFunction::OnExtensionInstallFailure(
452 const std::string& id, const std::string& error) {
453 CHECK_EQ(id_, id);
454
455 error_ = error;
456 SendResponse(false);
457
458 Release(); // Matches the AddRef() in RunImpl().
459 }
460
348 bool GetBrowserLoginFunction::RunImpl() { 461 bool GetBrowserLoginFunction::RunImpl() {
349 if (!IsWebStoreURL(profile_, source_url())) 462 if (!IsWebStoreURL(profile_, source_url()))
350 return false; 463 return false;
351 result_.reset(CreateLoginResult(profile_->GetOriginalProfile())); 464 result_.reset(CreateLoginResult(profile_->GetOriginalProfile()));
352 return true; 465 return true;
353 } 466 }
354 467
355 bool GetStoreLoginFunction::RunImpl() { 468 bool GetStoreLoginFunction::RunImpl() {
356 if (!IsWebStoreURL(profile_, source_url())) 469 if (!IsWebStoreURL(profile_, source_url()))
357 return false; 470 return false;
(...skipping 11 matching lines...) Expand all
369 bool SetStoreLoginFunction::RunImpl() { 482 bool SetStoreLoginFunction::RunImpl() {
370 if (!IsWebStoreURL(profile_, source_url())) 483 if (!IsWebStoreURL(profile_, source_url()))
371 return false; 484 return false;
372 std::string login; 485 std::string login;
373 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &login)); 486 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &login));
374 ExtensionService* service = profile_->GetExtensionService(); 487 ExtensionService* service = profile_->GetExtensionService();
375 ExtensionPrefs* prefs = service->extension_prefs(); 488 ExtensionPrefs* prefs = service->extension_prefs();
376 prefs->SetWebStoreLogin(login); 489 prefs->SetWebStoreLogin(login);
377 return true; 490 return true;
378 } 491 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698