Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/GlobalJsObject.cpp

Issue 29367003: Issue #4711 - Rewrite legacy thread facilities
Patch Set: Created Dec. 7, 2016, 4:44 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <stdexcept>
19 #include <thread>
18 #include <vector> 20 #include <vector>
19 #include <stdexcept>
20 21
21 #include <AdblockPlus/JsValue.h> 22 #include <AdblockPlus/JsValue.h>
22 23
23 #include "AppInfoJsObject.h" 24 #include "AppInfoJsObject.h"
24 #include "ConsoleJsObject.h" 25 #include "ConsoleJsObject.h"
25 #include "FileSystemJsObject.h" 26 #include "FileSystemJsObject.h"
26 #include "GlobalJsObject.h" 27 #include "GlobalJsObject.h"
27 #include "ConsoleJsObject.h" 28 #include "Scheduler.h"
Eric 2016/12/07 18:06:25 Duplicated.
29 #include "Utils.h"
28 #include "WebRequestJsObject.h" 30 #include "WebRequestJsObject.h"
29 #include "Thread.h"
30 #include "Utils.h"
31 31
32 using namespace AdblockPlus; 32 using namespace AdblockPlus;
33 33
34 namespace 34 namespace
35 { 35 {
36 class TimeoutThread : public Thread 36 class TimeoutTask
37 { 37 {
38 public: 38 public:
39 TimeoutThread(JsValueList& arguments) 39 TimeoutTask(JsValueList& arguments)
40 : Thread(true)
41 { 40 {
42 if (arguments.size() < 2) 41 if (arguments.size() < 2)
43 throw std::runtime_error("setTimeout requires at least 2 parameters"); 42 throw std::runtime_error("setTimeout requires at least 2 parameters");
44 43
45 if (!arguments[0]->IsFunction()) 44 if (!arguments[0]->IsFunction())
46 throw std::runtime_error( 45 throw std::runtime_error(
47 "First argument to setTimeout must be a function"); 46 "First argument to setTimeout must be a function");
48 47
49 function = arguments[0]; 48 function = arguments[0];
50 delay = arguments[1]->AsInt(); 49 delay = arguments[1]->AsInt();
51 for (size_t i = 2; i < arguments.size(); i++) 50 for (size_t i = 2; i < arguments.size(); i++)
52 functionArguments.push_back(arguments[i]); 51 functionArguments.push_back(arguments[i]);
53 } 52 }
54 53
55 void Run() 54 void operator()()
56 { 55 {
57 Sleep(delay); 56 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
58
59 function->Call(functionArguments); 57 function->Call(functionArguments);
60 } 58 }
61 59
62 private: 60 private:
63 JsValuePtr function; 61 JsValuePtr function;
64 int delay; 62 int delay;
65 JsValueList functionArguments; 63 JsValueList functionArguments;
66 }; 64 };
67 65
68 v8::Handle<v8::Value> SetTimeoutCallback(const v8::Arguments& arguments) 66 v8::Handle<v8::Value> SetTimeoutCallback(const v8::Arguments& arguments)
69 { 67 {
70 TimeoutThread* timeoutThread; 68 std::shared_ptr<TimeoutTask> timeoutTask;
71 try 69 try
72 { 70 {
73 AdblockPlus::JsValueList converted = 71 AdblockPlus::JsValueList converted =
74 AdblockPlus::JsEngine::FromArguments(arguments) 72 AdblockPlus::JsEngine::FromArguments(arguments)
75 ->ConvertArguments(arguments); 73 ->ConvertArguments(arguments);
76 timeoutThread = new TimeoutThread(converted); 74 timeoutTask = std::make_shared<TimeoutTask>(converted);
77 } 75 }
78 catch (const std::exception& e) 76 catch (const std::exception& e)
79 { 77 {
80 v8::Isolate* isolate = arguments.GetIsolate(); 78 v8::Isolate* isolate = arguments.GetIsolate();
81 return v8::ThrowException(Utils::ToV8String(isolate, e.what())); 79 return v8::ThrowException(Utils::ToV8String(isolate, e.what()));
82 } 80 }
83 timeoutThread->Start(); 81 Scheduler::StartImmediatelyInSingleUseThread(MakeHeapFunction(timeoutTask));
84 82
85 // We should actually return the timer ID here, which could be 83 // We should actually return the timer ID here, which could be
86 // used via clearTimeout(). But since we don't seem to need 84 // used via clearTimeout(). But since we don't seem to need
87 // clearTimeout(), we can save that for later. 85 // clearTimeout(), we can save that for later.
88 return v8::Undefined(); 86 return v8::Undefined();
89 } 87 }
90 88
91 v8::Handle<v8::Value> TriggerEventCallback(const v8::Arguments& arguments) 89 v8::Handle<v8::Value> TriggerEventCallback(const v8::Arguments& arguments)
92 { 90 {
93 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 91 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
(...skipping 19 matching lines...) Expand all
113 obj->SetProperty("_fileSystem", 111 obj->SetProperty("_fileSystem",
114 FileSystemJsObject::Setup(jsEngine, jsEngine->NewObject())); 112 FileSystemJsObject::Setup(jsEngine, jsEngine->NewObject()));
115 obj->SetProperty("_webRequest", 113 obj->SetProperty("_webRequest",
116 WebRequestJsObject::Setup(jsEngine, jsEngine->NewObject())); 114 WebRequestJsObject::Setup(jsEngine, jsEngine->NewObject()));
117 obj->SetProperty("console", 115 obj->SetProperty("console",
118 ConsoleJsObject::Setup(jsEngine, jsEngine->NewObject())); 116 ConsoleJsObject::Setup(jsEngine, jsEngine->NewObject()));
119 obj->SetProperty("_appInfo", 117 obj->SetProperty("_appInfo",
120 AppInfoJsObject::Setup(jsEngine, appInfo, jsEngine->NewObject())); 118 AppInfoJsObject::Setup(jsEngine, appInfo, jsEngine->NewObject()));
121 return obj; 119 return obj;
122 } 120 }
OLDNEW
« no previous file with comments | « src/FilterEngine.cpp ('k') | src/Scheduler.h » ('j') | src/Scheduler.h » ('J')

Powered by Google App Engine
This is Rietveld