1 /* 2 * Copyright (C) 2009 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "config.h" 32 #include "DateExtension.h" 33 34 #include "V8Proxy.h" 35 #include "V8HiddenPropertyName.h" 36 37 namespace WebCore { 38 39 DateExtension* DateExtension::extension; 40 41 static const char* dateExtensionName = "v8/DateExtension"; 42 static const char* dateExtensionScript = 43 "(function () {" 44 " var counter;" 45 " var orig_getTime;" 46 " function getTimeOverride() {" 47 " if (++counter > 1000)" 48 " OnSleepDetected();" 49 " return orig_getTime.call(this);" 50 " };" 51 " function enableSleepDetection(enable) {" 52 " if (enable) {" 53 " counter = 0;" 54 " orig_getTime = Date.prototype.getTime;" 55 " Date.prototype.getTime = getTimeOverride;" 56 " } else {" 57 " Date.prototype.getTime = orig_getTime;" 58 " }" 59 " };" 60 " native function Setup();" 61 " native function OnSleepDetected();" 62 " Setup(Date, enableSleepDetection);" 63 "})()"; 64 65 DateExtension::DateExtension() : v8::Extension(dateExtensionName, dateExtensionScript) 66 { 67 } 68 69 DateExtension* DateExtension::get() 70 { 71 if (!extension) 72 extension = new DateExtension(); 73 return extension; 74 } 75 76 void DateExtension::setAllowSleep(bool allow) 77 { 78 v8::Local<v8::Value> result = V8Proxy::currentContext()->Global()->Get(v8::String::New("Date")); 79 if (result.IsEmpty() || !result->IsObject()) 80 return; 81 82 v8::Handle<v8::Object> dateObject = v8::Handle<v8::Object>::Cast(result); 83 if (dateObject.IsEmpty()) 84 return; 85 86 v8::Local<v8::Value> sleepFunctionHandle = dateObject->GetHiddenValue(V8HiddenPropertyName::sleepFunction()); 87 if (sleepFunctionHandle.IsEmpty() || !sleepFunctionHandle->IsFunction()) 88 return; 89 90 v8::Handle<v8::Value> argv[1]; 91 argv[0] = v8::Boolean::New(!allow); 92 v8::Handle<v8::Function>::Cast(sleepFunctionHandle)->Call(v8::Object::New(), 1, argv); 93 } 94 95 v8::Handle<v8::FunctionTemplate> DateExtension::GetNativeFunction(v8::Handle<v8::String> name) 96 { 97 if (name->Equals(v8::String::New("Setup"))) 98 return v8::FunctionTemplate::New(Setup); 99 if (name->Equals(v8::String::New("OnSleepDetected"))) 100 return v8::FunctionTemplate::New(OnSleepDetected); 101 102 return v8::Handle<v8::FunctionTemplate>(); 103 } 104 105 v8::Handle<v8::Value> DateExtension::Setup(const v8::Arguments& args) 106 { 107 if (args.Length() != 2 || !args[0]->IsObject() || !args[1]->IsFunction()) 108 return v8::Undefined(); 109 110 v8::Handle<v8::Object> dateObject = v8::Handle<v8::Object>::Cast(args[0]); 111 v8::Handle<v8::Function> enableSleepDetectionFunction = v8::Handle<v8::Function>::Cast(args[1]); 112 113 dateObject->SetHiddenValue(V8HiddenPropertyName::sleepFunction(), enableSleepDetectionFunction); 114 return v8::Undefined(); 115 } 116 117 v8::Handle<v8::Value> DateExtension::OnSleepDetected(const v8::Arguments&) 118 { 119 V8Proxy::throwError(V8Proxy::GeneralError, "Too much time spent in unload handler."); 120 return v8::Undefined(); 121 } 122 123 } // namespace WebCore 124