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

Side by Side Diff: src/WebRequestJsObject.cpp

Issue 29370568: Issue #4692 - Move responsibility for engine reference from tasks to scheduler
Patch Set: Created Dec. 31, 2016, 10:37 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
« no previous file with comments | « src/Scheduler.h ('k') | test/BaseJsTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
(...skipping 12 matching lines...) Expand all
23 #include "JsEngineTransition.h" 23 #include "JsEngineTransition.h"
24 #include "Utils.h" 24 #include "Utils.h"
25 #include "Scheduler.h" 25 #include "Scheduler.h"
26 #include "V8Upgrade.h" 26 #include "V8Upgrade.h"
27 #include "Value.h" 27 #include "Value.h"
28 #include "WebRequestJsObject.h" 28 #include "WebRequestJsObject.h"
29 29
30 namespace 30 namespace
31 { 31 {
32 class WebRequestTask 32 class WebRequestTask
33 : public TaskFunctionInterface
33 { 34 {
34 public: 35 public:
35 WebRequestTask( 36 WebRequestTask(
36 JsEngineInternal *engine, 37 JsEngineInternal *engine,
37 std::string url, 38 std::string url,
38 AdblockPlus::HeaderList headers, 39 AdblockPlus::HeaderList headers,
39 V8PersistentNG<v8::Function> callbackFunction 40 V8PersistentNG<v8::Function> callbackFunction
40 ) 41 )
41 : jsEngine(engine->shared_from_this()), 42 : engine(engine),
42 url(url), headers(headers), callbackFunction(callbackFunction) 43 url(url), headers(headers), callbackFunction(callbackFunction)
43 {} 44 {}
44 45
45 void operator()() 46 void operator()() override
46 { 47 {
47 auto engine = ToInternal(jsEngine); // temporary statement while task keep s its own engine alive
48 /* 48 /*
49 * Synchronous HTTP GET request is arbitrary-duration and not interruptibl e 49 * Synchronous HTTP GET request is arbitrary-duration and not interruptibl e
50 */ 50 */
51 AdblockPlus::ServerResponse result = engine->GetWebRequest()->GET(url, hea ders); 51 AdblockPlus::ServerResponse result = engine->GetWebRequest()->GET(url, hea ders);
52 /* 52 /*
53 * Instantiate our scope after the long-lived operation above. 53 * Instantiate our scope after the long-lived operation above.
54 */ 54 */
55 V8ExecutionScope sentry(engine); 55 V8ExecutionScope sentry(engine);
56 auto isolate = engine->GetIsolate(); 56 auto isolate = engine->GetIsolate();
57 57
(...skipping 12 matching lines...) Expand all
70 // Call the callback 70 // Call the callback
71 auto args = AllocatedArray<v8::Local<v8::Value>>(1); 71 auto args = AllocatedArray<v8::Local<v8::Value>>(1);
72 args[0] = response; 72 args[0] = response;
73 engine->ApplyFunction(callbackFunction.Get(isolate), std::move(args)); 73 engine->ApplyFunction(callbackFunction.Get(isolate), std::move(args));
74 } 74 }
75 75
76 private: 76 private:
77 /** 77 /**
78 * Engine pointer keeps engine in existence across thread boundary 78 * Engine pointer keeps engine in existence across thread boundary
79 */ 79 */
80 AdblockPlus::JsEnginePtr jsEngine; 80 JsEngineInternal *engine;
81 std::string url; 81 std::string url;
82 AdblockPlus::HeaderList headers; 82 AdblockPlus::HeaderList headers;
83 V8PersistentNG<v8::Function> callbackFunction; 83 V8PersistentNG<v8::Function> callbackFunction;
84 }; 84 };
85 } 85 }
86 86
87 /** 87 /**
88 * Implementing function for JS "<global>._webrequest.GET()". 88 * Implementing function for JS "<global>._webrequest.GET()".
89 * 89 *
90 * Used in "compat.js" to implement "XMLHttpRequest.send()" for GET requests. 90 * Used in "compat.js" to implement "XMLHttpRequest.send()" for GET requests.
91 * 91 *
92 * \par JavaScript arguments 92 * \par JavaScript arguments
93 * 1. url. 93 * 1. url.
94 * The URL of the resource to which the request will be sent. 94 * The URL of the resource to which the request will be sent.
95 * 2. requestHeaders. 95 * 2. requestHeaders.
96 * An object whose properties represent HTTP GET request headers. 96 * An object whose properties represent HTTP GET request headers.
97 * These properties are added to the GET request. 97 * These properties are added to the GET request.
98 * 3. readyCallback. 98 * 3. readyCallback.
99 * A function to be executed when the GET response is ready. 99 * A function to be executed when the GET response is ready.
100 * The argument of this call is an object encoding the GET response. 100 * The argument of this call is an object encoding the GET response.
101 */ 101 */
102 v8::Handle<v8::Value> GETCallback(const v8::Arguments& arguments) 102 v8::Handle<v8::Value> GETCallback(const v8::Arguments& arguments)
103 { 103 {
104 auto engine = JsEngineInternal::ExtractEngine(arguments); 104 auto engine = JsEngineInternal::ExtractEngine(arguments);
105 V8ExecutionScope sentry(engine); 105 V8ExecutionScope sentry(engine);
106 106
107 /* 107 /*
108 * Factory block for WebRequest tasks. 108 * Factory block for WebRequest tasks.
109 */ 109 */
110 std::shared_ptr<WebRequestTask> task;
111 try 110 try
112 { 111 {
113 std::string url; 112 std::string url;
114 if (arguments.Length() != 3u) 113 if (arguments.Length() != 3u)
115 { 114 {
116 throw std::runtime_error("GET requires exactly 3 arguments"); 115 throw std::runtime_error("GET requires exactly 3 arguments");
117 } 116 }
118 bool b; 117 bool b;
119 std::tie(b, url) = ConvertString(arguments[0]); 118 std::tie(b, url) = ConvertString(arguments[0]);
120 if (!b) 119 if (!b)
(...skipping 24 matching lines...) Expand all
145 { 144 {
146 headers.push_back(std::make_pair(propertyName, propertyValue)); 145 headers.push_back(std::make_pair(propertyName, propertyValue));
147 } 146 }
148 } 147 }
149 148
150 auto callbackArg = arguments[2]; 149 auto callbackArg = arguments[2];
151 if (!callbackArg->IsFunction()) 150 if (!callbackArg->IsFunction())
152 { 151 {
153 throw std::runtime_error("Third argument to GET must be a function"); 152 throw std::runtime_error("Third argument to GET must be a function");
154 } 153 }
155 task = std::make_shared<WebRequestTask>(engine, url, headers, 154 WebRequestTask task(engine, url, headers,
156 V8PersistentNG<v8::Function>(engine->GetIsolate(), v8::Local<v8::Function> ::Cast(arguments[2]))); 155 V8PersistentNG<v8::Function>(engine->GetIsolate(), v8::Local<v8::Function> ::Cast(arguments[2])));
156 /*
157 * Run the task
158 */
159 engine->ScheduleTask(std::move(task), ImmediateSingleUseThread);
160 return v8::Undefined();
157 } 161 }
158 catch (const std::exception& e) 162 catch (const std::exception& e)
159 { 163 {
160 using AdblockPlus::Utils::ToV8String; 164 using AdblockPlus::Utils::ToV8String;
161 v8::Isolate* isolate = arguments.GetIsolate(); 165 v8::Isolate* isolate = arguments.GetIsolate();
162 return v8::ThrowException(ToV8String(isolate, e.what())); 166 return v8::ThrowException(ToV8String(isolate, e.what()));
163 } 167 }
164 /*
165 * Run the task
166 */
167 engine->Schedule(AdblockPlus::MakeHeapFunction(task), AdblockPlus::ImmediateSi ngleUseThread);
168 return v8::Undefined();
169 } 168 }
OLDNEW
« no previous file with comments | « src/Scheduler.h ('k') | test/BaseJsTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld