4c5b3f2e1d2eb10874921a611fb448daf6143133
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / app_list / search / people / person.cc
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.
4
5 #include "chrome/browser/ui/app_list/search/people/person.h"
6
7 #include <vector>
8
9 #include "base/strings/string_number_conversions.h"
10 #include "base/values.h"
11
12 namespace {
13
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";
23
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 }
32 //
33 //  we'll return TARGET_VALUE.
34 //
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();
44
45   base::ListValue::const_iterator it = list->begin();
46   if (it == list->end())
47     return std::string();
48
49   base::DictionaryValue* sub_dict;
50   if (!(*it)->GetAsDictionary(&sub_dict) || !sub_dict)
51     return std::string();
52
53   std::string value;
54   if (!sub_dict->GetString(item_key, &value))
55     return std::string();
56
57   return value;
58 }
59
60 }  // namespace
61
62
63 namespace app_list {
64
65 // static
66 scoped_ptr<Person> Person::Create(const base::DictionaryValue& dict) {
67   scoped_ptr<Person> person(new Person());
68
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();
74   }
75
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();
83   }
84
85   person->display_name = GetTargetValue(dict, kKeyNames, kKeyDisplayName);
86   person->email = GetTargetValue(dict, kKeyEmails, kKeyEmailValue);
87   person->image_url = GURL(GetTargetValue(dict, kKeyImages, kKeyUrl));
88
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();
97   }
98
99   return person.Pass();
100 }
101
102 Person::Person() : interaction_rank(0.0) {
103 }
104
105 Person::~Person() {
106 }
107
108 scoped_ptr<Person> Person::Duplicate() {
109   scoped_ptr<Person> person(new Person());
110   *person = *this;
111   return person.Pass();
112 }
113
114 }  // namespace app_list