OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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/extensions/api/rtc_private/rtc_private_api.h" | |
6 | |
7 #include "base/json/json_writer.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "base/strings/stringprintf.h" | |
10 #include "base/strings/utf_string_conversions.h" | |
11 #include "base/time/time.h" | |
12 #include "base/value_conversions.h" | |
13 #include "base/values.h" | |
14 #include "chrome/browser/chrome_notification_types.h" | |
15 #include "chrome/browser/chromeos/contacts/contact.pb.h" | |
16 #include "chrome/browser/extensions/event_router.h" | |
17 #include "chrome/browser/extensions/extension_service.h" | |
18 #include "chrome/browser/extensions/extension_system.h" | |
19 #include "chrome/browser/extensions/state_store.h" | |
20 #include "chrome/browser/profiles/profile.h" | |
21 #include "chrome/common/extensions/api/rtc_private.h" | |
22 #include "content/public/browser/notification_service.h" | |
23 | |
24 using base::DictionaryValue; | |
25 using base::ListValue; | |
26 using base::Value; | |
27 using contacts::Contact; | |
28 using contacts::Contact_EmailAddress; | |
29 using contacts::Contact_PhoneNumber; | |
30 | |
31 namespace extensions { | |
32 | |
33 namespace rtc_private = api::rtc_private; | |
34 | |
35 namespace { | |
36 | |
37 // Web intent data payload mimetype. | |
38 const char kMimeTypeJson[] = "application/vnd.chromium.contact"; | |
39 // Web intent data structure fields. | |
40 const char kNameIntentField[] = "name"; | |
41 const char kPhoneIntentField[] = "phone"; | |
42 const char kEmailIntentField[] = "email"; | |
43 | |
44 // Returns the ActionType of intent action. | |
45 api::rtc_private::ActionType GetLaunchAction( | |
46 RtcPrivateEventRouter::LaunchAction action) { | |
47 switch (action) { | |
48 case RtcPrivateEventRouter::LAUNCH_ACTIVATE: | |
49 return api::rtc_private::ACTION_TYPE_NONE; | |
50 case RtcPrivateEventRouter::LAUNCH_CHAT: | |
51 return api::rtc_private::ACTION_TYPE_CHAT; | |
52 case RtcPrivateEventRouter::LAUNCH_VOICE: | |
53 return api::rtc_private::ACTION_TYPE_VOICE; | |
54 case RtcPrivateEventRouter::LAUNCH_VIDEO: | |
55 return api::rtc_private::ACTION_TYPE_VIDEO; | |
56 } | |
57 return api::rtc_private::ACTION_TYPE_NONE; | |
58 } | |
59 | |
60 // Creates JSON payload string for contact web intent data. | |
61 void GetContactIntentData(const Contact& contact, | |
62 DictionaryValue* dict) { | |
63 // TODO(derat): This might require more name extraction magic than this. | |
64 dict->SetString(kNameIntentField, contact.full_name()); | |
65 | |
66 ListValue* phone_list = new base::ListValue(); | |
67 dict->Set(kPhoneIntentField, phone_list); | |
68 for (int i = 0; i < contact.phone_numbers_size(); i++) { | |
69 const Contact_PhoneNumber& phone_number = contact.phone_numbers(i); | |
70 StringValue* value = new base::StringValue(phone_number.number()); | |
71 if (phone_number.primary()) | |
72 CHECK(phone_list->Insert(0, value)); | |
73 else | |
74 phone_list->Append(value); | |
75 } | |
76 | |
77 ListValue* email_list = new base::ListValue(); | |
78 dict->Set(kEmailIntentField, email_list); | |
79 for (int i = 0; i < contact.email_addresses_size(); i++) { | |
80 const Contact_EmailAddress& email_address = contact.email_addresses(i); | |
81 StringValue* value = new base::StringValue(email_address.address()); | |
82 if (email_address.primary()) | |
83 CHECK(email_list->Insert(0, value)); | |
84 else | |
85 email_list->Append(value); | |
86 } | |
87 } | |
88 | |
89 } // namespace | |
90 | |
91 void RtcPrivateEventRouter::DispatchLaunchEvent( | |
92 Profile* profile, LaunchAction action, const Contact* contact) { | |
93 if (action == RtcPrivateEventRouter::LAUNCH_ACTIVATE) { | |
94 scoped_ptr<Event> event(new Event( | |
95 rtc_private::OnLaunch::kEventName, make_scoped_ptr(new ListValue()))); | |
96 event->restrict_to_profile = profile; | |
97 ExtensionSystem::Get(profile)->event_router()->BroadcastEvent(event.Pass()); | |
98 } else { | |
99 DCHECK(contact); | |
100 extensions::api::rtc_private::LaunchData launch_data; | |
101 launch_data.intent.action = GetLaunchAction(action); | |
102 GetContactIntentData(*contact, | |
103 &launch_data.intent.data.additional_properties); | |
104 launch_data.intent.type = kMimeTypeJson; | |
105 scoped_ptr<Event> event(new Event( | |
106 rtc_private::OnLaunch::kEventName, | |
107 api::rtc_private::OnLaunch::Create(launch_data))); | |
108 event->restrict_to_profile = profile; | |
109 ExtensionSystem::Get(profile)->event_router()->BroadcastEvent(event.Pass()); | |
110 } | |
111 } | |
112 | |
113 } // namespace extensions | |
OLD | NEW |