OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/renderer/manifest/manifest_parser.h" | 5 #include "content/renderer/manifest/manifest_parser.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/strings/nullable_string16.h" | 8 #include "base/strings/nullable_string16.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 // TODO(mlamouri): provide a custom message to the developer console. | 25 // TODO(mlamouri): provide a custom message to the developer console. |
26 return base::NullableString16(); | 26 return base::NullableString16(); |
27 } | 27 } |
28 | 28 |
29 base::TrimWhitespace(name, base::TRIM_ALL, &name); | 29 base::TrimWhitespace(name, base::TRIM_ALL, &name); |
30 return base::NullableString16(name, false); | 30 return base::NullableString16(name, false); |
31 } | 31 } |
32 | 32 |
33 // Parses the 'short_name' field of the manifest, as defined in: | 33 // Parses the 'short_name' field of the manifest, as defined in: |
34 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-short-name-member | 34 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-short-name-member |
35 // |short_name| is an out parameter that must not be null. | |
36 // Returns the parsed string if any, a null string if the parsing failed. | 35 // Returns the parsed string if any, a null string if the parsing failed. |
37 base::NullableString16 ParseShortName( | 36 base::NullableString16 ParseShortName( |
38 const base::DictionaryValue& dictionary) { | 37 const base::DictionaryValue& dictionary) { |
39 if (!dictionary.HasKey("short_name")) | 38 if (!dictionary.HasKey("short_name")) |
40 return base::NullableString16(); | 39 return base::NullableString16(); |
41 | 40 |
42 base::string16 short_name; | 41 base::string16 short_name; |
43 if (!dictionary.GetString("short_name", &short_name)) { | 42 if (!dictionary.GetString("short_name", &short_name)) { |
44 // TODO(mlamouri): provide a custom message to the developer console. | 43 // TODO(mlamouri): provide a custom message to the developer console. |
45 return base::NullableString16(); | 44 return base::NullableString16(); |
46 } | 45 } |
47 | 46 |
48 base::TrimWhitespace(short_name, base::TRIM_ALL, &short_name); | 47 base::TrimWhitespace(short_name, base::TRIM_ALL, &short_name); |
49 return base::NullableString16(short_name, false); | 48 return base::NullableString16(short_name, false); |
50 } | 49 } |
51 | 50 |
| 51 // Parses the 'start_url' field of the manifest, as defined in: |
| 52 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-start_url-member |
| 53 // Returns the parsed GURL if any, an empty GURL if the parsing failed. |
| 54 GURL ParseStartURL(const base::DictionaryValue& dictionary, |
| 55 const GURL& manifest_url, |
| 56 const GURL& document_url) { |
| 57 if (!dictionary.HasKey("start_url")) |
| 58 return GURL(); |
| 59 |
| 60 base::string16 start_url_str; |
| 61 if (!dictionary.GetString("start_url", &start_url_str)) { |
| 62 // TODO(mlamouri): provide a custom message to the developer console. |
| 63 return GURL(); |
| 64 } |
| 65 |
| 66 GURL start_url = manifest_url.Resolve(start_url_str); |
| 67 if (!start_url.is_valid()) |
| 68 return GURL(); |
| 69 |
| 70 if (start_url.GetOrigin() != document_url.GetOrigin()) { |
| 71 // TODO(mlamouri): provide a custom message to the developer console. |
| 72 return GURL(); |
| 73 } |
| 74 |
| 75 return start_url; |
| 76 } |
| 77 |
52 } // anonymous namespace | 78 } // anonymous namespace |
53 | 79 |
54 namespace content { | 80 namespace content { |
55 | 81 |
56 Manifest ManifestParser::Parse(const base::StringPiece& json) { | 82 Manifest ManifestParser::Parse(const base::StringPiece& json, |
| 83 const GURL& manifest_url, |
| 84 const GURL& document_url) { |
57 scoped_ptr<base::Value> value(base::JSONReader::Read(json)); | 85 scoped_ptr<base::Value> value(base::JSONReader::Read(json)); |
58 if (!value) { | 86 if (!value) { |
59 // TODO(mlamouri): get the JSON parsing error and report it to the developer | 87 // TODO(mlamouri): get the JSON parsing error and report it to the developer |
60 // console. | 88 // console. |
61 return Manifest(); | 89 return Manifest(); |
62 } | 90 } |
63 | 91 |
64 if (value->GetType() != base::Value::TYPE_DICTIONARY) { | 92 if (value->GetType() != base::Value::TYPE_DICTIONARY) { |
65 // TODO(mlamouri): provide a custom message to the developer console. | 93 // TODO(mlamouri): provide a custom message to the developer console. |
66 return Manifest(); | 94 return Manifest(); |
67 } | 95 } |
68 | 96 |
69 base::DictionaryValue* dictionary = 0; | 97 base::DictionaryValue* dictionary = 0; |
70 value->GetAsDictionary(&dictionary); | 98 value->GetAsDictionary(&dictionary); |
71 if (!dictionary) { | 99 if (!dictionary) { |
72 // TODO(mlamouri): provide a custom message to the developer console. | 100 // TODO(mlamouri): provide a custom message to the developer console. |
73 return Manifest(); | 101 return Manifest(); |
74 } | 102 } |
75 | 103 |
76 Manifest manifest; | 104 Manifest manifest; |
77 | 105 |
78 manifest.name = ParseName(*dictionary); | 106 manifest.name = ParseName(*dictionary); |
79 manifest.short_name = ParseShortName(*dictionary); | 107 manifest.short_name = ParseShortName(*dictionary); |
| 108 manifest.start_url = ParseStartURL(*dictionary, manifest_url, document_url); |
80 | 109 |
81 return manifest; | 110 return manifest; |
82 } | 111 } |
83 | 112 |
84 } // namespace content | 113 } // namespace content |
OLD | NEW |