ScriptObjectUtility.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //Copyright 2010 Microsoft Corporation
  2. //
  3. //Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  4. //You may obtain a copy of the License at
  5. //
  6. //http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. //Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
  9. //"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. //See the License for the specific language governing permissions and limitations under the License.
  11. namespace System.Data.Services.Http
  12. {
  13. #region Namespaces.
  14. using System;
  15. using System.Diagnostics;
  16. using System.Windows.Browser;
  17. #endregion Namespaces.
  18. internal static class ScriptObjectUtility
  19. {
  20. private const string HelperScript =
  21. @"({
  22. cd: function(f) { return function() { f(); }; },
  23. callOpen: function(requestObj, method, uri) {
  24. requestObj.open(method,uri,true);
  25. },
  26. setReadyStateChange: function(requestObj, o1) {
  27. requestObj.onreadystatechange = o1;
  28. },
  29. setReadyStateChangeToNull: function(requestObj) {
  30. try { requestObj.onreadystatechange = null; }
  31. catch (e) { requestObj.onreadystatechange = new Function(); }
  32. }
  33. })";
  34. private static readonly ScriptObject HelperScriptObject = (ScriptObject)HtmlPage.Window.Eval(HelperScript);
  35. internal static ScriptObject ToScriptFunction(Delegate d)
  36. {
  37. Debug.Assert(d != null, "d != null");
  38. return (ScriptObject)HelperScriptObject.Invoke("cd", d);
  39. }
  40. internal static void CallOpen(ScriptObject request, string method, string uri)
  41. {
  42. Debug.Assert(request != null, "request != null");
  43. HelperScriptObject.Invoke("callOpen", request, method, uri);
  44. }
  45. internal static void SetReadyStateChange(ScriptObject request, ScriptObject callback)
  46. {
  47. Debug.Assert(request != null, "request != null");
  48. if (callback == null)
  49. {
  50. HelperScriptObject.Invoke("setReadyStateChangeToNull", request);
  51. }
  52. else
  53. {
  54. HelperScriptObject.Invoke("setReadyStateChange", request, callback);
  55. }
  56. }
  57. }
  58. }