You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
7.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2007 Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <sstream>
  28. #include "talk/base/common.h"
  29. #include "talk/base/logging.h"
  30. #include "talk/base/macutils.h"
  31. #include "talk/base/scoped_ptr.h"
  32. #include "talk/base/stringutils.h"
  33. namespace talk_base {
  34. ///////////////////////////////////////////////////////////////////////////////
  35. bool ToUtf8(const CFStringRef str16, std::string* str8) {
  36. if ((NULL == str16) || (NULL == str8))
  37. return false;
  38. size_t maxlen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str16),
  39. kCFStringEncodingUTF8)
  40. + 1;
  41. scoped_array<char> buffer(new char[maxlen]);
  42. if (!buffer || !CFStringGetCString(str16, buffer.get(), maxlen,
  43. kCFStringEncodingUTF8))
  44. return false;
  45. str8->assign(buffer.get());
  46. return true;
  47. }
  48. bool ToUtf16(const std::string& str8, CFStringRef* str16) {
  49. if (NULL == str16)
  50. return false;
  51. *str16 = CFStringCreateWithBytes(kCFAllocatorDefault,
  52. reinterpret_cast<const UInt8*>(str8.data()),
  53. str8.length(), kCFStringEncodingUTF8,
  54. false);
  55. return (NULL != *str16);
  56. }
  57. #ifdef OSX
  58. void DecodeFourChar(UInt32 fc, std::string* out) {
  59. std::stringstream ss;
  60. ss << '\'';
  61. bool printable = true;
  62. for (int i = 3; i >= 0; --i) {
  63. char ch = (fc >> (8 * i)) & 0xFF;
  64. if (isprint(static_cast<unsigned char>(ch))) {
  65. ss << ch;
  66. } else {
  67. printable = false;
  68. break;
  69. }
  70. }
  71. if (printable) {
  72. ss << '\'';
  73. } else {
  74. ss.str("");
  75. ss << "0x" << std::hex << fc;
  76. }
  77. out->append(ss.str());
  78. }
  79. static bool GetGestalt(OSType ostype, int* value) {
  80. ASSERT(NULL != value);
  81. SInt32 native_value;
  82. OSStatus result = Gestalt(ostype, &native_value);
  83. if (noErr == result) {
  84. *value = native_value;
  85. return true;
  86. }
  87. std::string str;
  88. DecodeFourChar(ostype, &str);
  89. LOG_E(LS_ERROR, OS, result) << "Gestalt(" << str << ")";
  90. return false;
  91. }
  92. bool GetOSVersion(int* major, int* minor, int* bugfix) {
  93. ASSERT(major && minor && bugfix);
  94. if (!GetGestalt(gestaltSystemVersion, major))
  95. return false;
  96. if (*major < 0x1040) {
  97. *bugfix = *major & 0xF;
  98. *minor = (*major >> 4) & 0xF;
  99. *major = (*major >> 8);
  100. return true;
  101. }
  102. return GetGestalt(gestaltSystemVersionMajor, major)
  103. && GetGestalt(gestaltSystemVersionMinor, minor)
  104. && GetGestalt(gestaltSystemVersionBugFix, bugfix);
  105. }
  106. MacOSVersionName GetOSVersionName() {
  107. int major = 0, minor = 0, bugfix = 0;
  108. if (!GetOSVersion(&major, &minor, &bugfix))
  109. return kMacOSUnknown;
  110. if (major > 10) {
  111. return kMacOSNewer;
  112. }
  113. if ((major < 10) || (minor < 3)) {
  114. return kMacOSOlder;
  115. }
  116. switch (minor) {
  117. case 3:
  118. return kMacOSPanther;
  119. case 4:
  120. return kMacOSTiger;
  121. case 5:
  122. return kMacOSLeopard;
  123. case 6:
  124. return kMacOSSnowLeopard;
  125. case 7:
  126. return kMacOSLion;
  127. case 8:
  128. return kMacOSMountainLion;
  129. }
  130. return kMacOSNewer;
  131. }
  132. bool GetQuickTimeVersion(std::string* out) {
  133. int ver;
  134. if (!GetGestalt(gestaltQuickTimeVersion, &ver))
  135. return false;
  136. std::stringstream ss;
  137. ss << std::hex << ver;
  138. *out = ss.str();
  139. return true;
  140. }
  141. bool RunAppleScript(const std::string& script) {
  142. // TODO(thaloun): Add a .mm file that contains something like this:
  143. // NSString source from script
  144. // NSAppleScript* appleScript = [[NSAppleScript alloc] initWithSource:&source]
  145. // if (appleScript != nil) {
  146. // [appleScript executeAndReturnError:nil]
  147. // [appleScript release]
  148. #ifndef CARBON_DEPRECATED
  149. ComponentInstance component = NULL;
  150. AEDesc script_desc;
  151. AEDesc result_data;
  152. OSStatus err;
  153. OSAID script_id, result_id;
  154. AECreateDesc(typeNull, NULL, 0, &script_desc);
  155. AECreateDesc(typeNull, NULL, 0, &result_data);
  156. script_id = kOSANullScript;
  157. result_id = kOSANullScript;
  158. component = OpenDefaultComponent(kOSAComponentType, typeAppleScript);
  159. if (component == NULL) {
  160. LOG(LS_ERROR) << "Failed opening Apple Script component";
  161. return false;
  162. }
  163. err = AECreateDesc(typeUTF8Text, script.data(), script.size(), &script_desc);
  164. if (err != noErr) {
  165. CloseComponent(component);
  166. LOG(LS_ERROR) << "Failed creating Apple Script description";
  167. return false;
  168. }
  169. err = OSACompile(component, &script_desc, kOSAModeCanInteract, &script_id);
  170. if (err != noErr) {
  171. AEDisposeDesc(&script_desc);
  172. if (script_id != kOSANullScript) {
  173. OSADispose(component, script_id);
  174. }
  175. CloseComponent(component);
  176. LOG(LS_ERROR) << "Error compiling Apple Script";
  177. return false;
  178. }
  179. err = OSAExecute(component, script_id, kOSANullScript, kOSAModeCanInteract,
  180. &result_id);
  181. if (err == errOSAScriptError) {
  182. LOG(LS_ERROR) << "Error when executing Apple Script: " << script;
  183. AECreateDesc(typeNull, NULL, 0, &result_data);
  184. OSAScriptError(component, kOSAErrorMessage, typeChar, &result_data);
  185. int len = AEGetDescDataSize(&result_data);
  186. char* data = (char*) malloc(len);
  187. if (data != NULL) {
  188. err = AEGetDescData(&result_data, data, len);
  189. LOG(LS_ERROR) << "Script error: " << data;
  190. }
  191. AEDisposeDesc(&script_desc);
  192. AEDisposeDesc(&result_data);
  193. return false;
  194. }
  195. AEDisposeDesc(&script_desc);
  196. if (script_id != kOSANullScript) {
  197. OSADispose(component, script_id);
  198. }
  199. if (result_id != kOSANullScript) {
  200. OSADispose(component, result_id);
  201. }
  202. CloseComponent(component);
  203. return true;
  204. #else
  205. // TODO(thaloun): Support applescripts with the NSAppleScript API.
  206. return false;
  207. #endif // CARBON_DEPRECATED
  208. }
  209. #endif // OSX
  210. ///////////////////////////////////////////////////////////////////////////////
  211. } // namespace talk_base