Index: components/omnibox/suggestion_answer.cc |
diff --git a/components/omnibox/suggestion_answer.cc b/components/omnibox/suggestion_answer.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d7a56d42b5fd863cb751d2cbf9bdbd6a2e5cf379 |
--- /dev/null |
+++ b/components/omnibox/suggestion_answer.cc |
@@ -0,0 +1,200 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/omnibox/suggestion_answer.h" |
+ |
+#include "base/json/json_reader.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/values.h" |
+ |
+namespace { |
+ |
+static const char* kAnswerJsonLines = "l"; |
+static const char* kAnswerJsonImageLine = "il"; |
+static const char* kAnswerJsonText = "t"; |
+static const char* kAnswerJsonAdditionalText = "at"; |
+static const char* kAnswerJsonStatusText = "st"; |
+static const char* kAnswerJsonTextType = "tt"; |
+static const char* kAnswerJsonImage = "i"; |
+static const char* kAnswerJsonImageData = "i.d"; |
+ |
+} |
+ |
+// SuggestionAnswer::TextField ------------------------------------------------- |
+ |
+SuggestionAnswer::TextField::TextField() {} |
+SuggestionAnswer::TextField::~TextField() {} |
+ |
+// static |
+bool SuggestionAnswer::TextField::ParseTextField( |
+ const base::DictionaryValue* field_json, TextField* text_field) { |
+ if (!field_json->GetString(kAnswerJsonText, &text_field->text_) || |
+ text_field->text_.empty()) |
+ return false; |
+ if (!field_json->GetInteger(kAnswerJsonTextType, &text_field->type_)) |
+ return false; |
+ return true; |
+} |
+ |
+bool SuggestionAnswer::TextField::operator==(const TextField& field) const { |
+ return type_ == field.type_ && text_ == field.text_; |
+} |
+ |
+bool SuggestionAnswer::TextField::operator!=(const TextField& field) const { |
+ return !operator==(field); |
+} |
+ |
+void SuggestionAnswer::TextField::Clear() { |
+ text_.clear(); |
+ type_ = 0; |
+} |
+ |
+// SuggestionAnswer::ImageLine ------------------------------------------------- |
+ |
+SuggestionAnswer::ImageLine::ImageLine() {} |
+SuggestionAnswer::ImageLine::~ImageLine() {} |
+ |
+// static |
+bool SuggestionAnswer::ImageLine::ParseImageLine( |
+ const base::DictionaryValue* line_json, ImageLine* image_line) { |
+ const base::DictionaryValue* inner_json; |
+ if (!line_json->GetDictionary(kAnswerJsonImageLine, &inner_json)) |
+ return false; |
+ |
+ const base::ListValue* fields_json; |
+ if (!inner_json->GetList(kAnswerJsonText, &fields_json) || |
+ fields_json->GetSize() == 0) |
+ return false; |
+ |
+ for (size_t i = 0; i < fields_json->GetSize(); ++i) { |
+ const base::DictionaryValue* field_json; |
+ TextField text_field; |
+ if (!fields_json->GetDictionary(i, &field_json) || |
+ !TextField::ParseTextField(field_json, &text_field)) |
+ return false; |
+ image_line->text_fields_.push_back(text_field); |
+ } |
+ |
+ if (inner_json->HasKey(kAnswerJsonAdditionalText)) { |
+ const base::DictionaryValue* field_json; |
+ if (!inner_json->GetDictionary(kAnswerJsonAdditionalText, &field_json) || |
+ !TextField::ParseTextField(field_json, &image_line->additional_text_)) |
+ return false; |
+ } |
+ |
+ if (inner_json->HasKey(kAnswerJsonStatusText)) { |
+ const base::DictionaryValue* field_json; |
+ if (!inner_json->GetDictionary(kAnswerJsonStatusText, &field_json) || |
+ !TextField::ParseTextField(field_json, &image_line->status_text_)) |
+ return false; |
+ } |
+ |
+ if (inner_json->HasKey(kAnswerJsonImage)) { |
+ std::string url_string; |
+ if (!inner_json->GetString(kAnswerJsonImageData, &url_string) || |
+ url_string.empty()) |
+ return false; |
+ // Concatenate scheme and host/path using only ':' as separator. This is |
+ // due to the results delivering strings of the form '//host/path', which |
+ // is web-speak for "use the enclosing page's scheme", but not a valid path |
+ // of an URL. |
+ GURL url("https:" + url_string); |
+ if (!url.is_valid()) |
+ return false; |
+ image_line->image_url_ = url; |
+ } |
+ |
+ return true; |
+} |
+ |
+bool SuggestionAnswer::ImageLine::operator==(const ImageLine& line) const { |
+ if (text_fields_.size() != line.text_fields_.size()) |
+ return false; |
+ for (size_t i = 0; i < text_fields_.size(); ++i) { |
+ if (text_fields_[i] != line.text_fields_[i]) |
+ return false; |
+ } |
+ return additional_text_ == line.additional_text_ && |
+ status_text_ == line.status_text_ && |
+ image_url_ == line.image_url_; |
+} |
+ |
+bool SuggestionAnswer::ImageLine::operator!=(const ImageLine& line) const { |
+ return !operator==(line); |
+} |
+ |
+bool SuggestionAnswer::ImageLine::HasImageURL() const { |
+ // If no URL was present in the source JSON then image_url_ will never be set |
+ // and the default value of is_valid() is false. |
+ return image_url_.is_valid(); |
+} |
+ |
+void SuggestionAnswer::ImageLine::Clear() { |
+ text_fields_.clear(); |
+ additional_text_.Clear(); |
+ status_text_.Clear(); |
+ image_url_ = GURL(); |
+} |
+ |
+// SuggestionAnswer ------------------------------------------------------------ |
+ |
+SuggestionAnswer::SuggestionAnswer() : type_(0), is_valid_(false) {} |
+SuggestionAnswer::~SuggestionAnswer() {} |
+ |
+// static |
+bool SuggestionAnswer::ParseAnswer( |
+ const std::string& answer_json, SuggestionAnswer* answer) { |
+ scoped_ptr<base::Value> value(base::JSONReader::Read(answer_json)); |
+ base::DictionaryValue* dict; |
+ if (!value.get() || !value->GetAsDictionary(&dict)) |
+ return false; |
+ |
+ return ParseAnswer(dict, answer); |
+} |
+ |
+// static |
+bool SuggestionAnswer::ParseAnswer( |
+ const base::DictionaryValue* answer_json, SuggestionAnswer* answer) { |
+ answer->Clear(); |
+ const base::ListValue* lines_json; |
+ if (!answer_json->GetList(kAnswerJsonLines, &lines_json) || |
+ lines_json->GetSize() != 2) |
+ return false; |
+ |
+ const base::DictionaryValue* first_line_json; |
+ if (!lines_json->GetDictionary(0, &first_line_json) || |
+ !ImageLine::ParseImageLine(first_line_json, &answer->first_line_)) |
+ return false; |
+ const base::DictionaryValue* second_line_json; |
+ if (!lines_json->GetDictionary(1, &second_line_json) || |
+ !ImageLine::ParseImageLine(second_line_json, &answer->second_line_)) |
+ return false; |
+ |
+ answer->is_valid_ = true; |
+ |
+ return true; |
+} |
+ |
+bool SuggestionAnswer::operator==(const SuggestionAnswer& answer) const { |
+ return is_valid_ == answer.is_valid_ && |
+ type_ == answer.type_ && |
+ first_line_ == answer.first_line_ && |
+ second_line_ == answer.second_line_; |
+} |
+ |
+bool SuggestionAnswer::operator!=(const SuggestionAnswer& answer) const { |
+ return !operator==(answer); |
+} |
+ |
+void SuggestionAnswer::SetType(const base::string16& type) { |
+ if (!base::StringToInt(type, &type_)) |
+ type_ = 0; |
+} |
+ |
+void SuggestionAnswer::Clear() { |
+ first_line_.Clear(); |
+ second_line_.Clear(); |
+ type_ = 0; |
+ is_valid_ = false; |
+} |