Skip to content

Can't prevent dropped file from opening in Window #908

Closed

Description

@anaisbetts

Despite having this in my startup code:

diff --git a/src/browser/main.coffee b/src/browser/main.coffee
index dcfdd9f..fc9873d 100644
--- a/src/browser/main.coffee
+++ b/src/browser/main.coffee
@@ -28,6 +28,14 @@ start = ->
   app.on 'will-finish-launching', ->
     setupCrashReporter()

+    console.log "***** Setting up URL hooks ******"
+    app.on 'open-url', (ev, path) ->
+      console.log "******* Open Url: #{path} *******"
+      ev.preventDefault()
+    app.on 'open-file', (ev, path) ->
+      console.log "******* Open File: #{path} *******"
+      ev.preventDefault()
+

Dropping a filetype that the browser recognizes (my test file is an MP4 file) will always result in the file being opened and my app page being unloaded

Activity

  1. anaisbetts commented on Dec 10, 2014

    @anaisbetts
    ContributorAuthor

    So, I'm able to stop this in the renderer via document.drop, I think this is a bug around WebView and event propagation

    In my WebView contents, I'm handling the drop event and preventing default. When I drop a file onto the WebView, the WebView gets the 'drop', but the renderer page (i.e. the WebView's host) also gets the 'drop' and proceeds to open the file.

    If I prevent default in the renderer page, that stops the file from being opened, but it also stops the WebView from receiving the event too.

    So, one thing that might make sense is, a way to pass Event objects to the WebView, so I can write something like:

    document.addEventHandler 'drop', (e) ->
      document.querySelector('webview').forwardDOMEvent(e)
      e.preventDefault()
  2. zcbenz commented on Dec 10, 2014

    @zcbenz
    Contributor

    The open-url and open-file events are emitted when you drag file to the dock icon or user opens your application from Finder, not related to drop-drag here.

    To solve your problem, I think you can prevent host page to handle dragover event when dragging over <webview>:

    document.addEventListener('drop', function(e) {
      e.preventDefault();
      e.stopPropagation();
    });
    document.addEventListener('dragover', function(e) {
      e.preventDefault();
      e.stopPropagation();
    });
    webview.addEventListener('dragover', function(e) {
      e.preventDefault();
    });
  3. anaisbetts commented on Dec 10, 2014

    @anaisbetts
    ContributorAuthor

    Getting closer - this prevents the renderer from navigating to the movie and results in the webview getting the dragover event, but not the drop event. If you want an easy test, throw https://reactivex.slack.com/messages into a WebView then drop a movie into it (or other content that a browser would open directly)

  4. zcbenz commented on Dec 10, 2014

    @zcbenz
    Contributor

    There is also a bug of Chromium that drop never firing: https://code.google.com/p/chromium/issues/detail?id=412373

    It is not exactly the same with our case but I think they are the same bug.

    I think you can work around this bug by sending a message to webview when the host page received drop event.

  5. anaisbetts commented on Dec 10, 2014

    @anaisbetts
    ContributorAuthor

    @zcbenz I can do that, but I can't recreate the DataTransfer object in the WebView because the constructor is protected. I suppose I could read the files in the renderer via node.js then transfer them via IPC to the WebView, but that's potentially a lot of data to read up into memory and send over IPC since we allow uploading of arbitrarily large files.

    The entire operation works if the file is not a MIME type that the browser can open inline (i.e. a zip file) and I allow everything (i.e. no preventDefault anywhere). Maybe we could rig Atom Shell to just never inline load files that are dropped onto the window?

    This is something we should probably be doing anyways, or else every renderer process has to hook the drop event because you could accidentally drop a JPEG on the window and break the app. For apps that (for whatever reason) do want to directly inline load the file, they can hook drop and do the navigation themselves

  6. zcbenz commented on Dec 11, 2014

    @zcbenz
    Contributor

    The entire operation works if the file is not a MIME type that the browser can open inline (i.e. a zip file) and I allow everything (i.e. no preventDefault anywhere). Maybe we could rig Atom Shell to just never inline load files that are dropped onto the window?

    I'm 👍 on this, I'll see if I can find a way to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions