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

Side by Side Diff: src/WebRequestJsObject.cpp

Issue 29361562: Issue 3594 - remove circular references JsEngine-JsValue-JsEngine (Closed)
Patch Set: Created Nov. 3, 2016, 11:26 a.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
(...skipping 12 matching lines...) Expand all
23 #include "Thread.h" 23 #include "Thread.h"
24 #include "Utils.h" 24 #include "Utils.h"
25 #include "WebRequestJsObject.h" 25 #include "WebRequestJsObject.h"
26 26
27 namespace 27 namespace
28 { 28 {
29 class WebRequestThread : public AdblockPlus::Thread 29 class WebRequestThread : public AdblockPlus::Thread
30 { 30 {
31 public: 31 public:
32 WebRequestThread(AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValueList & arguments) 32 WebRequestThread(AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValueList & arguments)
33 : Thread(true), jsEngine(jsEngine), url(arguments[0]->AsString()) 33 : Thread(true), m_jsEngine(jsEngine), url(arguments[0]->AsString())
34 { 34 {
35 if (!url.length()) 35 if (!url.length())
36 throw std::runtime_error("Invalid string passed as first argument to GET "); 36 throw std::runtime_error("Invalid string passed as first argument to GET ");
37 37
38 { 38 {
39 AdblockPlus::JsValuePtr headersObj = arguments[1]; 39 AdblockPlus::JsValuePtr headersObj = arguments[1];
40 if (!headersObj->IsObject()) 40 if (!headersObj->IsObject())
41 throw std::runtime_error("Second argument to GET must be an object"); 41 throw std::runtime_error("Second argument to GET must be an object");
42 42
43 std::vector<std::string> properties = headersObj->GetOwnPropertyNames(); 43 std::vector<std::string> properties = headersObj->GetOwnPropertyNames();
44 for (std::vector<std::string>::iterator it = properties.begin(); 44 for (std::vector<std::string>::iterator it = properties.begin();
45 it != properties.end(); ++it) 45 it != properties.end(); ++it)
46 { 46 {
47 std::string header = *it; 47 std::string header = *it;
48 std::string headerValue = headersObj->GetProperty(header)->AsString(); 48 std::string headerValue = headersObj->GetProperty(header)->AsString();
49 if (header.length() && headerValue.length()) 49 if (header.length() && headerValue.length())
50 headers.push_back(std::pair<std::string, std::string>(header, header Value)); 50 headers.push_back(std::pair<std::string, std::string>(header, header Value));
51 } 51 }
52 } 52 }
53 53
54 callback = arguments[2]; 54 callback = arguments[2];
55 if (!callback->IsFunction()) 55 if (!callback->IsFunction())
56 throw std::runtime_error("Third argument to GET must be a function"); 56 throw std::runtime_error("Third argument to GET must be a function");
57 } 57 }
58 58
59 void Run() 59 void Run()
60 { 60 {
61 AdblockPlus::ServerResponse result = jsEngine->GetWebRequest()->GET(url, h eaders); 61 // Don't keep a strong reference to JsEngine while request is being execut ed.
62 62 auto webRequest = AdblockPlus::Utils::lockJsEngine(m_jsEngine)->GetWebRequ est();
Oleksandr 2016/11/25 10:38:04 Is there any reason why the line above is not usin
sergei 2016/11/25 12:04:46 Yes, it's in the comment. Let's say we have "locke
63 AdblockPlus::JsContext context(jsEngine); 63 auto result = webRequest->GET(url, headers);
64 64 AdblockPlus::JsContext context(m_jsEngine);
65 AdblockPlus::JsValuePtr resultObject = jsEngine->NewObject(); 65 AdblockPlus::JsValuePtr resultObject = context.jsEngine().NewObject();
66 resultObject->SetProperty("status", result.status); 66 resultObject->SetProperty("status", result.status);
67 resultObject->SetProperty("responseStatus", result.responseStatus); 67 resultObject->SetProperty("responseStatus", result.responseStatus);
68 resultObject->SetProperty("responseText", result.responseText); 68 resultObject->SetProperty("responseText", result.responseText);
69 69
70 AdblockPlus::JsValuePtr headersObject = jsEngine->NewObject(); 70 AdblockPlus::JsValuePtr headersObject = context.jsEngine().NewObject();
71 for (AdblockPlus::HeaderList::iterator it = result.responseHeaders.begin() ; 71 for (AdblockPlus::HeaderList::iterator it = result.responseHeaders.begin() ;
72 it != result.responseHeaders.end(); ++it) 72 it != result.responseHeaders.end(); ++it)
73 { 73 {
74 headersObject->SetProperty(it->first, it->second); 74 headersObject->SetProperty(it->first, it->second);
75 } 75 }
76 resultObject->SetProperty("responseHeaders", headersObject); 76 resultObject->SetProperty("responseHeaders", headersObject);
77 77
78 AdblockPlus::JsValueList params; 78 AdblockPlus::JsValueList params;
79 params.push_back(resultObject); 79 params.push_back(resultObject);
80 callback->Call(params); 80 callback->Call(params);
81 } 81 }
82 82
83 private: 83 private:
84 AdblockPlus::JsEnginePtr jsEngine; 84 std::weak_ptr<AdblockPlus::JsEngine> m_jsEngine;
85 std::string url; 85 std::string url;
86 AdblockPlus::HeaderList headers; 86 AdblockPlus::HeaderList headers;
87 AdblockPlus::JsValuePtr callback; 87 AdblockPlus::JsValuePtr callback;
88 }; 88 };
89 89
90 v8::Handle<v8::Value> GETCallback(const v8::Arguments& arguments) 90 v8::Handle<v8::Value> GETCallback(const v8::Arguments& arguments)
91 { 91 {
92 WebRequestThread* thread; 92 WebRequestThread* thread;
93 try 93 try
94 { 94 {
(...skipping 13 matching lines...) Expand all
108 return v8::Undefined(); 108 return v8::Undefined();
109 } 109 }
110 } 110 }
111 111
112 AdblockPlus::JsValuePtr AdblockPlus::WebRequestJsObject::Setup( 112 AdblockPlus::JsValuePtr AdblockPlus::WebRequestJsObject::Setup(
113 AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValuePtr obj) 113 AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValuePtr obj)
114 { 114 {
115 obj->SetProperty("GET", jsEngine->NewCallback(::GETCallback)); 115 obj->SetProperty("GET", jsEngine->NewCallback(::GETCallback));
116 return obj; 116 return obj;
117 } 117 }
OLDNEW

Powered by Google App Engine
This is Rietveld