| 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/public/renderer/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_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "content/public/common/content_client.h" |
| 14 #include "content/public/common/manifest.h" | 15 #include "content/public/common/manifest.h" |
| 16 #include "content/public/renderer/content_renderer_client.h" |
| 15 #include "content/renderer/manifest/manifest_uma_util.h" | 17 #include "content/renderer/manifest/manifest_uma_util.h" |
| 16 #include "ui/gfx/geometry/size.h" | 18 #include "ui/gfx/geometry/size.h" |
| 17 | 19 |
| 18 namespace content { | 20 namespace content { |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 // Helper function that returns whether the given |str| is a valid width or | 24 // Helper function that returns whether the given |str| is a valid width or |
| 23 // height value for an icon sizes per: | 25 // height value for an icon sizes per: |
| 24 // https://html.spec.whatwg.org/multipage/semantics.html#attr-link-sizes | 26 // https://html.spec.whatwg.org/multipage/semantics.html#attr-link-sizes |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 } | 80 } |
| 79 | 81 |
| 80 const std::string& GetErrorPrefix() { | 82 const std::string& GetErrorPrefix() { |
| 81 CR_DEFINE_STATIC_LOCAL(std::string, error_prefix, | 83 CR_DEFINE_STATIC_LOCAL(std::string, error_prefix, |
| 82 ("Manifest parsing error: ")); | 84 ("Manifest parsing error: ")); |
| 83 return error_prefix; | 85 return error_prefix; |
| 84 } | 86 } |
| 85 | 87 |
| 86 } // anonymous namespace | 88 } // anonymous namespace |
| 87 | 89 |
| 90 // static |
| 91 scoped_ptr<ManifestParser> ManifestParser::Get() { |
| 92 ManifestParser* parser = nullptr; |
| 93 if (GetContentClient()->renderer()->OverrideManifestParser(&parser)) { |
| 94 return scoped_ptr<ManifestParser>(parser); |
| 95 } |
| 88 | 96 |
| 89 ManifestParser::ManifestParser(const base::StringPiece& data, | 97 return scoped_ptr<ManifestParser>(new ManifestParser); |
| 90 const GURL& manifest_url, | 98 } |
| 91 const GURL& document_url) | 99 |
| 92 : data_(data), | 100 ManifestParser::ManifestParser() |
| 93 manifest_url_(manifest_url), | 101 : failed_(false) { |
| 94 document_url_(document_url), | |
| 95 failed_(false) { | |
| 96 } | 102 } |
| 97 | 103 |
| 98 ManifestParser::~ManifestParser() { | 104 ManifestParser::~ManifestParser() { |
| 99 } | 105 } |
| 100 | 106 |
| 101 void ManifestParser::Parse() { | 107 void ManifestParser::Parse(const base::StringPiece& data, |
| 108 const GURL& manifest_url, |
| 109 const GURL& document_url) { |
| 110 manifest_url_ = manifest_url; |
| 111 document_url_ = document_url; |
| 112 |
| 113 DCHECK(manifest_url_.is_valid()); |
| 114 DCHECK(document_url_.is_valid()); |
| 115 |
| 102 std::string parse_error; | 116 std::string parse_error; |
| 103 scoped_ptr<base::Value> value( | 117 scoped_ptr<base::Value> value( |
| 104 base::JSONReader::ReadAndReturnError(data_, base::JSON_PARSE_RFC, | 118 base::JSONReader::ReadAndReturnError(data, base::JSON_PARSE_RFC, |
| 105 nullptr, &parse_error)); | 119 nullptr, &parse_error)); |
| 106 | 120 |
| 107 if (!value) { | 121 if (!value) { |
| 108 errors_.push_back(GetErrorPrefix() + parse_error); | 122 errors_.push_back(GetErrorPrefix() + parse_error); |
| 109 ManifestUmaUtil::ParseFailed(); | 123 ManifestUmaUtil::ParseFailed(); |
| 110 failed_ = true; | 124 failed_ = true; |
| 111 return; | 125 return; |
| 112 } | 126 } |
| 113 | 127 |
| 114 base::DictionaryValue* dictionary = nullptr; | 128 base::DictionaryValue* dictionary = nullptr; |
| 115 if (!value->GetAsDictionary(&dictionary)) { | 129 if (!value->GetAsDictionary(&dictionary)) { |
| 116 errors_.push_back(GetErrorPrefix() + | 130 errors_.push_back(GetErrorPrefix() + |
| 117 "root element must be a valid JSON object."); | 131 "root element must be a valid JSON object."); |
| 118 ManifestUmaUtil::ParseFailed(); | 132 ManifestUmaUtil::ParseFailed(); |
| 119 failed_ = true; | 133 failed_ = true; |
| 120 return; | 134 return; |
| 121 } | 135 } |
| 122 DCHECK(dictionary); | 136 DCHECK(dictionary); |
| 123 | 137 |
| 138 ParseExtensionPoint(*dictionary); |
| 139 |
| 124 manifest_.name = ParseName(*dictionary); | 140 manifest_.name = ParseName(*dictionary); |
| 125 manifest_.short_name = ParseShortName(*dictionary); | 141 manifest_.short_name = ParseShortName(*dictionary); |
| 126 manifest_.start_url = ParseStartURL(*dictionary); | 142 manifest_.start_url = ParseStartURL(*dictionary); |
| 127 manifest_.display = ParseDisplay(*dictionary); | 143 manifest_.display = ParseDisplay(*dictionary); |
| 128 manifest_.orientation = ParseOrientation(*dictionary); | 144 manifest_.orientation = ParseOrientation(*dictionary); |
| 129 manifest_.icons = ParseIcons(*dictionary); | 145 manifest_.icons = ParseIcons(*dictionary); |
| 130 manifest_.gcm_sender_id = ParseGCMSenderID(*dictionary); | |
| 131 manifest_.gcm_user_visible_only = ParseGCMUserVisibleOnly(*dictionary); | |
| 132 | 146 |
| 133 ManifestUmaUtil::ParseSucceeded(manifest_); | 147 ManifestUmaUtil::ParseSucceeded(manifest_); |
| 134 } | 148 } |
| 135 | 149 |
| 150 void ManifestParser::ParseExtensionPoint(const base::DictionaryValue&) { |
| 151 } |
| 152 |
| 136 const Manifest& ManifestParser::manifest() const { | 153 const Manifest& ManifestParser::manifest() const { |
| 137 return manifest_; | 154 return manifest_; |
| 138 } | 155 } |
| 139 | 156 |
| 140 const std::vector<std::string>& ManifestParser::errors() const { | 157 const std::vector<std::string>& ManifestParser::errors() const { |
| 141 return errors_; | 158 return errors_; |
| 142 } | 159 } |
| 143 | 160 |
| 144 bool ManifestParser::failed() const { | 161 bool ManifestParser::failed() const { |
| 145 return failed_; | 162 return failed_; |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 icon.type = ParseIconType(*icon_dictionary); | 344 icon.type = ParseIconType(*icon_dictionary); |
| 328 icon.density = ParseIconDensity(*icon_dictionary); | 345 icon.density = ParseIconDensity(*icon_dictionary); |
| 329 icon.sizes = ParseIconSizes(*icon_dictionary); | 346 icon.sizes = ParseIconSizes(*icon_dictionary); |
| 330 | 347 |
| 331 icons.push_back(icon); | 348 icons.push_back(icon); |
| 332 } | 349 } |
| 333 | 350 |
| 334 return icons; | 351 return icons; |
| 335 } | 352 } |
| 336 | 353 |
| 337 base::NullableString16 ManifestParser::ParseGCMSenderID( | |
| 338 const base::DictionaryValue& dictionary) { | |
| 339 return ParseString(dictionary, "gcm_sender_id", Trim); | |
| 340 } | |
| 341 | |
| 342 bool ManifestParser::ParseGCMUserVisibleOnly( | |
| 343 const base::DictionaryValue& dictionary) { | |
| 344 return ParseBoolean(dictionary, "gcm_user_visible_only", false); | |
| 345 } | |
| 346 | |
| 347 } // namespace content | 354 } // namespace content |
| OLD | NEW |