]> khamidou — GitHub mirrors - electron.git/commitdiff
implement wrapSession
authorRobo <hop2deep@gmail.com>
Tue, 1 Sep 2015 10:02:14 +0000 (15:32 +0530)
committerRobo <hop2deep@gmail.com>
Tue, 1 Sep 2015 11:20:42 +0000 (16:50 +0530)
atom/browser/api/atom_api_session.cc
atom/browser/api/lib/app.coffee
atom/common/node_bindings.cc
docs/api/app.md
docs/api/session.md

index 0b7640e416b58be3331d781c1b18ce667f2b7083..3ca15ab871fa2d6abcf17bd79adbc1fdcb124a72 100644 (file)
@@ -106,12 +106,12 @@ template<>
 struct Converter<content::DownloadItem*> {
   static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
                                    content::DownloadItem* val) {
-    return mate::ObjectTemplateBuilder(isolate)
-        .SetValue("url", val->GetURL())
-        .SetValue("filename", val->GetSuggestedFilename())
-        .SetValue("mimeType", val->GetMimeType())
-        .SetValue("hasUserGesture", val->HasUserGesture())
-        .Build()->NewInstance();
+    mate::Dictionary dict(isolate, v8::Object::New(isolate));
+    dict.Set("url", val->GetURL());
+    dict.Set("filename", val->GetSuggestedFilename());
+    dict.Set("mimeType", val->GetMimeType());
+    dict.Set("hasUserGesture", val->HasUserGesture());
+    return dict.GetHandle();
   }
 };
 
@@ -123,6 +123,10 @@ namespace api {
 
 namespace {
 
+// The wrapSession funtion which is implemented in JavaScript
+using WrapSessionCallback = base::Callback<void(v8::Local<v8::Value>)>;
+WrapSessionCallback g_wrap_session;
+
 class ResolveProxyHelper {
  public:
   ResolveProxyHelper(AtomBrowserContext* browser_context,
@@ -321,9 +325,33 @@ mate::Handle<Session> Session::CreateFrom(
   if (existing)
     return mate::CreateHandle(isolate, static_cast<Session*>(existing));
 
-  return mate::CreateHandle(isolate, new Session(browser_context));
+  auto handle = mate::CreateHandle(isolate, new Session(browser_context));
+  g_wrap_session.Run(handle.ToV8());
+  return handle;
+}
+
+void SetWrapSession(const WrapSessionCallback& callback) {
+  g_wrap_session = callback;
+}
+
+void ClearWrapSession() {
+  g_wrap_session.Reset();
 }
 
 }  // namespace api
 
 }  // namespace atom
+
+namespace {
+
+void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
+                v8::Local<v8::Context> context, void* priv) {
+  v8::Isolate* isolate = context->GetIsolate();
+  mate::Dictionary dict(isolate, exports);
+  dict.SetMethod("_setWrapSession", &atom::api::SetWrapSession);
+  dict.SetMethod("_clearWrapSession", &atom::api::ClearWrapSession);
+}
+
+}  // namespace
+
+NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_session, Initialize)
index f71297c596f0e2836a4122440f4a1c067054ad76..b3446e86e23aaf7f068e5ebf49b7e783b1cb7b0f 100644 (file)
@@ -1,10 +1,15 @@
 EventEmitter = require('events').EventEmitter
 
 bindings = process.atomBinding 'app'
+sessionBindings = process.atomBinding 'session'
 
 app = bindings.app
 app.__proto__ = EventEmitter.prototype
 
+wrapSession = (session) ->
+  # session is an Event Emitter.
+  session.__proto__ = EventEmitter.prototype
+
 app.setApplicationMenu = (menu) ->
   require('menu').setApplicationMenu menu
 
@@ -32,10 +37,8 @@ app.setAppPath = (path) ->
 app.getAppPath = ->
   appPath
 
-app.once 'ready', ->
-  app.defaultSession.__proto__ = EventEmitter.prototype
-  # Be compatible with old API.
-  @emit 'finish-launching'
+# Be compatible with old API.
+app.once 'ready', -> @emit 'finish-launching'
 app.terminate = app.quit
 app.exit = process.exit
 app.getHomeDir = -> @getPath 'home'
@@ -43,5 +46,9 @@ app.getDataPath = -> @getPath 'userData'
 app.setDataPath = (path) -> @setPath 'userData', path
 app.resolveProxy = -> @defaultSession.resolveProxy.apply @defaultSession, arguments
 
+# Session wrapper.
+sessionBindings._setWrapSession wrapSession
+process.once 'exit', sessionBindings._clearWrapSession
+
 # Only one App object pemitted.
 module.exports = app
index 5aed5619f9cf035511af31bbf122c7195057c1bb..09666a470b6c12e335ca11196d29ab2c19e29483 100644 (file)
@@ -40,6 +40,7 @@ REFERENCE_MODULE(atom_browser_power_monitor);
 REFERENCE_MODULE(atom_browser_power_save_blocker);
 REFERENCE_MODULE(atom_browser_protocol);
 REFERENCE_MODULE(atom_browser_global_shortcut);
+REFERENCE_MODULE(atom_browser_session);
 REFERENCE_MODULE(atom_browser_tray);
 REFERENCE_MODULE(atom_browser_web_contents);
 REFERENCE_MODULE(atom_browser_web_view_manager);
index 8951adf45cfd8e2dc05b9a6742783c14e810e841..db34d37ec59e60d0a12f02e8c3030d0ba6148522 100644 (file)
@@ -330,35 +330,3 @@ Sets the application's [dock menu][dock-menu].
 
 [dock-menu]:https://developer.apple.com/library/mac/documentation/Carbon/Conceptual/customizing_docktile/concepts/dockconcepts.html#//apple_ref/doc/uid/TP30000986-CH2-TPXREF103
 [tasks]:http://msdn.microsoft.com/en-us/library/windows/desktop/dd378460(v=vs.85).aspx#tasks
-
-## `app.defaultSession`
-
-The default session of the app available once the (ready)[app.md#event-ready] event is fired.
-
-```javascript
-app.on('ready', function() {
-  var defaultSession = app.defaultSession;
-})
-```
-
-### Event: 'will-download'
-
-* `event` Event
-* `downloadItem` Object
-  * `url` String
-  * `filename` String
-  * `mimeType` String
-  * `hasUserGesture` Boolean
-* `webContents` (WebContents)[web-contents.md]
-
-Fired when a download is about to start. Calling `preventDefault()`
-will cancel the download.
-
-```javascript
-app.defaultSession.on('will-download', function(e, downloadItem, webContents) {
-  e.preventDefault();
-  require('request')(downloadItem.url, function(data) {
-    require('fs').writeFileSync('/somewhere', data);
-  });
-});
-```
index 7b0c1b44c7dc55c05dfe84c411e9d6784a4ddb01..28a0c92ca655ed69b0e96d1529b6a084bedea624 100644 (file)
@@ -13,6 +13,30 @@ win.loadUrl("http://github.com");
 var session = win.webContents.session
 ```
 
+## Events
+
+### Event: 'will-download'
+
+* `event` Event
+* `downloadItem` Object
+  * `url` String
+  * `filename` String
+  * `mimeType` String
+  * `hasUserGesture` Boolean
+* `webContents` (WebContents)[web-contents.md]
+
+Fired when a download is about to start. Calling `preventDefault()`
+will cancel the download.
+
+```javascript
+session.on('will-download', function(e, downloadItem, webContents) {
+  e.preventDefault();
+  require('request')(downloadItem.url, function(data) {
+    require('fs').writeFileSync('/somewhere', data);
+  });
+});
+```
+
 ## Methods
 
 The `session` object has the following methods: