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"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/values.h"
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
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
)
45 base::ListValue::const_iterator it
= list
->begin();
46 if (it
== list
->end())
49 base::DictionaryValue
* sub_dict
;
50 if (!(*it
)->GetAsDictionary(&sub_dict
) || !sub_dict
)
54 if (!sub_dict
->GetString(item_key
, &value
))
66 scoped_ptr
<Person
> Person::Create(const base::DictionaryValue
& dict
) {
67 scoped_ptr
<Person
> person(new Person());
70 if (!dict
.GetString(kKeyId
, &person
->id
) ||
71 !dict
.GetString(kKeyOwnerId
, &person
->owner_id
)) {
77 std::string interaction_rank_string
;
78 if (!dict
.GetString(kKeyInteractionRank
, &interaction_rank_string
) ||
79 !base::StringToDouble(
80 interaction_rank_string
, &person
->interaction_rank
)) {
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) {
102 Person::Person() : interaction_rank(0.0) {
108 scoped_ptr
<Person
> Person::Duplicate() {
109 scoped_ptr
<Person
> person(new Person());
111 return person
.Pass();
114 } // namespace app_list