diff options
author | erg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-24 18:00:20 +0000 |
---|---|---|
committer | erg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-24 18:00:20 +0000 |
commit | 6db416f0b82588c147de698cb9febf930475e5b8 (patch) | |
tree | 5aa3ecd3a2ac48845c8e5ecb3b7b027bd0ef9137 /chrome | |
parent | 063676f690cd71759e8a94d620201934ea10b3b9 (diff) | |
download | chromium_src-6db416f0b82588c147de698cb9febf930475e5b8.zip chromium_src-6db416f0b82588c147de698cb9febf930475e5b8.tar.gz chromium_src-6db416f0b82588c147de698cb9febf930475e5b8.tar.bz2 |
content: Move drag_download_* to content.
These are leaf nodes in moving tab_contents_view_gtk, and usage is sparse
enough that I'm just moving the headers and fixing up the three places that
they are included.
BUG=93804
TEST=none; code move
Review URL: http://codereview.chromium.org/7720014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@98073 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/download/drag_download_file.cc | 229 | ||||
-rw-r--r-- | chrome/browser/download/drag_download_file.h | 115 | ||||
-rw-r--r-- | chrome/browser/download/drag_download_util.cc | 113 | ||||
-rw-r--r-- | chrome/browser/download/drag_download_util.h | 62 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/tab_contents/web_drag_source.mm | 4 | ||||
-rw-r--r-- | chrome/browser/ui/gtk/tab_contents_drag_source.cc | 4 | ||||
-rw-r--r-- | chrome/browser/ui/views/tab_contents/tab_contents_drag_win.cc | 4 | ||||
-rw-r--r-- | chrome/chrome_browser.gypi | 4 |
8 files changed, 6 insertions, 529 deletions
diff --git a/chrome/browser/download/drag_download_file.cc b/chrome/browser/download/drag_download_file.cc deleted file mode 100644 index 55150d6..0000000 --- a/chrome/browser/download/drag_download_file.cc +++ /dev/null @@ -1,229 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/download/drag_download_file.h" - -#include "base/file_util.h" -#include "base/message_loop.h" -#include "chrome/browser/profiles/profile.h" -#include "content/browser/browser_thread.h" -#include "content/browser/download/download_item.h" -#include "content/browser/download/download_stats.h" -#include "content/browser/tab_contents/tab_contents.h" -#include "net/base/file_stream.h" - -DragDownloadFile::DragDownloadFile( - const FilePath& file_name_or_path, - linked_ptr<net::FileStream> file_stream, - const GURL& url, - const GURL& referrer, - const std::string& referrer_encoding, - TabContents* tab_contents) - : file_stream_(file_stream), - url_(url), - referrer_(referrer), - referrer_encoding_(referrer_encoding), - tab_contents_(tab_contents), - drag_message_loop_(MessageLoop::current()), - is_started_(false), - is_successful_(false), - download_manager_(NULL), - download_manager_observer_added_(false), - download_item_(NULL) { -#if defined(OS_WIN) - DCHECK(!file_name_or_path.empty() && !file_stream.get()); - file_name_ = file_name_or_path; -#elif defined(OS_POSIX) - DCHECK(!file_name_or_path.empty() && file_stream.get()); - file_path_ = file_name_or_path; -#endif -} - -DragDownloadFile::~DragDownloadFile() { - AssertCurrentlyOnDragThread(); - - // Since the target application can still hold and use the dragged file, - // we do not know the time that it can be safely deleted. To solve this - // problem, we schedule it to be removed after the system is restarted. -#if defined(OS_WIN) - if (!temp_dir_path_.empty()) { - if (!file_path_.empty()) - file_util::DeleteAfterReboot(file_path_); - file_util::DeleteAfterReboot(temp_dir_path_); - } -#endif - - RemoveObservers(); -} - -void DragDownloadFile::RemoveObservers() { - if (download_item_) { - download_item_->RemoveObserver(this); - download_item_ = NULL; - } - - if (download_manager_observer_added_) { - download_manager_observer_added_ = false; - download_manager_->RemoveObserver(this); - } -} - -bool DragDownloadFile::Start(ui::DownloadFileObserver* observer) { - AssertCurrentlyOnDragThread(); - - if (is_started_) - return true; - is_started_ = true; - - DCHECK(!observer_.get()); - observer_ = observer; - - if (!file_stream_.get()) { - // Create a temporary directory to save the temporary download file. We do - // not want to use the default download directory since we do not want the - // twisted file name shown in the download shelf if the file with the same - // name already exists. - if (!file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("chrome"), - &temp_dir_path_)) - return false; - - file_path_ = temp_dir_path_.Append(file_name_); - } - - InitiateDownload(); - - // On Windows, we need to wait till the download file is completed. -#if defined(OS_WIN) - StartNestedMessageLoop(); -#endif - - return is_successful_; -} - -void DragDownloadFile::Stop() { -} - -void DragDownloadFile::InitiateDownload() { -#if defined(OS_WIN) - // DownloadManager could only be invoked from the UI thread. - if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { - BrowserThread::PostTask( - BrowserThread::UI, FROM_HERE, - NewRunnableMethod(this, - &DragDownloadFile::InitiateDownload)); - return; - } -#endif - - download_manager_ = tab_contents_->browser_context()->GetDownloadManager(); - download_manager_observer_added_ = true; - download_manager_->AddObserver(this); - - DownloadSaveInfo save_info; - save_info.file_path = file_path_; - save_info.file_stream = file_stream_; - download_manager_->DownloadUrlToFile(url_, - referrer_, - referrer_encoding_, - save_info, - tab_contents_); - download_stats::RecordDownloadCount( - download_stats::INITIATED_BY_DRAG_N_DROP_COUNT); -} - -void DragDownloadFile::DownloadCompleted(bool is_successful) { -#if defined(OS_WIN) - // If not in drag-and-drop thread, defer the running to it. - if (drag_message_loop_ != MessageLoop::current()) { - drag_message_loop_->PostTask( - FROM_HERE, - NewRunnableMethod(this, - &DragDownloadFile::DownloadCompleted, - is_successful)); - return; - } -#endif - - is_successful_ = is_successful; - - // Call the observer. - DCHECK(observer_); - if (is_successful) - observer_->OnDownloadCompleted(file_path_); - else - observer_->OnDownloadAborted(); - - // Release the observer since we do not need it any more. - observer_ = NULL; - - // On Windows, we need to stop the waiting. -#if defined(OS_WIN) - QuitNestedMessageLoop(); -#endif -} - -void DragDownloadFile::ModelChanged() { - AssertCurrentlyOnUIThread(); - - if (download_item_) - return; - - std::vector<DownloadItem*> downloads; - download_manager_->GetTemporaryDownloads(file_path_.DirName(), &downloads); - for (std::vector<DownloadItem*>::const_iterator i = downloads.begin(); - i != downloads.end(); ++i) { - if ((*i)->original_url() == url_) { - download_item_ = *i; - download_item_->AddObserver(this); - break; - } - } -} - -void DragDownloadFile::OnDownloadUpdated(DownloadItem* download) { - AssertCurrentlyOnUIThread(); - if (download->IsCancelled() || download->state() == DownloadItem::REMOVING) { - RemoveObservers(); - DownloadCompleted(false); - } else if (download->IsComplete()) { - RemoveObservers(); - DownloadCompleted(true); - } - // Ignore other states. -} - -void DragDownloadFile::AssertCurrentlyOnDragThread() { - // Only do the check on Windows where two threads are involved. -#if defined(OS_WIN) - DCHECK(drag_message_loop_ == MessageLoop::current()); -#endif -} - -void DragDownloadFile::AssertCurrentlyOnUIThread() { - // Only do the check on Windows where two threads are involved. -#if defined(OS_WIN) - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); -#endif -} - -#if defined(OS_WIN) -void DragDownloadFile::StartNestedMessageLoop() { - AssertCurrentlyOnDragThread(); - - bool old_state = MessageLoop::current()->NestableTasksAllowed(); - MessageLoop::current()->SetNestableTasksAllowed(true); - is_running_nested_message_loop_ = true; - MessageLoop::current()->Run(); - MessageLoop::current()->SetNestableTasksAllowed(old_state); -} - -void DragDownloadFile::QuitNestedMessageLoop() { - AssertCurrentlyOnDragThread(); - - if (is_running_nested_message_loop_) { - is_running_nested_message_loop_ = false; - MessageLoop::current()->Quit(); - } -} -#endif diff --git a/chrome/browser/download/drag_download_file.h b/chrome/browser/download/drag_download_file.h deleted file mode 100644 index af1bd92..0000000 --- a/chrome/browser/download/drag_download_file.h +++ /dev/null @@ -1,115 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_DOWNLOAD_DRAG_DOWNLOAD_FILE_H_ -#define CHROME_BROWSER_DOWNLOAD_DRAG_DOWNLOAD_FILE_H_ -#pragma once - -#include "base/file_path.h" -#include "base/memory/linked_ptr.h" -#include "content/browser/download/download_file.h" -#include "content/browser/download/download_item.h" -#include "content/browser/download/download_manager.h" -#include "googleurl/src/gurl.h" -#include "ui/base/dragdrop/download_file_interface.h" - -class TabContents; - -namespace net { -class FileStream; -} - -class DragDownloadFile : public ui::DownloadFileProvider, - public DownloadManager::Observer, - public DownloadItem::Observer { - public: - // On Windows, we need to download into a temporary file. Two threads are - // involved: background drag-and-drop thread and UI thread. - // The first parameter file_name_or_path should contain file name while the - // second parameter file_stream should be NULL. - // - // On MacOSX, we need to download into a file stream that has already been - // created. Only UI thread is involved. - // The file path and file stream should be provided as the first two - // parameters. - DragDownloadFile(const FilePath& file_name_or_path, - linked_ptr<net::FileStream> file_stream, - const GURL& url, - const GURL& referrer, - const std::string& referrer_encoding, - TabContents* tab_contents); - - // DownloadFileProvider methods. - // Called on drag-and-drop thread (Windows). - // Called on UI thread (MacOSX). - virtual bool Start(ui::DownloadFileObserver* observer); - virtual void Stop(); -#if defined(OS_WIN) - virtual IStream* GetStream() { return NULL; } -#endif - - // DownloadManager::Observer methods. - // Called on UI thread. - virtual void ModelChanged(); - - // DownloadItem::Observer methods. - // Called on UI thread. - virtual void OnDownloadUpdated(DownloadItem* download); - virtual void OnDownloadOpened(DownloadItem* download) { } - - private: - // Called on drag-and-drop thread (Windows). - // Called on UI thread (Windows). - virtual ~DragDownloadFile(); - - // Called on drag-and-drop thread (Windows only). -#if defined(OS_WIN) - void StartNestedMessageLoop(); - void QuitNestedMessageLoop(); -#endif - - // Called on either drag-and-drop thread or UI thread (Windows). - // Called on UI thread (MacOSX). - void InitiateDownload(); - void DownloadCompleted(bool is_successful); - - // Helper methods to make sure we're in the correct thread. - void AssertCurrentlyOnDragThread(); - void AssertCurrentlyOnUIThread(); - - void RemoveObservers(); - - // Initialized on drag-and-drop thread. Accessed on either thread after that - // (Windows). - // Accessed on UI thread (MacOSX). - FilePath file_path_; - FilePath file_name_; - linked_ptr<net::FileStream> file_stream_; - GURL url_; - GURL referrer_; - std::string referrer_encoding_; - TabContents* tab_contents_; - MessageLoop* drag_message_loop_; - FilePath temp_dir_path_; - - // Accessed on drag-and-drop thread (Windows). - // Accessed on UI thread (MacOSX). - bool is_started_; - bool is_successful_; - scoped_refptr<ui::DownloadFileObserver> observer_; - - // Accessed on drag-and-drop thread (Windows only). -#if defined(OS_WIN) - bool is_running_nested_message_loop_; -#endif - - // Access on UI thread. - DownloadManager* download_manager_; - bool download_manager_observer_added_; - DownloadItem* download_item_; - - DISALLOW_COPY_AND_ASSIGN(DragDownloadFile); -}; - -#endif // CHROME_BROWSER_DOWNLOAD_DRAG_DOWNLOAD_FILE_H_ diff --git a/chrome/browser/download/drag_download_util.cc b/chrome/browser/download/drag_download_util.cc deleted file mode 100644 index b441c39..0000000 --- a/chrome/browser/download/drag_download_util.cc +++ /dev/null @@ -1,113 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/download/drag_download_util.h" - -#include "base/string_util.h" -#include "base/file_path.h" -#include "base/file_util.h" -#include "base/memory/scoped_ptr.h" -#include "base/task.h" -#include "base/string_number_conversions.h" -#include "base/utf_string_conversions.h" -#include "content/browser/browser_thread.h" -#include "googleurl/src/gurl.h" -#include "net/base/file_stream.h" -#include "net/base/net_errors.h" - -using net::FileStream; - -namespace drag_download_util { - -bool ParseDownloadMetadata(const string16& metadata, - string16* mime_type, - FilePath* file_name, - GURL* url) { - const char16 separator = L':'; - - size_t mime_type_end_pos = metadata.find(separator); - if (mime_type_end_pos == string16::npos) - return false; - - size_t file_name_end_pos = metadata.find(separator, mime_type_end_pos + 1); - if (file_name_end_pos == string16::npos) - return false; - - GURL parsed_url = GURL(metadata.substr(file_name_end_pos + 1)); - if (!parsed_url.is_valid()) - return false; - - if (mime_type) - *mime_type = metadata.substr(0, mime_type_end_pos); - if (file_name) { - string16 file_name_str = metadata.substr( - mime_type_end_pos + 1, file_name_end_pos - mime_type_end_pos - 1); -#if defined(OS_WIN) - *file_name = FilePath(file_name_str); -#else - *file_name = FilePath(UTF16ToUTF8(file_name_str)); -#endif - } - if (url) - *url = parsed_url; - - return true; -} - -FileStream* CreateFileStreamForDrop(FilePath* file_path) { - DCHECK(file_path && !file_path->empty()); - - scoped_ptr<FileStream> file_stream(new FileStream); - const int kMaxSeq = 99; - for (int seq = 0; seq <= kMaxSeq; seq++) { - FilePath new_file_path; - if (seq == 0) { - new_file_path = *file_path; - } else { - #if defined(OS_WIN) - string16 suffix = ASCIIToUTF16("-") + base::IntToString16(seq); - #else - std::string suffix = std::string("-") + base::IntToString(seq); - #endif - new_file_path = file_path->InsertBeforeExtension(suffix); - } - - // Explicitly (and redundantly check) for file -- despite the fact that our - // open won't overwrite -- just to avoid log spew. - if (!file_util::PathExists(new_file_path) && - file_stream->Open(new_file_path, base::PLATFORM_FILE_CREATE | - base::PLATFORM_FILE_WRITE) == net::OK) { - *file_path = new_file_path; - return file_stream.release(); - } - } - - return NULL; -} - -PromiseFileFinalizer::PromiseFileFinalizer( - DragDownloadFile* drag_file_downloader) - : drag_file_downloader_(drag_file_downloader) { -} - -PromiseFileFinalizer::~PromiseFileFinalizer() {} - -void PromiseFileFinalizer::Cleanup() { - if (drag_file_downloader_.get()) - drag_file_downloader_ = NULL; -} - -void PromiseFileFinalizer::OnDownloadCompleted(const FilePath& file_path) { - BrowserThread::PostTask( - BrowserThread::UI, FROM_HERE, - NewRunnableMethod(this, &PromiseFileFinalizer::Cleanup)); -} - -void PromiseFileFinalizer::OnDownloadAborted() { - BrowserThread::PostTask( - BrowserThread::UI, FROM_HERE, - NewRunnableMethod(this, &PromiseFileFinalizer::Cleanup)); -} - -} // namespace drag_download_util diff --git a/chrome/browser/download/drag_download_util.h b/chrome/browser/download/drag_download_util.h deleted file mode 100644 index 78ccbb5..0000000 --- a/chrome/browser/download/drag_download_util.h +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_DOWNLOAD_DRAG_DOWNLOAD_UTIL_H_ -#define CHROME_BROWSER_DOWNLOAD_DRAG_DOWNLOAD_UTIL_H_ -#pragma once - -#include "base/basictypes.h" -#include "base/memory/ref_counted.h" -#include "base/string16.h" -#include "chrome/browser/download/drag_download_file.h" -#include "ui/base/dragdrop/download_file_interface.h" - -class FilePath; -class GURL; -namespace net { -class FileStream; -} - -namespace drag_download_util { - -// Parse the download metadata set in DataTransfer.setData. The metadata -// consists of a set of the following values separated by ":" -// * MIME type -// * File name -// * URL -// If the file name contains special characters, they need to be escaped -// appropriately. -// For example, we can have -// text/plain:example.txt:http://example.com/example.txt -bool ParseDownloadMetadata(const string16& metadata, - string16* mime_type, - FilePath* file_name, - GURL* url); - -// Create a new file at the specified path. If the file already exists, try to -// insert the sequential unifier to produce a new file, like foo-01.txt. -// Return a FileStream if successful. -net::FileStream* CreateFileStreamForDrop(FilePath* file_path); - -// Implementation of DownloadFileObserver to finalize the download process. -class PromiseFileFinalizer : public ui::DownloadFileObserver { - public: - explicit PromiseFileFinalizer(DragDownloadFile* drag_file_downloader); - virtual ~PromiseFileFinalizer(); - - // DownloadFileObserver methods. - virtual void OnDownloadCompleted(const FilePath& file_path); - virtual void OnDownloadAborted(); - - private: - void Cleanup(); - - scoped_refptr<DragDownloadFile> drag_file_downloader_; - - DISALLOW_COPY_AND_ASSIGN(PromiseFileFinalizer); -}; - -} // namespace drag_download_util - -#endif // CHROME_BROWSER_DOWNLOAD_DRAG_DOWNLOAD_UTIL_H_ diff --git a/chrome/browser/ui/cocoa/tab_contents/web_drag_source.mm b/chrome/browser/ui/cocoa/tab_contents/web_drag_source.mm index b7300ee..b962489 100644 --- a/chrome/browser/ui/cocoa/tab_contents/web_drag_source.mm +++ b/chrome/browser/ui/cocoa/tab_contents/web_drag_source.mm @@ -16,10 +16,10 @@ #import "chrome/app/breakpad_mac.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/download/download_util.h" -#include "chrome/browser/download/drag_download_file.h" -#include "chrome/browser/download/drag_download_util.h" #include "chrome/browser/tab_contents/tab_contents_view_mac.h" #include "content/browser/download/download_manager.h" +#include "content/browser/download/drag_download_file.h" +#include "content/browser/download/drag_download_util.h" #include "content/browser/renderer_host/render_view_host.h" #include "content/browser/tab_contents/tab_contents.h" #include "content/common/url_constants.h" diff --git a/chrome/browser/ui/gtk/tab_contents_drag_source.cc b/chrome/browser/ui/gtk/tab_contents_drag_source.cc index 5cff95c..28d4ecd 100644 --- a/chrome/browser/ui/gtk/tab_contents_drag_source.cc +++ b/chrome/browser/ui/gtk/tab_contents_drag_source.cc @@ -11,9 +11,9 @@ #include "base/threading/thread_restrictions.h" #include "base/utf_string_conversions.h" #include "chrome/browser/download/download_util.h" -#include "chrome/browser/download/drag_download_file.h" -#include "chrome/browser/download/drag_download_util.h" #include "chrome/browser/ui/gtk/gtk_util.h" +#include "content/browser/download/drag_download_file.h" +#include "content/browser/download/drag_download_util.h" #include "content/browser/renderer_host/render_view_host.h" #include "content/browser/renderer_host/render_view_host_delegate.h" #include "content/browser/tab_contents/tab_contents.h" diff --git a/chrome/browser/ui/views/tab_contents/tab_contents_drag_win.cc b/chrome/browser/ui/views/tab_contents/tab_contents_drag_win.cc index c2e42c0..f011f17 100644 --- a/chrome/browser/ui/views/tab_contents/tab_contents_drag_win.cc +++ b/chrome/browser/ui/views/tab_contents/tab_contents_drag_win.cc @@ -16,14 +16,14 @@ #include "base/utf_string_conversions.h" #include "chrome/browser/bookmarks/bookmark_node_data.h" #include "chrome/browser/download/download_util.h" -#include "chrome/browser/download/drag_download_file.h" -#include "chrome/browser/download/drag_download_util.h" #include "chrome/browser/tab_contents/web_drag_source_win.h" #include "chrome/browser/tab_contents/web_drag_utils_win.h" #include "chrome/browser/tab_contents/web_drop_target_win.h" #include "chrome/browser/ui/views/tab_contents/native_tab_contents_view_win.h" #include "chrome/common/url_constants.h" #include "content/browser/browser_thread.h" +#include "content/browser/download/drag_download_file.h" +#include "content/browser/download/drag_download_util.h" #include "content/browser/tab_contents/tab_contents.h" #include "net/base/net_util.h" #include "views/drag_utils.h" diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 734c023..9d274e7 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -870,10 +870,6 @@ 'browser/download/download_throttling_resource_handler.h', 'browser/download/download_util.cc', 'browser/download/download_util.h', - 'browser/download/drag_download_file.cc', - 'browser/download/drag_download_file.h', - 'browser/download/drag_download_util.cc', - 'browser/download/drag_download_util.h', 'browser/download/save_package_file_picker.cc', 'browser/download/save_package_file_picker.h', 'browser/enumerate_modules_model_win.cc', |