Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2008 The Native Client Authors. All rights reserved. | 2 * Copyright 2008 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can | 3 * Use of this source code is governed by a BSD-style license that can |
| 4 * be found in the LICENSE file. | 4 * be found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 | 7 |
| 8 // Portable interface for browser interaction - NPAPI implementation | 8 // Portable interface for browser interaction - NPAPI implementation |
| 9 | 9 |
| 10 #include "native_client/src/trusted/plugin/npapi/browser_impl_npapi.h" | 10 #include "native_client/src/trusted/plugin/npapi/browser_impl_npapi.h" |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 } while (0); | 103 } while (0); |
| 104 | 104 |
| 105 // Free the dummy return from the evaluate. | 105 // Free the dummy return from the evaluate. |
| 106 NPN_ReleaseVariantValue(&dummy_return); | 106 NPN_ReleaseVariantValue(&dummy_return); |
| 107 // Free the string copy we allocated in CopyToNPString. | 107 // Free the string copy we allocated in CopyToNPString. |
| 108 FreeNPString(&str); | 108 FreeNPString(&str); |
| 109 | 109 |
| 110 return true; | 110 return true; |
| 111 } | 111 } |
| 112 | 112 |
| 113 bool BrowserImplNpapi::AddToConsole(InstanceIdentifier instance_id, | |
| 114 const nacl::string& text) { | |
| 115 #if defined(NACL_STANDALONE) | |
| 116 // We cannot be sure whether console.log is supported by the browser, | |
| 117 // so we use alerts instead. | |
| 118 return Alert(instance_id, text); | |
| 119 #else | |
| 120 bool success = false; | |
| 121 // Usually these messages are important enough to call attention to them. | |
| 122 puts(text.c_str()); | |
| 123 | |
| 124 NPObject* window = NULL; | |
| 125 NPP npp = InstanceIdentifierToNPP(instance_id); | |
| 126 if (NPN_GetValue(npp, NPNVWindowNPObject, &window) != NPERR_NO_ERROR) { | |
| 127 return false; | |
| 128 } | |
| 129 | |
| 130 NPVariant console_variant; | |
| 131 VOID_TO_NPVARIANT(console_variant); | |
|
Mark Seaborn
2010/08/20 18:45:32
If you're using gotos for cleanup you don't need t
| |
| 132 if (!NPN_GetProperty(npp, window, NPN_GetStringIdentifier("console"), | |
| 133 &console_variant)) { | |
| 134 goto cleanup_window; | |
| 135 } | |
| 136 if (!NPVARIANT_IS_OBJECT(console_variant)) { | |
| 137 goto cleanup_console_variant; | |
| 138 } | |
| 139 NPObject* console_object = NPVARIANT_TO_OBJECT(console_variant); | |
| 140 | |
| 141 NPVariant message; // doesn't hold its own string data, so don't release | |
| 142 STRINGN_TO_NPVARIANT(text.c_str(), | |
| 143 static_cast<uint32_t>(text.size()), | |
| 144 message); | |
| 145 | |
| 146 NPVariant result; | |
| 147 VOID_TO_NPVARIANT(result); | |
| 148 success = NPN_Invoke(npp, console_object, NPN_GetStringIdentifier("log"), | |
| 149 &message, 1, &result); | |
| 150 if (success) { | |
| 151 NPN_ReleaseVariantValue(&result); | |
| 152 } | |
| 153 cleanup_console_variant: | |
| 154 NPN_ReleaseVariantValue(&console_variant); | |
| 155 cleanup_window: | |
| 156 NPN_ReleaseObject(window); | |
| 157 return success; | |
| 158 #endif | |
| 159 } | |
| 160 | |
| 113 bool BrowserImplNpapi::Alert(InstanceIdentifier instance_id, | 161 bool BrowserImplNpapi::Alert(InstanceIdentifier instance_id, |
| 114 const nacl::string& text) { | 162 const nacl::string& text) { |
| 115 // Usually these messages are important enough to call attention to them. | 163 // Usually these messages are important enough to call attention to them. |
| 116 puts(text.c_str()); | 164 puts(text.c_str()); |
| 117 | 165 |
| 118 NPObject* window; | 166 NPObject* window; |
| 119 NPP npp = InstanceIdentifierToNPP(instance_id); | 167 NPP npp = InstanceIdentifierToNPP(instance_id); |
| 120 if (NPN_GetValue(npp, NPNVWindowNPObject, &window) != NPERR_NO_ERROR) { | 168 if (NPN_GetValue(npp, NPNVWindowNPObject, &window) != NPERR_NO_ERROR) { |
| 121 return false; | 169 return false; |
| 122 } | 170 } |
| 123 | 171 |
| 124 NPVariant message; // doesn't hold its own string data, so don't release | 172 NPVariant message; // doesn't hold its own string data, so don't release |
| 125 STRINGN_TO_NPVARIANT(text.c_str(), | 173 STRINGN_TO_NPVARIANT(text.c_str(), |
| 126 static_cast<uint32_t>(text.size()), | 174 static_cast<uint32_t>(text.size()), |
| 127 message); | 175 message); |
| 128 | 176 |
| 129 NPVariant result; | 177 NPVariant result; |
| 130 VOID_TO_NPVARIANT(result); | 178 VOID_TO_NPVARIANT(result); |
| 131 bool ok = NPN_Invoke(npp, window, NPN_GetStringIdentifier("alert"), | 179 bool ok = NPN_Invoke(npp, window, NPN_GetStringIdentifier("alert"), |
| 132 &message, 1, &result); | 180 &message, 1, &result); |
| 133 if (ok) NPN_ReleaseVariantValue(&result); | 181 if (ok) NPN_ReleaseVariantValue(&result); |
| 134 // TODO(adonovan): NPN_ReleaseObject(window) needed? | 182 NPN_ReleaseObject(window); |
| 135 return ok; | 183 return ok; |
| 136 } | 184 } |
| 137 | 185 |
| 138 bool BrowserImplNpapi::GetFullURL(InstanceIdentifier instance_id, | 186 bool BrowserImplNpapi::GetFullURL(InstanceIdentifier instance_id, |
| 139 nacl::string* full_url) { | 187 nacl::string* full_url) { |
| 140 NPP npp = InstanceIdentifierToNPP(instance_id); | 188 NPP npp = InstanceIdentifierToNPP(instance_id); |
| 141 NPObject* win_obj = NULL; | 189 NPObject* win_obj = NULL; |
| 142 NPVariant loc_value; | 190 NPVariant loc_value; |
| 143 NPVariant href_value; | 191 NPVariant href_value; |
| 144 | 192 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 192 | 240 |
| 193 NPP InstanceIdentifierToNPP(InstanceIdentifier id) { | 241 NPP InstanceIdentifierToNPP(InstanceIdentifier id) { |
| 194 return reinterpret_cast<NPP>(assert_cast<intptr_t>(id)); | 242 return reinterpret_cast<NPP>(assert_cast<intptr_t>(id)); |
| 195 } | 243 } |
| 196 | 244 |
| 197 InstanceIdentifier NPPToInstanceIdentifier(NPP npp) { | 245 InstanceIdentifier NPPToInstanceIdentifier(NPP npp) { |
| 198 return assert_cast<InstanceIdentifier>(reinterpret_cast<intptr_t>(npp)); | 246 return assert_cast<InstanceIdentifier>(reinterpret_cast<intptr_t>(npp)); |
| 199 } | 247 } |
| 200 | 248 |
| 201 } // namespace plugin | 249 } // namespace plugin |
| OLD | NEW |