| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 library browser; | 4 library browser; |
| 5 | 5 |
| 6 import "dart:async"; | 6 import "dart:async"; |
| 7 import "dart:core"; | 7 import "dart:core"; |
| 8 import "dart:io"; | 8 import "dart:io"; |
| 9 | 9 |
| 10 import 'android.dart'; | 10 import 'android.dart'; |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 * The binary used to run safari - changing this can be nececcary for | 176 * The binary used to run safari - changing this can be nececcary for |
| 177 * testing or using non standard safari installation. | 177 * testing or using non standard safari installation. |
| 178 */ | 178 */ |
| 179 const String binary = "/Applications/Safari.app/Contents/MacOS/Safari"; | 179 const String binary = "/Applications/Safari.app/Contents/MacOS/Safari"; |
| 180 | 180 |
| 181 /** | 181 /** |
| 182 * We get the safari version by parsing a version file | 182 * We get the safari version by parsing a version file |
| 183 */ | 183 */ |
| 184 const String versionFile = "/Applications/Safari.app/Contents/version.plist"; | 184 const String versionFile = "/Applications/Safari.app/Contents/version.plist"; |
| 185 | 185 |
| 186 /** |
| 187 * Directories where safari stores state. We delete these if the deleteCache |
| 188 * is set |
| 189 */ |
| 190 static const List<String> CACHE_DIRECTORIES = |
| 191 const ["Library/Caches/com.apple.Safari", |
| 192 "Library/Safari"]; |
| 193 |
| 194 |
| 186 Future<bool> allowPopUps() { | 195 Future<bool> allowPopUps() { |
| 187 var command = "defaults"; | 196 var command = "defaults"; |
| 188 var args = ["write", "com.apple.safari", | 197 var args = ["write", "com.apple.safari", |
| 189 "com.apple.Safari.ContentPageGroupIdentifier." | 198 "com.apple.Safari.ContentPageGroupIdentifier." |
| 190 "WebKit2JavaScriptCanOpenWindowsAutomatically", | 199 "WebKit2JavaScriptCanOpenWindowsAutomatically", |
| 191 "1"]; | 200 "1"]; |
| 192 return Process.run(command, args).then((result) { | 201 return Process.run(command, args).then((result) { |
| 193 if (result.exitCode != 0) { | 202 if (result.exitCode != 0) { |
| 194 _logEvent("Could not disable pop-up blocking for safari"); | 203 _logEvent("Could not disable pop-up blocking for safari"); |
| 195 return false; | 204 return false; |
| 196 } | 205 } |
| 197 return true; | 206 return true; |
| 198 }); | 207 }); |
| 199 } | 208 } |
| 200 | 209 |
| 210 Future<bool> deleteIfExists(Iterator<String> paths) { |
| 211 if (!paths.moveNext()) return new Future.immediate(true); |
| 212 Directory directory = new Directory(paths.current); |
| 213 return directory.exists().then((exists) { |
| 214 if (exists) { |
| 215 _logEvent("Deleting ${paths.current}"); |
| 216 return directory.delete(recursive: true) |
| 217 .then((_) => deleteIfExists(paths)) |
| 218 .catchError((error) { |
| 219 _logEvent("Failure trying to delete ${paths.current}: $error"); |
| 220 return false; |
| 221 }); |
| 222 } else { |
| 223 _logEvent("${paths.current} is not present"); |
| 224 return deleteIfExists(paths); |
| 225 } |
| 226 }); |
| 227 } |
| 228 |
| 201 // Clears the cache if the static deleteCache flag is set. | 229 // Clears the cache if the static deleteCache flag is set. |
| 202 // Returns false if the command to actually clear the cache did not complete. | 230 // Returns false if the command to actually clear the cache did not complete. |
| 203 Future<bool> clearCache() { | 231 Future<bool> clearCache() { |
| 204 if (!deleteCache) return new Future.immediate(true); | 232 if (!deleteCache) return new Future.immediate(true); |
| 205 var home = Platform.environment['HOME']; | 233 var home = Platform.environment['HOME']; |
| 206 var cache = "$home/Library/Caches/com.apple.Safari"; | 234 Iterator iterator = CACHE_DIRECTORIES.map((s) => "$home/$s").iterator; |
| 207 _logEvent("Clearing safari cache: $cache}"); | 235 return deleteIfExists(iterator); |
| 208 return new Directory(cache).delete(recursive: true) | |
| 209 .then((_) => true) | |
| 210 » // This normally means that the directory was not there. | |
| 211 » .catchError((error) => true); | |
| 212 } | 236 } |
| 213 | 237 |
| 214 Future<String> getVersion() { | 238 Future<String> getVersion() { |
| 215 /** | 239 /** |
| 216 * Example of the file: | 240 * Example of the file: |
| 217 * <?xml version="1.0" encoding="UTF-8"?> | 241 * <?xml version="1.0" encoding="UTF-8"?> |
| 218 * <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.co
m/DTDs/PropertyList-1.0.dtd"> | 242 * <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.co
m/DTDs/PropertyList-1.0.dtd"> |
| 219 * <plist version="1.0"> | 243 * <plist version="1.0"> |
| 220 * <dict> | 244 * <dict> |
| 221 * <key>BuildVersion</key> | 245 * <key>BuildVersion</key> |
| (...skipping 27 matching lines...) Expand all Loading... |
| 249 var randomFile = file.openSync(FileMode.WRITE); | 273 var randomFile = file.openSync(FileMode.WRITE); |
| 250 var content = '<script language="JavaScript">location = "$url"</script>'; | 274 var content = '<script language="JavaScript">location = "$url"</script>'; |
| 251 randomFile.writeStringSync(content); | 275 randomFile.writeStringSync(content); |
| 252 randomFile.close(); | 276 randomFile.close(); |
| 253 } | 277 } |
| 254 | 278 |
| 255 Future<bool> start(String url) { | 279 Future<bool> start(String url) { |
| 256 _logEvent("Starting Safari browser on: $url"); | 280 _logEvent("Starting Safari browser on: $url"); |
| 257 return allowPopUps().then((success) { | 281 return allowPopUps().then((success) { |
| 258 if (!success) { | 282 if (!success) { |
| 259 return new Future.immediate(false); | 283 return false; |
| 260 } | 284 } |
| 261 return clearCache().then((_) { | 285 return clearCache().then((cleared) { |
| 286 if (!cleared) { |
| 287 _logEvent("Could not clear cache"); |
| 288 return false; |
| 289 } |
| 262 // Get the version and log that. | 290 // Get the version and log that. |
| 263 return getVersion().then((version) { | 291 return getVersion().then((version) { |
| 264 _logEvent("Got version: $version"); | 292 _logEvent("Got version: $version"); |
| 265 return new Directory('').createTemp().then((userDir) { | 293 return new Directory('').createTemp().then((userDir) { |
| 266 _cleanup = () { userDir.deleteSync(recursive: true); }; | 294 _cleanup = () { userDir.deleteSync(recursive: true); }; |
| 267 _createLaunchHTML(userDir.path, url); | 295 _createLaunchHTML(userDir.path, url); |
| 268 var args = ["${userDir.path}/launch.html"]; | 296 var args = ["${userDir.path}/launch.html"]; |
| 269 return startBrowser(binary, args); | 297 return startBrowser(binary, args); |
| 270 }); | 298 }); |
| 271 }).catchError((error) { | 299 }).catchError((error) { |
| (...skipping 753 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1025 </head> | 1053 </head> |
| 1026 <body onload="startTesting()"> | 1054 <body onload="startTesting()"> |
| 1027 Dart test driver, number of tests: <div id="number"></div> | 1055 Dart test driver, number of tests: <div id="number"></div> |
| 1028 <iframe id="embedded_iframe"></iframe> | 1056 <iframe id="embedded_iframe"></iframe> |
| 1029 </body> | 1057 </body> |
| 1030 </html> | 1058 </html> |
| 1031 """; | 1059 """; |
| 1032 return driverContent; | 1060 return driverContent; |
| 1033 } | 1061 } |
| 1034 } | 1062 } |
| OLD | NEW |