1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/renderer/playback_extension.h"
6
7#include "v8/include/v8.h"
8
9const char kPlaybackExtensionName[] = "v8/PlaybackMode";
10
11namespace extensions_v8 {
12
13v8::Extension* PlaybackExtension::Get() {
14  v8::Extension* extension = new v8::Extension(
15      kPlaybackExtensionName,
16      "(function () {"
17      "  var orig_date = Date;"
18      "  var x = 0;"
19      "  var time_seed = 1204251968254;"
20      "  Math.random = function() {"
21      "    x += .1;"
22      "    return (x % 1);"
23      "  };"
24      "  Date = function() {"
25      "    if (this instanceof Date) {"
26      "      switch (arguments.length) {"
27      "        case 0: return new orig_date(time_seed += 50);"
28      "        case 1: return new orig_date(arguments[0]);"
29      "        default: return new orig_date(arguments[0], arguments[1],"
30      "            arguments.length >= 3 ? arguments[2] : 1,"
31      "            arguments.length >= 4 ? arguments[3] : 0,"
32      "            arguments.length >= 5 ? arguments[4] : 0,"
33      "            arguments.length >= 6 ? arguments[5] : 0,"
34      "            arguments.length >= 7 ? arguments[6] : 0);"
35      "      }"
36      "    }"
37      "    return new Date().toString();"
38      "  };"
39      "  Date.__proto__ = orig_date;"
40      "  Date.prototype.constructor = Date;"
41      "  orig_date.now = function() {"
42      "    return new Date().getTime();"
43      "  };"
44      "})()");
45    return extension;
46}
47
48}  // namespace extensions_v8
49