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

Side by Side Diff: include/AdblockPlus/JsEngine.h

Issue 10310030: Convert references to FileSystem & Co. into shared pointers (avoid use after free) (Closed)
Patch Set: Created April 18, 2013, 11:59 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 #ifndef ADBLOCKPLUS_JS_ENGINE_H 1 #ifndef ADBLOCKPLUS_JS_ENGINE_H
2 #define ADBLOCKPLUS_JS_ENGINE_H 2 #define ADBLOCKPLUS_JS_ENGINE_H
3 3
4 #include <stdexcept> 4 #include <stdexcept>
5 #include <string> 5 #include <string>
6 #include <v8.h> 6 #include <v8.h>
7 #include <AdblockPlus/AppInfo.h>
8 #include <AdblockPlus/ErrorCallback.h>
9 #include <AdblockPlus/FileSystem.h>
7 #include <AdblockPlus/JsValue.h> 10 #include <AdblockPlus/JsValue.h>
11 #include <AdblockPlus/WebRequest.h>
12
13 #include "tr1_memory.h"
8 14
9 namespace AdblockPlus 15 namespace AdblockPlus
10 { 16 {
11 struct AppInfo;
12 class FileSystem;
13 class WebRequest;
14 class ErrorCallback;
15
16 class JsError : public std::runtime_error 17 class JsError : public std::runtime_error
17 { 18 {
18 public: 19 public:
19 explicit JsError(const v8::Handle<v8::Value> exception, 20 explicit JsError(const v8::Handle<v8::Value> exception,
20 const v8::Handle<v8::Message> message); 21 const v8::Handle<v8::Message> message);
21 }; 22 };
22 23
24 typedef std::tr1::shared_ptr<JsEngine> JsEnginePtr;
25
23 class JsEngine 26 class JsEngine
24 { 27 {
25 friend class JsValue; 28 friend class JsValue;
26 29
27 public: 30 public:
28 JsEngine(const AppInfo& appInfo, 31 JsEngine(const AppInfo& appInfo = AppInfo());
29 FileSystem* const fileReader,
30 WebRequest* const webRequest,
31 ErrorCallback* const errorCallback);
32 JsValuePtr Evaluate(const std::string& source, 32 JsValuePtr Evaluate(const std::string& source,
33 const std::string& filename = ""); 33 const std::string& filename = "");
34 void Load(const std::string& scriptPath); 34 void Load(const std::string& scriptPath);
35 void Gc(); 35 void Gc();
36 JsValuePtr NewValue(const std::string& val); 36 JsValuePtr NewValue(const std::string& val);
37 JsValuePtr NewValue(int64_t val); 37 JsValuePtr NewValue(int64_t val);
38 JsValuePtr NewValue(bool val); 38 JsValuePtr NewValue(bool val);
39 inline JsValuePtr NewValue(const char* val) 39 inline JsValuePtr NewValue(const char* val)
40 { 40 {
41 return NewValue(std::string(val)); 41 return NewValue(std::string(val));
42 } 42 }
43 inline JsValuePtr NewValue(int val) 43 inline JsValuePtr NewValue(int val)
44 { 44 {
45 return NewValue(static_cast<int64_t>(val)); 45 return NewValue(static_cast<int64_t>(val));
46 } 46 }
47 JsValuePtr NewObject(); 47 JsValuePtr NewObject();
48 JsValuePtr NewCallback(v8::InvocationCallback callback); 48 JsValuePtr NewCallback(v8::InvocationCallback callback);
49 static JsEngine& FromArguments(const v8::Arguments& arguments); 49 static JsEngine& FromArguments(const v8::Arguments& arguments);
50 JsValueList ConvertArguments(const v8::Arguments& arguments); 50 JsValueList ConvertArguments(const v8::Arguments& arguments);
51 51
52 inline FileSystem& GetFileSystem() 52 FileSystemPtr GetFileSystem();
53 { 53 void SetFileSystem(FileSystemPtr val);
54 return fileSystem; 54 WebRequestPtr GetWebRequest();
55 } 55 void SetWebRequest(WebRequestPtr val);
56 inline WebRequest& GetWebRequest() 56 ErrorCallbackPtr GetErrorCallback();
57 { 57 void SetErrorCallback(ErrorCallbackPtr val);
58 return webRequest;
59 }
60 inline ErrorCallback& GetErrorCallback()
61 {
62 return errorCallback;
63 }
64 58
65 class Context 59 class Context
66 { 60 {
67 public: 61 public:
68 Context(const JsEngine& jsEngine); 62 Context(const JsEngine& jsEngine);
69 virtual inline ~Context() {}; 63 virtual inline ~Context() {};
70 64
71 private: 65 private:
72 const v8::Locker locker; 66 const v8::Locker locker;
73 const v8::HandleScope handleScope; 67 const v8::HandleScope handleScope;
74 const v8::Context::Scope contextScope; 68 const v8::Context::Scope contextScope;
75 }; 69 };
76 70
77 private: 71 private:
78 FileSystem& fileSystem; 72 FileSystemPtr fileSystem;
79 WebRequest& webRequest; 73 WebRequestPtr webRequest;
80 ErrorCallback& errorCallback; 74 ErrorCallbackPtr errorCallback;
81 v8::Isolate* isolate; 75 v8::Isolate* isolate;
82 v8::Persistent<v8::Context> context; 76 v8::Persistent<v8::Context> context;
83 }; 77 };
84 } 78 }
85 79
86 #endif 80 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld