Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: content/browser/download/download_manager_impl.cc

Issue 10833058: Make DownloadItem derive SupportsUserData instead of re-implementing it. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: gdata: "external"->"user" Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/download/download_manager_impl.h" 5 #include "content/browser/download/download_manager_impl.h"
6 6
7 #include <iterator> 7 #include <iterator>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/debug/alias.h" 11 #include "base/debug/alias.h"
12 #include "base/file_util.h" 12 #include "base/file_util.h"
13 #include "base/i18n/case_conversion.h" 13 #include "base/i18n/case_conversion.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/message_loop.h" 15 #include "base/message_loop.h"
16 #include "base/stl_util.h" 16 #include "base/stl_util.h"
17 #include "base/stringprintf.h" 17 #include "base/stringprintf.h"
18 #include "base/supports_user_data.h"
18 #include "base/synchronization/lock.h" 19 #include "base/synchronization/lock.h"
19 #include "base/sys_string_conversions.h" 20 #include "base/sys_string_conversions.h"
20 #include "build/build_config.h" 21 #include "build/build_config.h"
21 #include "content/browser/download/byte_stream.h" 22 #include "content/browser/download/byte_stream.h"
22 #include "content/browser/download/download_create_info.h" 23 #include "content/browser/download/download_create_info.h"
23 #include "content/browser/download/download_file_manager.h" 24 #include "content/browser/download/download_file_manager.h"
24 #include "content/browser/download/download_item_impl.h" 25 #include "content/browser/download/download_item_impl.h"
25 #include "content/browser/download/download_stats.h" 26 #include "content/browser/download/download_stats.h"
26 #include "content/browser/renderer_host/render_view_host_impl.h" 27 #include "content/browser/renderer_host/render_view_host_impl.h"
27 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" 28 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h"
(...skipping 17 matching lines...) Expand all
45 using content::BrowserThread; 46 using content::BrowserThread;
46 using content::DownloadId; 47 using content::DownloadId;
47 using content::DownloadItem; 48 using content::DownloadItem;
48 using content::DownloadPersistentStoreInfo; 49 using content::DownloadPersistentStoreInfo;
49 using content::ResourceDispatcherHostImpl; 50 using content::ResourceDispatcherHostImpl;
50 using content::WebContents; 51 using content::WebContents;
51 52
52 namespace { 53 namespace {
53 54
54 // This is just used to remember which DownloadItems come from SavePage. 55 // This is just used to remember which DownloadItems come from SavePage.
55 class SavePageExternalData : public DownloadItem::ExternalData { 56 class SavePageData : public base::SupportsUserData::Data {
56 public: 57 public:
57 // A spoonful of syntactic sugar. 58 // A spoonful of syntactic sugar.
58 static bool Get(DownloadItem* item) { 59 static bool Get(DownloadItem* item) {
59 return item->GetExternalData(kKey) != NULL; 60 return item->GetUserData(kKey) != NULL;
60 } 61 }
61 62
62 explicit SavePageExternalData(DownloadItem* item) { 63 explicit SavePageData(DownloadItem* item) {
63 item->SetExternalData(kKey, this); 64 item->SetUserData(kKey, this);
64 } 65 }
65 66
66 virtual ~SavePageExternalData() {} 67 virtual ~SavePageData() {}
67 68
68 private: 69 private:
69 static const char kKey[]; 70 static const char kKey[];
70 71
71 DISALLOW_COPY_AND_ASSIGN(SavePageExternalData); 72 DISALLOW_COPY_AND_ASSIGN(SavePageData);
72 }; 73 };
73 74
74 const char SavePageExternalData::kKey[] = "DownloadItem SavePageExternalData"; 75 const char SavePageData::kKey[] = "DownloadItem SavePageData";
75 76
76 void BeginDownload(content::DownloadUrlParameters* params) { 77 void BeginDownload(content::DownloadUrlParameters* params) {
77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
78 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and 79 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and
79 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so 80 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so
80 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4. 81 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4.
81 scoped_ptr<net::URLRequest> request(new net::URLRequest( 82 scoped_ptr<net::URLRequest> request(new net::URLRequest(
82 params->url(), 83 params->url(),
83 NULL, 84 NULL,
84 params->resource_context()->GetRequestContext())); 85 params->resource_context()->GetRequestContext()));
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
559 page_url, 560 page_url,
560 is_otr, 561 is_otr,
561 GetNextId(), 562 GetNextId(),
562 mime_type, 563 mime_type,
563 bound_net_log); 564 bound_net_log);
564 565
565 download->AddObserver(observer); 566 download->AddObserver(observer);
566 567
567 DCHECK(!ContainsKey(downloads_, download->GetId())); 568 DCHECK(!ContainsKey(downloads_, download->GetId()));
568 downloads_[download->GetId()] = download; 569 downloads_[download->GetId()] = download;
569 DCHECK(!SavePageExternalData::Get(download)); 570 DCHECK(!SavePageData::Get(download));
570 new SavePageExternalData(download); 571 new SavePageData(download);
571 DCHECK(SavePageExternalData::Get(download)); 572 DCHECK(SavePageData::Get(download));
572 573
573 // TODO(benjhayden): Fire OnDownloadCreated for SavePackage downloads when 574 // TODO(benjhayden): Fire OnDownloadCreated for SavePackage downloads when
574 // we're comfortable with the user interacting with them. 575 // we're comfortable with the user interacting with them.
575 // FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); 576 // FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download));
576 577
577 // Will notify the observer in the callback. 578 // Will notify the observer in the callback.
578 if (delegate_) 579 if (delegate_)
579 delegate_->AddItemToPersistentStore(download); 580 delegate_->AddItemToPersistentStore(download);
580 581
581 return download; 582 return download;
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after
921 922
922 923
923 void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id, 924 void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id,
924 int64 db_handle) { 925 int64 db_handle) {
925 // It's valid that we don't find a matching item, i.e. on shutdown. 926 // It's valid that we don't find a matching item, i.e. on shutdown.
926 if (!ContainsKey(downloads_, download_id)) 927 if (!ContainsKey(downloads_, download_id))
927 return; 928 return;
928 929
929 DownloadItemImpl* item = downloads_[download_id]; 930 DownloadItemImpl* item = downloads_[download_id];
930 AddDownloadItemToHistory(item, db_handle); 931 AddDownloadItemToHistory(item, db_handle);
931 if (SavePageExternalData::Get(item)) { 932 if (SavePageData::Get(item)) {
932 OnSavePageItemAddedToPersistentStore(item); 933 OnSavePageItemAddedToPersistentStore(item);
933 } else { 934 } else {
934 OnDownloadItemAddedToPersistentStore(item); 935 OnDownloadItemAddedToPersistentStore(item);
935 } 936 }
936 } 937 }
937 938
938 // Once the new DownloadItem has been committed to the persistent store, 939 // Once the new DownloadItem has been committed to the persistent store,
939 // associate it with its db_handle (TODO(benjhayden) merge db_handle with id), 940 // associate it with its db_handle (TODO(benjhayden) merge db_handle with id),
940 // show it in the browser (TODO(benjhayden) the ui should observe us instead), 941 // show it in the browser (TODO(benjhayden) the ui should observe us instead),
941 // and notify observers (TODO(benjhayden) observers should be able to see the 942 // and notify observers (TODO(benjhayden) observers should be able to see the
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
1115 void DownloadManagerImpl::DownloadRenamedToFinalName( 1116 void DownloadManagerImpl::DownloadRenamedToFinalName(
1116 DownloadItemImpl* download) { 1117 DownloadItemImpl* download) {
1117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1118 // If the rename failed, we receive an OnDownloadInterrupted() call before we 1119 // If the rename failed, we receive an OnDownloadInterrupted() call before we
1119 // receive the DownloadRenamedToFinalName() call. 1120 // receive the DownloadRenamedToFinalName() call.
1120 if (delegate_) { 1121 if (delegate_) {
1121 delegate_->UpdatePathForItemInPersistentStore( 1122 delegate_->UpdatePathForItemInPersistentStore(
1122 download, download->GetFullPath()); 1123 download, download->GetFullPath());
1123 } 1124 }
1124 } 1125 }
OLDNEW
« no previous file with comments | « content/browser/download/download_item_impl_unittest.cc ('k') | content/browser/download/download_manager_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698