Home | History | Annotate | Download | only in download
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CONTENT_BROWSER_DOWNLOAD_DRAG_DOWNLOAD_FILE_H_
      6 #define CONTENT_BROWSER_DOWNLOAD_DRAG_DOWNLOAD_FILE_H_
      7 
      8 #include "base/compiler_specific.h"
      9 #include "base/files/file_path.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/memory/weak_ptr.h"
     12 #include "base/run_loop.h"
     13 #include "content/browser/download/download_file.h"
     14 #include "content/common/content_export.h"
     15 #include "content/public/browser/download_item.h"
     16 #include "content/public/browser/download_manager.h"
     17 #include "content/public/common/referrer.h"
     18 #include "net/base/file_stream.h"
     19 #include "ui/base/dragdrop/download_file_interface.h"
     20 #include "url/gurl.h"
     21 
     22 namespace net {
     23 class FileStream;
     24 }
     25 
     26 namespace content {
     27 
     28 class DownloadManager;
     29 class WebContents;
     30 
     31 // This class implements downloading a file via dragging virtual files out of
     32 // the browser:
     33 // http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2009-August/022118.html
     34 // http://www.html5rocks.com/en/tutorials/casestudies/box_dnd_download/
     35 class CONTENT_EXPORT DragDownloadFile : public ui::DownloadFileProvider {
     36  public:
     37   // On Windows, we need to download into a temporary file. On posix, we need to
     38   // download into a file stream that has already been created, so only the UI
     39   // thread is involved. |file_stream| must be null on windows but non-null on
     40   // posix systems. |file_path| is an absolute path on all systems.
     41   DragDownloadFile(const base::FilePath& file_path,
     42                    scoped_ptr<net::FileStream> file_stream,
     43                    const GURL& url,
     44                    const Referrer& referrer,
     45                    const std::string& referrer_encoding,
     46                    WebContents* web_contents);
     47 
     48   // DownloadFileProvider methods.
     49   virtual void Start(ui::DownloadFileObserver* observer) OVERRIDE;
     50   virtual bool Wait() OVERRIDE;
     51   virtual void Stop() OVERRIDE;
     52 
     53  private:
     54   class DragDownloadFileUI;
     55   enum State {INITIALIZED, STARTED, SUCCESS, FAILURE};
     56 
     57   virtual ~DragDownloadFile();
     58 
     59   void DownloadCompleted(bool is_successful);
     60   void CheckThread();
     61 
     62   base::FilePath file_path_;
     63   scoped_ptr<net::FileStream> file_stream_;
     64   base::MessageLoop* drag_message_loop_;
     65   State state_;
     66   scoped_refptr<ui::DownloadFileObserver> observer_;
     67   base::RunLoop nested_loop_;
     68   DragDownloadFileUI* drag_ui_;
     69   base::WeakPtrFactory<DragDownloadFile> weak_ptr_factory_;
     70 
     71   DISALLOW_COPY_AND_ASSIGN(DragDownloadFile);
     72 };
     73 
     74 }  // namespace content
     75 
     76 #endif  // CONTENT_BROWSER_DOWNLOAD_DRAG_DOWNLOAD_FILE_H_
     77