Skip to content

Commit

Permalink
fix: crash when loadExtension fails (#27590)
Browse files Browse the repository at this point in the history
Co-authored-by: samuelmaddock <samuel.maddock@gmail.com>
  • Loading branch information
trop[bot] and samuelmaddock committed Feb 3, 2021
1 parent 0827a06 commit 8baf1dc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
30 changes: 15 additions & 15 deletions shell/browser/extensions/electron_extension_loader.cc
Expand Up @@ -112,22 +112,22 @@ void ElectronExtensionLoader::FinishExtensionLoad(
scoped_refptr<const Extension> extension = result.first;
if (extension) {
extension_registrar_.AddExtension(extension);
}

// Write extension install time to ExtensionPrefs. This is required by
// WebRequestAPI which calls extensions::ExtensionPrefs::GetInstallTime.
//
// Implementation for writing the pref was based on
// PreferenceAPIBase::SetExtensionControlledPref.
{
ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(browser_context_);
ExtensionPrefs::ScopedDictionaryUpdate update(
extension_prefs, extension.get()->id(),
extensions::pref_names::kPrefPreferences);
auto preference = update.Create();
const base::Time install_time = base::Time().Now();
preference->SetString("install_time",
base::NumberToString(install_time.ToInternalValue()));
// Write extension install time to ExtensionPrefs. This is required by
// WebRequestAPI which calls extensions::ExtensionPrefs::GetInstallTime.
//
// Implementation for writing the pref was based on
// PreferenceAPIBase::SetExtensionControlledPref.
{
ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(browser_context_);
ExtensionPrefs::ScopedDictionaryUpdate update(
extension_prefs, extension.get()->id(),
extensions::pref_names::kPrefPreferences);
auto preference = update.Create();
const base::Time install_time = base::Time().Now();
preference->SetString(
"install_time", base::NumberToString(install_time.ToInternalValue()));
}
}

std::move(cb).Run(extension.get(), result.second);
Expand Down
6 changes: 6 additions & 0 deletions spec-main/extensions-spec.ts
Expand Up @@ -111,6 +111,12 @@ describe('chrome extensions', () => {
expect(bg).to.equal('red');
});

it('does not crash when failing to load an extension', async () => {
const customSession = session.fromPartition(`persist:${uuid.v4()}`);
const promise = customSession.loadExtension(path.join(fixtures, 'extensions', 'load-error'));
await expect(promise).to.eventually.be.rejected();
});

it('serializes a loaded extension', async () => {
const extensionPath = path.join(fixtures, 'extensions', 'red-bg');
const manifest = JSON.parse(fs.readFileSync(path.join(extensionPath, 'manifest.json'), 'utf-8'));
Expand Down
8 changes: 8 additions & 0 deletions spec-main/fixtures/extensions/load-error/manifest.json
@@ -0,0 +1,8 @@
{
"name": "load-error",
"version": "1.0",
"icons": {
"16": "/images/error.png"
},
"manifest_version": 2
}

0 comments on commit 8baf1dc

Please sign in to comment.