Chromium Code Reviews| 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/command_line.h" | |
| 8 #include "base/json/json_writer.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "base/time.h" | |
| 11 #include "base/stringprintf.h" | |
|
Daniel Erat
2012/09/05 19:28:32
nit: alphabetize
zel
2012/09/05 19:56:03
Done.
| |
| 12 #include "base/utf_string_conversions.h" | |
| 13 #include "base/value_conversions.h" | |
| 14 #include "base/values.h" | |
| 15 #include "chrome/browser/chromeos/contacts/contact_manager.h" | |
| 16 #include "chrome/browser/chromeos/contacts/contact.pb.h" | |
| 17 #include "chrome/browser/extensions/event_router.h" | |
| 18 #include "chrome/browser/extensions/extension_service.h" | |
| 19 #include "chrome/browser/extensions/extension_system.h" | |
| 20 #include "chrome/browser/extensions/state_store.h" | |
| 21 #include "chrome/browser/profiles/profile.h" | |
| 22 #include "chrome/common/chrome_notification_types.h" | |
| 23 #include "chrome/common/chrome_switches.h" | |
| 24 #include "content/public/browser/notification_service.h" | |
| 25 | |
| 26 using base::DictionaryValue; | |
| 27 using base::ListValue; | |
| 28 using base::Value; | |
| 29 using contacts::Contact; | |
| 30 using contacts::Contact_EmailAddress; | |
| 31 using contacts::Contact_PhoneNumber; | |
| 32 | |
| 33 namespace extensions { | |
| 34 | |
| 35 namespace { | |
| 36 | |
| 37 // Launch event name. | |
| 38 const char kOnLaunchEvent[] = "rtcPrivate.onLaunch"; | |
| 39 // Default RTC extension id. | |
| 40 const char kDefaultRtcExtensionId[] = "ljclpkphhpbpinifbeabbhlfddcpfdde"; | |
| 41 // Web intent data payload mimetype. | |
| 42 const char kMimeTypeJson[] = "application/json"; | |
| 43 // Web intent actions. | |
| 44 const char kActivateAction[] = "activate"; | |
| 45 const char kChatAction[] = "chat"; | |
| 46 const char kVoiceAction[] = "voice"; | |
| 47 const char kVideoAction[] = "video"; | |
| 48 // Web intent data structure fields. | |
| 49 const char kNameIntentField[] = "name"; | |
| 50 const char kPhoneIntentField[] = "phone"; | |
| 51 const char kEmailIntentField[] = "email"; | |
| 52 | |
| 53 const char kIntentField[] = "intent"; | |
| 54 const char kActionField[] = "action"; | |
| 55 const char kDataField[] = "data"; | |
| 56 const char kTypeField[] = "type"; | |
| 57 | |
| 58 // Returns string representation of intent action. | |
| 59 const char* GetLaunchAction(RtcPrivateApi::LaunchAction action) { | |
| 60 const char* action_str = kActivateAction; | |
| 61 switch (action) { | |
| 62 case RtcPrivateApi::LAUNCH_ACTIVATE: | |
| 63 action_str = kActivateAction; | |
| 64 break; | |
| 65 case RtcPrivateApi::LAUNCH_CHAT: | |
| 66 action_str = kChatAction; | |
| 67 break; | |
| 68 case RtcPrivateApi::LAUNCH_VOICE: | |
| 69 action_str = kVoiceAction; | |
| 70 break; | |
| 71 case RtcPrivateApi::LAUNCH_VIDEO: | |
| 72 action_str = kVideoAction; | |
| 73 break; | |
|
Daniel Erat
2012/09/05 19:28:32
nit: add a default case with NOTREACHED() << "Unkn
zel
2012/09/05 19:56:03
Done.
| |
| 74 } | |
| 75 return action_str; | |
| 76 } | |
| 77 | |
| 78 // Creates JSON payload string for contact web intent data. | |
| 79 std::string GetContactIntentData(Contact* contact) { | |
| 80 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
|
Daniel Erat
2012/09/05 19:28:32
nit: any reason that you actually need a scoped_pt
zel
2012/09/05 19:56:03
Done.
| |
| 81 | |
| 82 // TODO(derat): This might require more name extraction magic than this. | |
| 83 dict->SetString(kNameIntentField, contact->full_name()); | |
| 84 | |
| 85 int i; | |
| 86 ListValue* phone_list = new base::ListValue(); | |
| 87 dict->Set(kPhoneIntentField, phone_list); | |
| 88 for (i = 0; i < contact->phone_numbers_size(); i++) { | |
|
Daniel Erat
2012/09/05 19:28:32
nit: move 'i' declaration into the loop condition
zel
2012/09/05 19:56:03
Done.
| |
| 89 const Contact_PhoneNumber& phone_number = contact->phone_numbers(i); | |
| 90 phone_list->Append(Value::CreateStringValue(phone_number.number())); | |
| 91 } | |
| 92 | |
| 93 ListValue* email_list = new base::ListValue(); | |
| 94 dict->Set(kPhoneIntentField, email_list); | |
| 95 for (i = 0; i < contact->email_addresses_size(); i++) { | |
|
Daniel Erat
2012/09/05 19:28:32
we talked earlier about sorting phone numbers and
zel
2012/09/05 19:56:03
Done.
| |
| 96 const Contact_EmailAddress& email_address = contact->email_addresses(i); | |
| 97 email_list->Append(Value::CreateStringValue(email_address.address())); | |
| 98 } | |
| 99 | |
| 100 std::string data; | |
| 101 base::JSONWriter::Write(dict.get(), &data); | |
| 102 return data; | |
| 103 } | |
| 104 | |
| 105 | |
| 106 } // namespace | |
| 107 | |
| 108 | |
| 109 void RtcPrivateApi::RaiseLaunchEvent(Profile* profile, | |
| 110 LaunchAction action, | |
| 111 Contact* contact) { | |
| 112 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 113 if (!command_line->HasSwitch(switches::kEnableContacts)) | |
|
Daniel Erat
2012/09/05 19:28:32
is this something that you actually need to check
zel
2012/09/05 19:56:03
Fair enough. Removed.
| |
| 114 return; | |
| 115 | |
| 116 scoped_ptr<ListValue> args(new ListValue()); | |
| 117 if (action != RtcPrivateApi::LAUNCH_ACTIVATE) { | |
| 118 DictionaryValue* launch_data = new DictionaryValue(); | |
| 119 args->Append(launch_data); | |
| 120 DictionaryValue* intent = new DictionaryValue(); | |
| 121 launch_data->Set(kIntentField, intent); | |
| 122 intent->SetString(kActionField, GetLaunchAction(action)); | |
| 123 intent->SetString(kDataField, GetContactIntentData(contact)); | |
| 124 intent->SetString(kTypeField, kMimeTypeJson); | |
| 125 } | |
| 126 | |
| 127 extensions::ExtensionSystem::Get(profile)->event_router()-> | |
| 128 DispatchEventToRenderers( | |
| 129 kOnLaunchEvent, | |
| 130 args.Pass(), | |
| 131 profile, | |
| 132 GURL()); | |
| 133 } | |
| 134 | |
| 135 } // namespace extensions | |
| OLD | NEW |