| 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.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 "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 { |
| 34 |
| 35 // Launch event name. |
| 36 const char kOnLaunchEvent[] = "rtcPrivate.onLaunch"; |
| 37 // Default RTC extension id. |
| 38 const char kDefaultRtcExtensionId[] = "ljclpkphhpbpinifbeabbhlfddcpfdde"; |
| 39 // Web intent data payload mimetype. |
| 40 const char kMimeTypeJson[] = "application/json"; |
| 41 // Web intent actions. |
| 42 const char kActivateAction[] = "activate"; |
| 43 const char kChatAction[] = "chat"; |
| 44 const char kVoiceAction[] = "voice"; |
| 45 const char kVideoAction[] = "video"; |
| 46 // Web intent data structure fields. |
| 47 const char kNameIntentField[] = "name"; |
| 48 const char kPhoneIntentField[] = "phone"; |
| 49 const char kEmailIntentField[] = "email"; |
| 50 |
| 51 const char kIntentField[] = "intent"; |
| 52 const char kActionField[] = "action"; |
| 53 const char kDataField[] = "data"; |
| 54 const char kTypeField[] = "type"; |
| 55 |
| 56 // Returns string representation of intent action. |
| 57 const char* GetLaunchAction(RtcPrivateApi::LaunchAction action) { |
| 58 const char* action_str = kActivateAction; |
| 59 switch (action) { |
| 60 case RtcPrivateApi::LAUNCH_ACTIVATE: |
| 61 action_str = kActivateAction; |
| 62 break; |
| 63 case RtcPrivateApi::LAUNCH_CHAT: |
| 64 action_str = kChatAction; |
| 65 break; |
| 66 case RtcPrivateApi::LAUNCH_VOICE: |
| 67 action_str = kVoiceAction; |
| 68 break; |
| 69 case RtcPrivateApi::LAUNCH_VIDEO: |
| 70 action_str = kVideoAction; |
| 71 break; |
| 72 default: |
| 73 NOTREACHED() << "Unknown action " << action; |
| 74 break; |
| 75 } |
| 76 return action_str; |
| 77 } |
| 78 |
| 79 // Creates JSON payload string for contact web intent data. |
| 80 std::string GetContactIntentData(Contact* contact) { |
| 81 base::DictionaryValue dict; |
| 82 |
| 83 // TODO(derat): This might require more name extraction magic than this. |
| 84 dict.SetString(kNameIntentField, contact->full_name()); |
| 85 |
| 86 ListValue* phone_list = new base::ListValue(); |
| 87 dict.Set(kPhoneIntentField, phone_list); |
| 88 for (int i = 0; i < contact->phone_numbers_size(); i++) { |
| 89 const Contact_PhoneNumber& phone_number = contact->phone_numbers(i); |
| 90 StringValue* value = Value::CreateStringValue(phone_number.number()); |
| 91 if (phone_number.primary()) |
| 92 CHECK(phone_list->Insert(0, value)); |
| 93 else |
| 94 phone_list->Append(value); |
| 95 } |
| 96 |
| 97 ListValue* email_list = new base::ListValue(); |
| 98 dict.Set(kPhoneIntentField, email_list); |
| 99 for (int i = 0; i < contact->email_addresses_size(); i++) { |
| 100 const Contact_EmailAddress& email_address = contact->email_addresses(i); |
| 101 StringValue* value = Value::CreateStringValue(email_address.address()); |
| 102 if (email_address.primary()) |
| 103 CHECK(email_list->Insert(0, value)); |
| 104 else |
| 105 email_list->Append(value); |
| 106 } |
| 107 |
| 108 std::string data; |
| 109 base::JSONWriter::Write(&dict, &data); |
| 110 return data; |
| 111 } |
| 112 |
| 113 |
| 114 } // namespace |
| 115 |
| 116 |
| 117 void RtcPrivateApi::RaiseLaunchEvent(Profile* profile, |
| 118 LaunchAction action, |
| 119 Contact* contact) { |
| 120 scoped_ptr<ListValue> args(new ListValue()); |
| 121 if (action != RtcPrivateApi::LAUNCH_ACTIVATE) { |
| 122 DictionaryValue* launch_data = new DictionaryValue(); |
| 123 args->Append(launch_data); |
| 124 DictionaryValue* intent = new DictionaryValue(); |
| 125 launch_data->Set(kIntentField, intent); |
| 126 intent->SetString(kActionField, GetLaunchAction(action)); |
| 127 if (contact) { |
| 128 intent->SetString(kDataField, GetContactIntentData(contact)); |
| 129 intent->SetString(kTypeField, kMimeTypeJson); |
| 130 } |
| 131 } |
| 132 |
| 133 extensions::ExtensionSystem::Get(profile)->event_router()-> |
| 134 DispatchEventToRenderers( |
| 135 kOnLaunchEvent, |
| 136 args.Pass(), |
| 137 profile, |
| 138 GURL()); |
| 139 } |
| 140 |
| 141 } // namespace extensions |
| OLD | NEW |