Chromium Code Reviews| 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 "components/search_provider_logos/google_logo_api.h" | 5 #include "components/search_provider_logos/google_logo_api.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 64 // "url": "http://www.google.com/logos/doodle.png", | 64 // "url": "http://www.google.com/logos/doodle.png", |
| 65 // "alt": "Wilbur Christiansen's Birthday" | 65 // "alt": "Wilbur Christiansen's Birthday" |
| 66 // "time_to_live": 1389304799 | 66 // "time_to_live": 1389304799 |
| 67 // }}} | 67 // }}} |
| 68 | 68 |
| 69 // The response may start with )]}'. Ignore this. | 69 // The response may start with )]}'. Ignore this. |
| 70 base::StringPiece response_sp(*response); | 70 base::StringPiece response_sp(*response); |
| 71 if (response_sp.starts_with(kResponsePreamble)) | 71 if (response_sp.starts_with(kResponsePreamble)) |
| 72 response_sp.remove_prefix(strlen(kResponsePreamble)); | 72 response_sp.remove_prefix(strlen(kResponsePreamble)); |
| 73 | 73 |
| 74 scoped_ptr<base::Value> value = base::JSONReader::Read(response_sp); | |
| 75 | |
| 76 // Check if no logo today. | |
| 77 if (!value.get()) { | |
| 78 *parsing_failed = false; | |
| 79 return scoped_ptr<EncodedLogo>(); | |
| 80 } | |
| 81 | |
| 82 // Default parsing failure to be true. | 74 // Default parsing failure to be true. |
| 83 *parsing_failed = true; | 75 *parsing_failed = true; |
|
newt (away)
2016/01/22 03:11:20
I'd move this line to very beginning of the functi
| |
| 76 scoped_ptr<base::Value> value = base::JSONReader::Read(response_sp); | |
| 77 if (!value.get()) | |
| 78 return scoped_ptr<EncodedLogo>(); | |
| 84 // The important data lives inside several nested dictionaries: | 79 // The important data lives inside several nested dictionaries: |
| 85 // {"update": {"logo": { "mime_type": ..., etc } } } | 80 // {"update": {"logo": { "mime_type": ..., etc } } } |
| 86 const base::DictionaryValue* outer_dict; | 81 const base::DictionaryValue* outer_dict; |
| 87 if (!value->GetAsDictionary(&outer_dict)) | 82 if (!value->GetAsDictionary(&outer_dict)) |
| 88 return scoped_ptr<EncodedLogo>(); | 83 return scoped_ptr<EncodedLogo>(); |
| 89 const base::DictionaryValue* update_dict; | 84 const base::DictionaryValue* update_dict; |
| 90 if (!outer_dict->GetDictionary("update", &update_dict)) | 85 if (!outer_dict->GetDictionary("update", &update_dict)) |
| 91 return scoped_ptr<EncodedLogo>(); | 86 return scoped_ptr<EncodedLogo>(); |
| 87 | |
| 88 // If there is no logo today, the server sends out an empty update. | |
|
newt (away)
2016/01/22 03:11:20
Slightly less ambiguous, I think: "If there is no
| |
| 89 if (update_dict->empty()) { | |
| 90 *parsing_failed = false; | |
| 91 return scoped_ptr<EncodedLogo>(); | |
| 92 } | |
| 93 | |
| 92 const base::DictionaryValue* logo_dict; | 94 const base::DictionaryValue* logo_dict; |
| 93 if (!update_dict->GetDictionary("logo", &logo_dict)) | 95 if (!update_dict->GetDictionary("logo", &logo_dict)) |
| 94 return scoped_ptr<EncodedLogo>(); | 96 return scoped_ptr<EncodedLogo>(); |
| 95 | 97 |
| 96 scoped_ptr<EncodedLogo> logo(new EncodedLogo()); | 98 scoped_ptr<EncodedLogo> logo(new EncodedLogo()); |
| 97 | 99 |
| 98 std::string encoded_image_base64; | 100 std::string encoded_image_base64; |
| 99 if (logo_dict->GetString("data", &encoded_image_base64)) { | 101 if (logo_dict->GetString("data", &encoded_image_base64)) { |
| 100 // Data is optional, since we may be revalidating a cached logo. | 102 // Data is optional, since we may be revalidating a cached logo. |
| 101 base::RefCountedString* encoded_image_string = new base::RefCountedString(); | 103 base::RefCountedString* encoded_image_string = new base::RefCountedString(); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 127 logo->metadata.can_show_after_expiration = true; | 129 logo->metadata.can_show_after_expiration = true; |
| 128 } | 130 } |
| 129 logo->metadata.expiration_time = response_time + time_to_live; | 131 logo->metadata.expiration_time = response_time + time_to_live; |
| 130 | 132 |
| 131 // If this point is reached, parsing has succeeded. | 133 // If this point is reached, parsing has succeeded. |
| 132 *parsing_failed = false; | 134 *parsing_failed = false; |
| 133 return logo; | 135 return logo; |
| 134 } | 136 } |
| 135 | 137 |
| 136 } // namespace search_provider_logos | 138 } // namespace search_provider_logos |
| OLD | NEW |