1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/common/extensions/api/side_panel/side_panel_info.h"
#include "base/files/file_util.h"
#include "chrome/common/extensions/api/side_panel.h"
#include "extensions/common/manifest.h"
#include "extensions/common/manifest_constants.h"
namespace extensions {
namespace keys = manifest_keys;
namespace errors = manifest_errors;
using SidePanelManifestKeys = api::side_panel::ManifestKeys;
namespace {
const SidePanelInfo* GetResourcesInfo(const Extension* extension) {
return static_cast<SidePanelInfo*>(
extension->GetManifestData(SidePanelManifestKeys::kSidePanel));
}
std::unique_ptr<SidePanelInfo> ParseFromDictionary(const Extension& extension,
std::u16string* error) {
SidePanelManifestKeys manifest_keys;
if (!SidePanelManifestKeys::ParseFromDictionary(
extension.manifest()->available_values(), manifest_keys, *error)) {
return nullptr;
}
auto info = std::make_unique<SidePanelInfo>();
info->default_path = std::move(manifest_keys.side_panel.default_path);
return info;
}
bool ExtensionResourceExists(const Extension* extension,
const std::string& path) {
auto resource_path = extension->GetResource(path).GetFilePath();
return !resource_path.empty() && base::PathExists(resource_path);
}
} // namespace
SidePanelInfo::SidePanelInfo() = default;
SidePanelInfo::~SidePanelInfo() = default;
// static
bool SidePanelInfo::HasSidePanel(const Extension* extension) {
const SidePanelInfo* info = GetResourcesInfo(extension);
return info != nullptr;
}
// static
std::string SidePanelInfo::GetDefaultPath(const Extension* extension) {
const SidePanelInfo* info = GetResourcesInfo(extension);
return info ? info->default_path : "";
}
SidePanelManifestHandler::SidePanelManifestHandler() = default;
SidePanelManifestHandler::~SidePanelManifestHandler() = default;
bool SidePanelManifestHandler::Parse(Extension* extension,
std::u16string* error) {
auto info = ParseFromDictionary(*extension, error);
if (!info) {
return false;
}
extension->SetManifestData(SidePanelManifestKeys::kSidePanel,
std::move(info));
return true;
}
base::span<const char* const> SidePanelManifestHandler::Keys() const {
static constexpr const char* kKeys[] = {SidePanelManifestKeys::kSidePanel};
return kKeys;
}
bool SidePanelManifestHandler::Validate(
const Extension* extension,
std::string* error,
std::vector<InstallWarning>* warnings) const {
std::string path = SidePanelInfo::GetDefaultPath(extension);
GURL side_panel_url = extension->GetResourceURL(path);
if (!side_panel_url.is_valid() ||
!ExtensionResourceExists(extension, side_panel_url.path())) {
*error = errors::kSidePanelManifestDefaultPathError;
return false;
}
return true;
}
} // namespace extensions
|