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 #include "content/browser/webui/url_data_source_impl.h" 6 7 #include "base/bind.h" 8 #include "base/memory/ref_counted_memory.h" 9 #include "base/strings/string_util.h" 10 #include "content/browser/webui/url_data_manager_backend.h" 11 #include "content/public/browser/browser_thread.h" 12 #include "content/public/browser/url_data_source.h" 13 14 namespace content { 15 16 URLDataSourceImpl::URLDataSourceImpl(const std::string& source_name, 17 URLDataSource* source) 18 : source_name_(source_name), 19 backend_(NULL), 20 source_(source) { 21 } 22 23 URLDataSourceImpl::~URLDataSourceImpl() { 24 } 25 26 void URLDataSourceImpl::SendResponse( 27 int request_id, 28 base::RefCountedMemory* bytes) { 29 // Take a ref-pointer on entry so byte->Release() will always get called. 30 scoped_refptr<base::RefCountedMemory> bytes_ptr(bytes); 31 if (URLDataManager::IsScheduledForDeletion(this)) { 32 // We're scheduled for deletion. Servicing the request would result in 33 // this->AddRef being invoked, even though the ref count is 0 and 'this' is 34 // about to be deleted. If the AddRef were allowed through, when 'this' is 35 // released it would be deleted again. 36 // 37 // This scenario occurs with DataSources that make history requests. Such 38 // DataSources do a history query in |StartDataRequest| and the request is 39 // live until the object is deleted (history requests don't up the ref 40 // count). This means it's entirely possible for the DataSource to invoke 41 // |SendResponse| between the time when there are no more refs and the time 42 // when the object is deleted. 43 return; 44 } 45 BrowserThread::PostTask( 46 BrowserThread::IO, FROM_HERE, 47 base::Bind(&URLDataSourceImpl::SendResponseOnIOThread, this, request_id, 48 bytes_ptr)); 49 } 50 51 void URLDataSourceImpl::SendResponseOnIOThread( 52 int request_id, 53 scoped_refptr<base::RefCountedMemory> bytes) { 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 55 if (backend_) 56 backend_->DataAvailable(request_id, bytes.get()); 57 } 58 59 } // namespace content 60