0317936d9b0064f8b4f87e89d58d11660fd70b72
[platform/framework/web/crosswalk.git] / src / xwalk / application / common / manifest_handlers / csp_handler.cc
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.
4
5 #include "xwalk/application/common/manifest_handlers/csp_handler.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/strings/string_split.h"
9 #include "xwalk/application/common/application_manifest_constants.h"
10
11 namespace xwalk {
12 namespace application {
13 namespace {
14 const char directive_separator = ';';
15 const char value_separator = ' ';
16 }  // namespace
17
18 CSPInfo::CSPInfo() {
19 }
20
21 CSPInfo::~CSPInfo() {
22 }
23
24 CSPHandler::CSPHandler(Manifest::PackageType type)
25     : package_type_(type) {
26 }
27
28 CSPHandler::~CSPHandler() {
29 }
30
31 bool CSPHandler::Parse(scoped_refptr<ApplicationData> application,
32                        base::string16* error) {
33   if (package_type_ != application->GetPackageType())
34     return false;
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).");
42     return false;
43   }
44
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]);
51       return false;
52     }
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);
58   }
59   application->SetManifestData(csp_key, csp_info.release());
60
61   return true;
62 }
63
64 bool CSPHandler::AlwaysParseForType(Manifest::Type type) const {
65   return package_type_ == Manifest::TYPE_XPK;
66 }
67
68 std::vector<std::string> CSPHandler::Keys() const {
69   return std::vector<std::string>(1, GetCSPKey(package_type_));
70 }
71
72 }  // namespace application
73 }  // namespace xwalk