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

Side by Side Diff: chrome/browser/renderer_host/offline_resource_handler.cc

Issue 8662047: base::Bind: Implement a 1-arity CancelableCallback and use this to implement (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "chrome/browser/renderer_host/offline_resource_handler.h" 5 #include "chrome/browser/renderer_host/offline_resource_handler.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 27 matching lines...) Expand all
38 render_view_id_(route_id), 38 render_view_id_(route_id),
39 rdh_(rdh), 39 rdh_(rdh),
40 request_(request), 40 request_(request),
41 appcache_service_(appcache_service), 41 appcache_service_(appcache_service),
42 deferred_request_id_(-1) { 42 deferred_request_id_(-1) {
43 DCHECK(appcache_service_); 43 DCHECK(appcache_service_);
44 } 44 }
45 45
46 OfflineResourceHandler::~OfflineResourceHandler() { 46 OfflineResourceHandler::~OfflineResourceHandler() {
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
48 DCHECK(!appcache_completion_callback_.get()); 48 DCHECK(appcache_completion_callback_.IsCancelled());
49 } 49 }
50 50
51 bool OfflineResourceHandler::OnUploadProgress(int request_id, 51 bool OfflineResourceHandler::OnUploadProgress(int request_id,
52 uint64 position, 52 uint64 position,
53 uint64 size) { 53 uint64 size) {
54 return next_handler_->OnUploadProgress(request_id, position, size); 54 return next_handler_->OnUploadProgress(request_id, position, size);
55 } 55 }
56 56
57 bool OfflineResourceHandler::OnRequestRedirected( 57 bool OfflineResourceHandler::OnRequestRedirected(
58 int request_id, 58 int request_id,
(...skipping 11 matching lines...) Expand all
70 } 70 }
71 71
72 bool OfflineResourceHandler::OnResponseCompleted( 72 bool OfflineResourceHandler::OnResponseCompleted(
73 int request_id, 73 int request_id,
74 const net::URLRequestStatus& status, 74 const net::URLRequestStatus& status,
75 const std::string& security_info) { 75 const std::string& security_info) {
76 return next_handler_->OnResponseCompleted(request_id, status, security_info); 76 return next_handler_->OnResponseCompleted(request_id, status, security_info);
77 } 77 }
78 78
79 void OfflineResourceHandler::OnRequestClosed() { 79 void OfflineResourceHandler::OnRequestClosed() {
80 if (appcache_completion_callback_) { 80 if (!appcache_completion_callback_.IsCancelled()) {
81 appcache_completion_callback_->Cancel(); 81 appcache_completion_callback_.Cancel();
82 appcache_completion_callback_.release();
83 Release(); // Balanced with OnWillStart 82 Release(); // Balanced with OnWillStart
michaeln 2011/11/30 01:18:21 looks like another balancing call to Release() tha
James Hawkins 2011/11/30 01:44:58 Done.
84 } 83 }
84
85 next_handler_->OnRequestClosed(); 85 next_handler_->OnRequestClosed();
86 } 86 }
87 87
88 void OfflineResourceHandler::OnCanHandleOfflineComplete(int rv) { 88 void OfflineResourceHandler::OnCanHandleOfflineComplete(int rv) {
89 CHECK(appcache_completion_callback_);
90 appcache_completion_callback_ = NULL;
91 if (deferred_request_id_ == -1) { 89 if (deferred_request_id_ == -1) {
92 LOG(WARNING) << "OnCanHandleOfflineComplete called after completion: " 90 DLOG(FATAL) << "OnCanHandleOfflineComplete called after completion: "
93 << " this=" << this; 91 << " this=" << this;
94 NOTREACHED();
95 return;
michaeln 2011/11/30 01:18:21 assuming DLOG() is a noop in release builds, it'd
James Hawkins 2011/11/30 01:44:58 Done.
96 } 92 }
93
97 if (rv == net::OK) { 94 if (rv == net::OK) {
98 Resume(); 95 Resume();
99 Release(); // Balanced with OnWillStart
100 } else { 96 } else {
101 BrowserThread::PostTask( 97 BrowserThread::PostTask(
102 BrowserThread::UI, FROM_HERE, 98 BrowserThread::UI, FROM_HERE,
103 base::Bind(&OfflineResourceHandler::ShowOfflinePage, this)); 99 base::Bind(&OfflineResourceHandler::ShowOfflinePage, this));
104 } 100 }
105 } 101 }
106 102
107 bool OfflineResourceHandler::OnWillStart(int request_id, 103 bool OfflineResourceHandler::OnWillStart(int request_id,
108 const GURL& url, 104 const GURL& url,
109 bool* defer) { 105 bool* defer) {
110 if (ShouldShowOfflinePage(url)) { 106 if (ShouldShowOfflinePage(url)) {
111 deferred_request_id_ = request_id; 107 deferred_request_id_ = request_id;
112 deferred_url_ = url; 108 deferred_url_ = url;
113 DVLOG(1) << "OnWillStart: this=" << this << ", request id=" << request_id 109 DVLOG(1) << "OnWillStart: this=" << this << ", request id=" << request_id
114 << ", url=" << url; 110 << ", url=" << url;
115 AddRef(); // Balanced with OnCanHandleOfflineComplete 111
michaeln 2011/11/30 01:18:21 just checking... does the base::Bind do the AddRef
James Hawkins 2011/11/30 01:44:58 Yes.
116 DCHECK(!appcache_completion_callback_); 112 DCHECK(appcache_completion_callback_.IsCancelled());
117 appcache_completion_callback_ = 113 appcache_completion_callback_.Reset(
118 new net::CancelableOldCompletionCallback<OfflineResourceHandler>( 114 base::Bind(&OfflineResourceHandler::OnCanHandleOfflineComplete, this));
119 this, &OfflineResourceHandler::OnCanHandleOfflineComplete);
120 appcache_service_->CanHandleMainResourceOffline( 115 appcache_service_->CanHandleMainResourceOffline(
121 url, request_->first_party_for_cookies(), 116 url, request_->first_party_for_cookies(),
122 appcache_completion_callback_); 117 appcache_completion_callback_.callback());
123 118
124 *defer = true; 119 *defer = true;
125 return true; 120 return true;
126 } 121 }
127 return next_handler_->OnWillStart(request_id, url, defer); 122 return next_handler_->OnWillStart(request_id, url, defer);
128 } 123 }
129 124
130 // We'll let the original event handler provide a buffer, and reuse it for 125 // We'll let the original event handler provide a buffer, and reuse it for
131 // subsequent reads until we're done buffering. 126 // subsequent reads until we're done buffering.
132 bool OfflineResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, 127 bool OfflineResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf,
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 TabContents* tab_contents = render_view_host ? 202 TabContents* tab_contents = render_view_host ?
208 render_view_host->delegate()->GetAsTabContents() : NULL; 203 render_view_host->delegate()->GetAsTabContents() : NULL;
209 // There is a chance that the tab closed after we decided to show 204 // There is a chance that the tab closed after we decided to show
210 // the offline page on the IO thread and before we actually show the 205 // the offline page on the IO thread and before we actually show the
211 // offline page here on the UI thread. 206 // offline page here on the UI thread.
212 if (tab_contents) 207 if (tab_contents)
213 (new chromeos::OfflineLoadPage(tab_contents, deferred_url_, this))-> 208 (new chromeos::OfflineLoadPage(tab_contents, deferred_url_, this))->
214 Show(); 209 Show();
215 } 210 }
216 } 211 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698