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.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"
21
22namespace content {
23
24class DownloadManager;
25class WebContents;
26
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/
31class 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);
43
44  // DownloadFileProvider methods.
45  virtual void Start(ui::DownloadFileObserver* observer) OVERRIDE;
46  virtual bool Wait() OVERRIDE;
47  virtual void Stop() OVERRIDE;
48
49 private:
50  class DragDownloadFileUI;
51  enum State {INITIALIZED, STARTED, SUCCESS, FAILURE};
52
53  virtual ~DragDownloadFile();
54
55  void DownloadCompleted(bool is_successful);
56  void CheckThread();
57
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_;
66
67  DISALLOW_COPY_AND_ASSIGN(DragDownloadFile);
68};
69
70}  // namespace content
71
72#endif  // CONTENT_BROWSER_DOWNLOAD_DRAG_DOWNLOAD_FILE_H_
73