Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / download / drag_download_file.h
blob825f73668aaf3a91d0ebc33c21b4d084a0b72231
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.
5 #ifndef CONTENT_BROWSER_DOWNLOAD_DRAG_DOWNLOAD_FILE_H_
6 #define CONTENT_BROWSER_DOWNLOAD_DRAG_DOWNLOAD_FILE_H_
8 #include "base/compiler_specific.h"
9 #include "base/files/file.h"
10 #include "base/files/file_path.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/run_loop.h"
14 #include "content/browser/download/download_file.h"
15 #include "content/common/content_export.h"
16 #include "content/public/browser/download_item.h"
17 #include "content/public/browser/download_manager.h"
18 #include "content/public/common/referrer.h"
19 #include "ui/base/dragdrop/download_file_interface.h"
20 #include "url/gurl.h"
22 namespace content {
24 class DownloadManager;
25 class WebContents;
27 // This class implements downloading a file via dragging virtual files out of
28 // the browser:
29 // http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2009-August/022118.html
30 // http://www.html5rocks.com/en/tutorials/casestudies/box_dnd_download/
31 class CONTENT_EXPORT DragDownloadFile : public ui::DownloadFileProvider {
32 public:
33 // On Windows, we need to download into a temporary file. On posix, we need to
34 // download into a file that has already been created, so only the UI
35 // thread is involved. |file| must be null on windows but non-null on
36 // posix systems. |file_path| is an absolute path on all systems.
37 DragDownloadFile(const base::FilePath& file_path,
38 base::File file,
39 const GURL& url,
40 const Referrer& referrer,
41 const std::string& referrer_encoding,
42 WebContents* web_contents);
44 // DownloadFileProvider methods.
45 void Start(ui::DownloadFileObserver* observer) override;
46 bool Wait() override;
47 void Stop() override;
49 private:
50 class DragDownloadFileUI;
51 enum State {INITIALIZED, STARTED, SUCCESS, FAILURE};
53 ~DragDownloadFile() override;
55 void DownloadCompleted(bool is_successful);
56 void CheckThread();
58 base::FilePath file_path_;
59 base::File file_;
60 base::MessageLoop* drag_message_loop_;
61 State state_;
62 scoped_refptr<ui::DownloadFileObserver> observer_;
63 base::RunLoop nested_loop_;
64 DragDownloadFileUI* drag_ui_;
65 base::WeakPtrFactory<DragDownloadFile> weak_ptr_factory_;
67 DISALLOW_COPY_AND_ASSIGN(DragDownloadFile);
70 } // namespace content
72 #endif // CONTENT_BROWSER_DOWNLOAD_DRAG_DOWNLOAD_FILE_H_