Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / app_list / search / people / person.cc
blob4c5b3f2e1d2eb10874921a611fb448daf6143133
1 // Copyright 2013 The Chromium Authors. 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.
5 #include "chrome/browser/ui/app_list/search/people/person.h"
7 #include <vector>
9 #include "base/strings/string_number_conversions.h"
10 #include "base/values.h"
12 namespace {
14 const char kKeyId[] = "person.id";
15 const char kKeyNames[] = "person.names";
16 const char kKeyDisplayName[] = "displayName";
17 const char kKeyEmails[] = "person.emails";
18 const char kKeyEmailValue[] = "value";
19 const char kKeyInteractionRank[] = "person.sortKeys.interactionRank";
20 const char kKeyImages[] = "person.images";
21 const char kKeyUrl[] = "url";
22 const char kKeyOwnerId[] = "person.metadata.ownerId";
24 // Finds a list in a dictionary, specified by list_key, then returns the
25 // first value associated with item_key in the first dictionary in that list.
26 // So, for this dictionary,
27 // { key_random: value1,
28 // list_key: [ { random_key: value2,
29 // item_key: TARGET_VALUE,
30 // another_random_key: value3 } ],
31 // another_key: value4 }
33 // we'll return TARGET_VALUE.
35 // The reason for this (seemingly) strange behavior is that several of our
36 // results are going to be in this form and we need to repeat this operation
37 // to parse them.
38 std::string GetTargetValue(const base::DictionaryValue& dict,
39 const char list_key[],
40 const char item_key[]) {
41 const base::ListValue* list;
42 if (!dict.GetList(list_key, &list) || !list)
43 return std::string();
45 base::ListValue::const_iterator it = list->begin();
46 if (it == list->end())
47 return std::string();
49 base::DictionaryValue* sub_dict;
50 if (!(*it)->GetAsDictionary(&sub_dict) || !sub_dict)
51 return std::string();
53 std::string value;
54 if (!sub_dict->GetString(item_key, &value))
55 return std::string();
57 return value;
60 } // namespace
63 namespace app_list {
65 // static
66 scoped_ptr<Person> Person::Create(const base::DictionaryValue& dict) {
67 scoped_ptr<Person> person(new Person());
69 // Person id's.
70 if (!dict.GetString(kKeyId, &person->id) ||
71 !dict.GetString(kKeyOwnerId, &person->owner_id)) {
72 person.reset();
73 return person.Pass();
76 // Interaction rank.
77 std::string interaction_rank_string;
78 if (!dict.GetString(kKeyInteractionRank, &interaction_rank_string) ||
79 !base::StringToDouble(
80 interaction_rank_string, &person->interaction_rank)) {
81 person.reset();
82 return person.Pass();
85 person->display_name = GetTargetValue(dict, kKeyNames, kKeyDisplayName);
86 person->email = GetTargetValue(dict, kKeyEmails, kKeyEmailValue);
87 person->image_url = GURL(GetTargetValue(dict, kKeyImages, kKeyUrl));
89 // If any of our values are invalid, null out our result.
90 if (person->id.empty() ||
91 person->owner_id.empty() ||
92 person->display_name.empty() ||
93 person->email.empty() ||
94 !person->image_url.is_valid() ||
95 person->interaction_rank == 0.0) {
96 person.reset();
99 return person.Pass();
102 Person::Person() : interaction_rank(0.0) {
105 Person::~Person() {
108 scoped_ptr<Person> Person::Duplicate() {
109 scoped_ptr<Person> person(new Person());
110 *person = *this;
111 return person.Pass();
114 } // namespace app_list