1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "xwalk/application/common/manifest_handlers/csp_handler.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/strings/string_split.h"
9 #include "xwalk/application/common/application_manifest_constants.h"
12 namespace application {
14 const char directive_separator = ';';
15 const char value_separator = ' ';
24 CSPHandler::CSPHandler(Package::Type type)
25 : package_type_(type) {
28 CSPHandler::~CSPHandler() {
31 bool CSPHandler::Parse(scoped_refptr<ApplicationData> application,
32 base::string16* error) {
33 if (package_type_ != application->GetPackageType())
35 scoped_ptr<CSPInfo> csp_info(new CSPInfo);
36 std::string policies_str;
37 const char* csp_key = GetCSPKey(package_type_);
38 if (application->GetManifest()->HasPath(csp_key) &&
39 !application->GetManifest()->GetString(csp_key, &policies_str)) {
40 *error = base::ASCIIToUTF16(
41 "Invalid value of Content Security Policy (CSP).");
45 std::vector<std::string> policies;
46 base::SplitString(policies_str, directive_separator, &policies);
47 for (size_t i = 0; i < policies.size(); ++i) {
48 size_t found = policies[i].find(value_separator);
49 if (found == std::string::npos) {
50 *error = base::ASCIIToUTF16("Invalid value of directive: " + policies[i]);
53 const std::string& directive_name = policies[i].substr(0, found);
54 const std::string& directive_value_str = policies[i].substr(found+1);
55 std::vector<std::string> directive_value;
56 base::SplitStringAlongWhitespace(directive_value_str, &directive_value);
57 csp_info->SetDirective(directive_name, directive_value);
59 application->SetManifestData(csp_key, csp_info.release());
64 bool CSPHandler::AlwaysParseForType(Manifest::Type type) const {
65 return package_type_ == Package::XPK;
68 std::vector<std::string> CSPHandler::Keys() const {
69 return std::vector<std::string>(1, GetCSPKey(package_type_));
72 } // namespace application