Home | History | Annotate | Download | only in v8
      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 "bindings/v8/DateExtension.h"
     33 
     34 #include "bindings/v8/V8Binding.h"
     35 #include "bindings/v8/V8HiddenPropertyName.h"
     36 #include "bindings/v8/V8ScriptRunner.h"
     37 
     38 namespace WebCore {
     39 
     40 DateExtension* DateExtension::extension;
     41 
     42 static const char* dateExtensionName = "v8/DateExtension";
     43 static const char* dateExtensionScript =
     44     "(function () {"
     45     "  var counter;"
     46     "  var orig_getTime;"
     47     "  function getTimeOverride() {"
     48     "    if (++counter > 1000)"
     49     "      OnSleepDetected();"
     50     "    return orig_getTime.call(this);"
     51     "  };"
     52     "  function enableSleepDetection(enable) {"
     53     "    if (enable) {"
     54     "      counter = 0;"
     55     "      orig_getTime = Date.prototype.getTime;"
     56     "      Date.prototype.getTime = getTimeOverride;"
     57     "    } else {"
     58     "      Date.prototype.getTime = orig_getTime;"
     59     "    }"
     60     "  };"
     61     "  native function Setup();"
     62     "  native function OnSleepDetected();"
     63     "  Setup(Date, enableSleepDetection);"
     64     "})()";
     65 
     66 DateExtension::DateExtension() : v8::Extension(dateExtensionName, dateExtensionScript)
     67 {
     68 }
     69 
     70 DateExtension* DateExtension::get()
     71 {
     72     if (!extension)
     73         extension = new DateExtension();
     74     return extension;
     75 }
     76 
     77 void DateExtension::setAllowSleep(bool allow, v8::Isolate* isolate)
     78 {
     79     v8::Local<v8::Value> result = v8::Context::GetCurrent()->Global()->Get(v8::String::NewSymbol("Date"));
     80     if (result.IsEmpty() || !result->IsObject())
     81         return;
     82 
     83     v8::Handle<v8::Object> dateObject = v8::Handle<v8::Object>::Cast(result);
     84     if (dateObject.IsEmpty())
     85         return;
     86 
     87     v8::Local<v8::Value> sleepFunctionHandle = dateObject->GetHiddenValue(V8HiddenPropertyName::sleepFunction());
     88     if (sleepFunctionHandle.IsEmpty() || !sleepFunctionHandle->IsFunction())
     89         return;
     90 
     91     v8::Handle<v8::Value> argv[1];
     92     argv[0] = v8::Boolean::New(!allow);
     93     V8ScriptRunner::callInternalFunction(v8::Handle<v8::Function>::Cast(sleepFunctionHandle), v8::Object::New(), WTF_ARRAY_LENGTH(argv), argv, isolate);
     94 }
     95 
     96 v8::Handle<v8::FunctionTemplate> DateExtension::GetNativeFunction(v8::Handle<v8::String> name)
     97 {
     98     if (name->Equals(v8::String::NewSymbol("Setup")))
     99         return v8::FunctionTemplate::New(Setup);
    100     if (name->Equals(v8::String::NewSymbol("OnSleepDetected")))
    101         return v8::FunctionTemplate::New(OnSleepDetected);
    102 
    103     return v8::Handle<v8::FunctionTemplate>();
    104 }
    105 
    106 void DateExtension::Setup(const v8::FunctionCallbackInfo<v8::Value>& args)
    107 {
    108     if (args.Length() != 2 || !args[0]->IsObject() || !args[1]->IsFunction())
    109         return;
    110 
    111     v8::Handle<v8::Object> dateObject = v8::Handle<v8::Object>::Cast(args[0]);
    112     v8::Handle<v8::Function> enableSleepDetectionFunction = v8::Handle<v8::Function>::Cast(args[1]);
    113 
    114     dateObject->SetHiddenValue(V8HiddenPropertyName::sleepFunction(), enableSleepDetectionFunction);
    115     return;
    116 }
    117 
    118 void DateExtension::OnSleepDetected(const v8::FunctionCallbackInfo<v8::Value>& args)
    119 {
    120     throwError(v8GeneralError, "Too much time spent in unload handler.", args.GetIsolate());
    121 }
    122 
    123 }  // namespace WebCore
    124