| OLD | NEW |
| (Empty) |
| 1 // Copyright 2010 Google Inc. All Rights Reserved. | |
| 2 // Author: ajwong@google.com (Albert Wong) | |
| 3 | |
| 4 #include "remoting/client/plugin/chromoting_scriptable_object.h" | |
| 5 | |
| 6 #include "remoting/client/client_config.h" | |
| 7 #include "remoting/client/plugin/chromoting_plugin.h" | |
| 8 | |
| 9 #include "third_party/ppapi/cpp/var.h" | |
| 10 | |
| 11 using pp::Var; | |
| 12 | |
| 13 namespace remoting { | |
| 14 ChromotingScriptableObject::ChromotingScriptableObject( | |
| 15 ChromotingPlugin* instance) | |
| 16 : instance_(instance) { | |
| 17 } | |
| 18 | |
| 19 ChromotingScriptableObject::~ChromotingScriptableObject() { | |
| 20 } | |
| 21 | |
| 22 void ChromotingScriptableObject::Init() { | |
| 23 // Property addition order should match the interface description at the | |
| 24 // top of chromoting_scriptable_object.h. | |
| 25 AddAttribute("onreadystatechange", Var()); | |
| 26 | |
| 27 AddAttribute("NOT_CONNECTED", Var(0)); | |
| 28 AddAttribute("CONNECTED", Var(1)); | |
| 29 | |
| 30 AddMethod("connect", &ChromotingScriptableObject::DoConnect); | |
| 31 | |
| 32 // TODO(ajwong): Figure out how to implement the status variable. | |
| 33 AddAttribute("status", Var("not_implemented_yet")); | |
| 34 } | |
| 35 | |
| 36 bool ChromotingScriptableObject::HasProperty(const Var& name, Var* exception) { | |
| 37 // TODO(ajwong): Check if all these name.is_string() sentinels are required. | |
| 38 if (!name.is_string()) { | |
| 39 *exception = Var("HasProperty expects a string for the name."); | |
| 40 return false; | |
| 41 } | |
| 42 | |
| 43 PropertyNameMap::const_iterator iter = property_names_.find(name.AsString()); | |
| 44 if (iter == property_names_.end()) { | |
| 45 return false; | |
| 46 } | |
| 47 | |
| 48 return properties_[iter->second].method == NULL; | |
| 49 } | |
| 50 | |
| 51 bool ChromotingScriptableObject::HasMethod(const Var& name, Var* exception) { | |
| 52 // TODO(ajwong): Check if all these name.is_string() sentinels are required. | |
| 53 if (!name.is_string()) { | |
| 54 *exception = Var("HasMethod expects a string for the name."); | |
| 55 return false; | |
| 56 } | |
| 57 | |
| 58 PropertyNameMap::const_iterator iter = property_names_.find(name.AsString()); | |
| 59 if (iter == property_names_.end()) { | |
| 60 return false; | |
| 61 } | |
| 62 | |
| 63 return properties_[iter->second].method != NULL; | |
| 64 } | |
| 65 | |
| 66 Var ChromotingScriptableObject::GetProperty(const Var& name, Var* exception) { | |
| 67 // TODO(ajwong): Check if all these name.is_string() sentinels are required. | |
| 68 if (!name.is_string()) { | |
| 69 *exception = Var("GetProperty expects a string for the name."); | |
| 70 return Var(); | |
| 71 } | |
| 72 | |
| 73 PropertyNameMap::const_iterator iter = property_names_.find(name.AsString()); | |
| 74 | |
| 75 // No property found. | |
| 76 if (iter == property_names_.end()) { | |
| 77 return ScriptableObject::GetProperty(name, exception); | |
| 78 } | |
| 79 | |
| 80 // TODO(ajwong): This incorrectly return a null object if a function | |
| 81 // property is requested. | |
| 82 return properties_[iter->second].attribute; | |
| 83 } | |
| 84 | |
| 85 void ChromotingScriptableObject::GetAllPropertyNames( | |
| 86 std::vector<Var>* properties, | |
| 87 Var* exception) { | |
| 88 for (size_t i = 0; i < properties_.size(); i++) { | |
| 89 properties->push_back(Var(properties_[i].name)); | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 void ChromotingScriptableObject::SetProperty(const Var& name, | |
| 94 const Var& value, | |
| 95 Var* exception) { | |
| 96 // TODO(ajwong): Check if all these name.is_string() sentinels are required. | |
| 97 if (!name.is_string()) { | |
| 98 *exception = Var("SetProperty expects a string for the name."); | |
| 99 return; | |
| 100 } | |
| 101 | |
| 102 // Only allow writing to onreadystatechange. See top of | |
| 103 // chromoting_scriptable_object.h for the object interface definition. | |
| 104 std::string property_name = name.AsString(); | |
| 105 if (property_name != "onstatechange") { | |
| 106 *exception = | |
| 107 Var("Cannot set property " + property_name + " on this object."); | |
| 108 return; | |
| 109 } | |
| 110 | |
| 111 // Since we're whitelisting the propertie that are settable above, we can | |
| 112 // assume that the property exists in the map. | |
| 113 properties_[property_names_[property_name]].attribute = value; | |
| 114 } | |
| 115 | |
| 116 Var ChromotingScriptableObject::Call(const Var& method_name, | |
| 117 const std::vector<Var>& args, | |
| 118 Var* exception) { | |
| 119 PropertyNameMap::const_iterator iter = | |
| 120 property_names_.find(method_name.AsString()); | |
| 121 if (iter == property_names_.end()) { | |
| 122 return pp::ScriptableObject::Call(method_name, args, exception); | |
| 123 } | |
| 124 | |
| 125 return (this->*(properties_[iter->second].method))(args, exception); | |
| 126 } | |
| 127 | |
| 128 void ChromotingScriptableObject::AddAttribute(const std::string& name, | |
| 129 Var attribute) { | |
| 130 property_names_[name] = properties_.size(); | |
| 131 properties_.push_back(PropertyDescriptor(name, attribute)); | |
| 132 } | |
| 133 | |
| 134 void ChromotingScriptableObject::AddMethod(const std::string& name, | |
| 135 MethodHandler handler) { | |
| 136 property_names_[name] = properties_.size(); | |
| 137 properties_.push_back(PropertyDescriptor(name, handler)); | |
| 138 } | |
| 139 | |
| 140 pp::Var ChromotingScriptableObject::DoConnect(const std::vector<Var>& args, | |
| 141 Var* exception) { | |
| 142 if (args.size() != 3) { | |
| 143 *exception = Var("Usage: connect(username, host_jid, auth_token"); | |
| 144 return Var(); | |
| 145 } | |
| 146 | |
| 147 ClientConfig config; | |
| 148 | |
| 149 if (!args[0].is_string()) { | |
| 150 *exception = Var("The username must be a string."); | |
| 151 return Var(); | |
| 152 } | |
| 153 config.username = args[0].AsString(); | |
| 154 | |
| 155 if (!args[1].is_string()) { | |
| 156 *exception = Var("The host_jid must be a string."); | |
| 157 return Var(); | |
| 158 } | |
| 159 config.host_jid = args[1].AsString(); | |
| 160 | |
| 161 if (!args[2].is_string()) { | |
| 162 *exception = Var("The auth_token must be a string."); | |
| 163 return Var(); | |
| 164 } | |
| 165 config.auth_token = args[2].AsString(); | |
| 166 | |
| 167 instance_->Connect(config); | |
| 168 | |
| 169 return Var(); | |
| 170 } | |
| 171 | |
| 172 } // namespace remoting | |
| OLD | NEW |