Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: chrome/browser/extensions/api/rtc_private/rtc_private_api.cc

Issue 10919086: Wired chrome.rtcPrivate API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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.h"
9 #include "base/stringprintf.h"
10 #include "base/time.h"
11 #include "base/utf_string_conversions.h"
12 #include "base/value_conversions.h"
13 #include "base/values.h"
14 #include "chrome/browser/chromeos/contacts/contact_manager.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/chrome_notification_types.h"
22 #include "chrome/common/extensions/api/rtc_private.h"
23 #include "content/public/browser/notification_service.h"
24
25 using base::DictionaryValue;
26 using base::ListValue;
27 using base::Value;
28 using contacts::Contact;
29 using contacts::Contact_EmailAddress;
30 using contacts::Contact_PhoneNumber;
31
32 namespace extensions {
33
34 namespace {
35
36 // Launch event name.
37 const char kOnLaunchEvent[] = "rtcPrivate.onLaunch";
38 // Web intent data payload mimetype.
39 const char kMimeTypeJson[] = "application/vnd.chromium.contact";
40 // Web intent actions.
41 const char kActivateAction[] = "activate";
42 const char kChatAction[] = "chat";
43 const char kVoiceAction[] = "voice";
44 const char kVideoAction[] = "video";
45 // Web intent data structure fields.
46 const char kNameIntentField[] = "name";
47 const char kPhoneIntentField[] = "phone";
48 const char kEmailIntentField[] = "email";
49
50 // Returns string representation of intent action.
51 const char* GetLaunchAction(RtcPrivateEventRouter::LaunchAction action) {
52 const char* action_str = kActivateAction;
53 switch (action) {
54 case RtcPrivateEventRouter::LAUNCH_ACTIVATE:
55 action_str = kActivateAction;
56 break;
57 case RtcPrivateEventRouter::LAUNCH_CHAT:
58 action_str = kChatAction;
59 break;
60 case RtcPrivateEventRouter::LAUNCH_VOICE:
61 action_str = kVoiceAction;
62 break;
63 case RtcPrivateEventRouter::LAUNCH_VIDEO:
64 action_str = kVideoAction;
65 break;
66 default:
67 NOTREACHED() << "Unknown action " << action;
68 break;
69 }
70 return action_str;
71 }
72
73 // Creates JSON payload string for contact web intent data.
74 void GetContactIntentData(Contact* contact,
75 DictionaryValue* dict) {
76 // TODO(derat): This might require more name extraction magic than this.
77 dict->SetString(kNameIntentField, contact->full_name());
78
79 ListValue* phone_list = new base::ListValue();
80 dict->Set(kPhoneIntentField, phone_list);
81 for (int i = 0; i < contact->phone_numbers_size(); i++) {
82 const Contact_PhoneNumber& phone_number = contact->phone_numbers(i);
83 StringValue* value = Value::CreateStringValue(phone_number.number());
84 if (phone_number.primary())
85 CHECK(phone_list->Insert(0, value));
86 else
87 phone_list->Append(value);
88 }
89
90 ListValue* email_list = new base::ListValue();
91 dict->Set(kEmailIntentField, email_list);
92 for (int i = 0; i < contact->email_addresses_size(); i++) {
93 const Contact_EmailAddress& email_address = contact->email_addresses(i);
94 StringValue* value = Value::CreateStringValue(email_address.address());
95 if (email_address.primary())
96 CHECK(email_list->Insert(0, value));
97 else
98 email_list->Append(value);
99 }
100 }
101
102 } // namespace
103
104 void RtcPrivateEventRouter::DispatchLaunchEvent(
105 Profile* profile, LaunchAction action, Contact* contact) {
106 if (action == RtcPrivateEventRouter::LAUNCH_ACTIVATE) {
107 extensions::ExtensionSystem::Get(profile)->event_router()->
108 DispatchEventToRenderers(
109 kOnLaunchEvent,
110 scoped_ptr<ListValue>(new ListValue()),
111 profile,
112 GURL());
113 } else {
114 extensions::api::rtc_private::LaunchData launch_data;
115 launch_data.intent.action = GetLaunchAction(action);
116 GetContactIntentData(contact,
117 &launch_data.intent.data.additional_properties);
118 launch_data.intent.type = kMimeTypeJson;
119 extensions::ExtensionSystem::Get(profile)->event_router()->
120 DispatchEventToRenderers(
121 kOnLaunchEvent,
122 extensions::api::rtc_private::OnLaunch::Create(launch_data),
123 profile,
124 GURL());
125 }
126 }
127
128 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698