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

Side by Side Diff: content/browser/webui/url_data_manager.cc

Issue 12093012: Clean up of url data manager classes in content: move URLDataSourceImpl to its own file, move all t… (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 10 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/webui/url_data_manager.h" 5 #include "content/browser/webui/url_data_manager.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
11 #include "base/memory/ref_counted_memory.h" 11 #include "base/memory/ref_counted_memory.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop.h" 12 #include "base/message_loop.h"
14 #include "base/string_util.h" 13 #include "base/string_util.h"
15 #include "base/synchronization/lock.h" 14 #include "base/synchronization/lock.h"
16 #include "content/browser/webui/url_data_manager_backend.h" 15 #include "content/browser/webui/url_data_manager_backend.h"
17 #include "content/browser/webui/web_ui_data_source.h" 16 #include "content/browser/webui/url_data_source_impl.h"
17 #include "content/browser/webui/web_ui_data_source_impl.h"
18 #include "content/browser/resource_context_impl.h" 18 #include "content/browser/resource_context_impl.h"
19 #include "content/public/browser/browser_context.h" 19 #include "content/public/browser/browser_context.h"
20 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/url_data_source.h" 21 #include "content/public/browser/url_data_source.h"
22 22
23 using content::BrowserContext; 23 namespace content {
24 using content::BrowserThread;
25
26 namespace { 24 namespace {
27 25
28 const char kURLDataManagerKeyName[] = "url_data_manager"; 26 const char kURLDataManagerKeyName[] = "url_data_manager";
29 27
30 base::LazyInstance<base::Lock>::Leaky g_delete_lock = LAZY_INSTANCE_INITIALIZER; 28 base::LazyInstance<base::Lock>::Leaky g_delete_lock = LAZY_INSTANCE_INITIALIZER;
31 29
32 ChromeURLDataManager* GetFromBrowserContext(BrowserContext* context) { 30 URLDataManager* GetFromBrowserContext(BrowserContext* context) {
33 if (!context->GetUserData(kURLDataManagerKeyName)) { 31 if (!context->GetUserData(kURLDataManagerKeyName)) {
34 context->SetUserData(kURLDataManagerKeyName, 32 context->SetUserData(kURLDataManagerKeyName, new URLDataManager(context));
35 new ChromeURLDataManager(context));
36 } 33 }
37 return static_cast<ChromeURLDataManager*>( 34 return static_cast<URLDataManager*>(
38 context->GetUserData(kURLDataManagerKeyName)); 35 context->GetUserData(kURLDataManagerKeyName));
39 } 36 }
40 37
41 // Invoked on the IO thread to do the actual adding of the DataSource. 38 // Invoked on the IO thread to do the actual adding of the DataSource.
42 static void AddDataSourceOnIOThread( 39 static void AddDataSourceOnIOThread(
43 content::ResourceContext* resource_context, 40 ResourceContext* resource_context,
44 scoped_refptr<URLDataSourceImpl> data_source) { 41 scoped_refptr<URLDataSourceImpl> data_source) {
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
46 GetURLDataManagerForResourceContext(resource_context)->AddDataSource( 43 GetURLDataManagerForResourceContext(resource_context)->AddDataSource(
47 data_source.get()); 44 data_source.get());
48 } 45 }
49 46
50 } // namespace 47 } // namespace
51 48
49 // static
50 URLDataManager::URLDataSources* URLDataManager::data_sources_ = NULL;
52 51
53 // static 52 URLDataManager::URLDataManager(BrowserContext* browser_context)
54 ChromeURLDataManager::URLDataSources* ChromeURLDataManager::data_sources_ =
55 NULL;
56
57 ChromeURLDataManager::ChromeURLDataManager(
58 content::BrowserContext* browser_context)
59 : browser_context_(browser_context) { 53 : browser_context_(browser_context) {
60 } 54 }
61 55
62 ChromeURLDataManager::~ChromeURLDataManager() { 56 URLDataManager::~URLDataManager() {
63 } 57 }
64 58
65 void ChromeURLDataManager::AddDataSource(URLDataSourceImpl* source) { 59 void URLDataManager::AddDataSource(URLDataSourceImpl* source) {
66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
67 BrowserThread::PostTask( 61 BrowserThread::PostTask(
68 BrowserThread::IO, FROM_HERE, 62 BrowserThread::IO, FROM_HERE,
69 base::Bind(&AddDataSourceOnIOThread, 63 base::Bind(&AddDataSourceOnIOThread,
70 browser_context_->GetResourceContext(), 64 browser_context_->GetResourceContext(),
71 make_scoped_refptr(source))); 65 make_scoped_refptr(source)));
72 } 66 }
73 67
74 // static 68 // static
75 void ChromeURLDataManager::DeleteDataSources() { 69 void URLDataManager::DeleteDataSources() {
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
77 URLDataSources sources; 71 URLDataSources sources;
78 { 72 {
79 base::AutoLock lock(g_delete_lock.Get()); 73 base::AutoLock lock(g_delete_lock.Get());
80 if (!data_sources_) 74 if (!data_sources_)
81 return; 75 return;
82 data_sources_->swap(sources); 76 data_sources_->swap(sources);
83 } 77 }
84 for (size_t i = 0; i < sources.size(); ++i) 78 for (size_t i = 0; i < sources.size(); ++i)
85 delete sources[i]; 79 delete sources[i];
86 } 80 }
87 81
88 // static 82 // static
89 void ChromeURLDataManager::DeleteDataSource( 83 void URLDataManager::DeleteDataSource(const URLDataSourceImpl* data_source) {
90 const URLDataSourceImpl* data_source) {
91 // Invoked when a DataSource is no longer referenced and needs to be deleted. 84 // Invoked when a DataSource is no longer referenced and needs to be deleted.
92 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { 85 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
93 // We're on the UI thread, delete right away. 86 // We're on the UI thread, delete right away.
94 delete data_source; 87 delete data_source;
95 return; 88 return;
96 } 89 }
97 90
98 // We're not on the UI thread, add the DataSource to the list of DataSources 91 // We're not on the UI thread, add the DataSource to the list of DataSources
99 // to delete. 92 // to delete.
100 bool schedule_delete = false; 93 bool schedule_delete = false;
101 { 94 {
102 base::AutoLock lock(g_delete_lock.Get()); 95 base::AutoLock lock(g_delete_lock.Get());
103 if (!data_sources_) 96 if (!data_sources_)
104 data_sources_ = new URLDataSources(); 97 data_sources_ = new URLDataSources();
105 schedule_delete = data_sources_->empty(); 98 schedule_delete = data_sources_->empty();
106 data_sources_->push_back(data_source); 99 data_sources_->push_back(data_source);
107 } 100 }
108 if (schedule_delete) { 101 if (schedule_delete) {
109 // Schedule a task to delete the DataSource back on the UI thread. 102 // Schedule a task to delete the DataSource back on the UI thread.
110 BrowserThread::PostTask( 103 BrowserThread::PostTask(
111 BrowserThread::UI, FROM_HERE, 104 BrowserThread::UI, FROM_HERE,
112 base::Bind(&ChromeURLDataManager::DeleteDataSources)); 105 base::Bind(&URLDataManager::DeleteDataSources));
113 } 106 }
114 } 107 }
115 108
116 // static 109 // static
117 void ChromeURLDataManager::AddDataSource( 110 void URLDataManager::AddDataSource(BrowserContext* browser_context,
118 content::BrowserContext* browser_context, 111 URLDataSource* source) {
119 content::URLDataSource* source) {
120 GetFromBrowserContext(browser_context)-> 112 GetFromBrowserContext(browser_context)->
121 AddDataSource(new URLDataSourceImpl(source->GetSource(), source)); 113 AddDataSource(new URLDataSourceImpl(source->GetSource(), source));
122 } 114 }
123 115
124 // static 116 // static
125 void ChromeURLDataManager::AddWebUIDataSource( 117 void URLDataManager::AddWebUIDataSource(BrowserContext* browser_context,
126 content::BrowserContext* browser_context, 118 WebUIDataSource* source) {
127 content::WebUIDataSource* source) { 119 WebUIDataSourceImpl* impl = static_cast<WebUIDataSourceImpl*>(source);
128 ChromeWebUIDataSource* impl = static_cast<ChromeWebUIDataSource*>(source);
129 GetFromBrowserContext(browser_context)->AddDataSource(impl); 120 GetFromBrowserContext(browser_context)->AddDataSource(impl);
130 } 121 }
131 122
132 // static 123 // static
133 bool ChromeURLDataManager::IsScheduledForDeletion( 124 bool URLDataManager::IsScheduledForDeletion(
134 const URLDataSourceImpl* data_source) { 125 const URLDataSourceImpl* data_source) {
135 base::AutoLock lock(g_delete_lock.Get()); 126 base::AutoLock lock(g_delete_lock.Get());
136 if (!data_sources_) 127 if (!data_sources_)
137 return false; 128 return false;
138 return std::find(data_sources_->begin(), data_sources_->end(), data_source) != 129 return std::find(data_sources_->begin(), data_sources_->end(), data_source) !=
139 data_sources_->end(); 130 data_sources_->end();
140 } 131 }
141 132
142 URLDataSourceImpl::URLDataSourceImpl(const std::string& source_name, 133 } // namespace content
143 content::URLDataSource* source)
144 : source_name_(source_name),
145 backend_(NULL),
146 source_(source) {
147 }
148
149 URLDataSourceImpl::~URLDataSourceImpl() {
150 }
151
152 void URLDataSourceImpl::SendResponse(
153 int request_id,
154 base::RefCountedMemory* bytes) {
155 // Take a ref-pointer on entry so byte->Release() will always get called.
156 scoped_refptr<base::RefCountedMemory> bytes_ptr(bytes);
157 if (ChromeURLDataManager::IsScheduledForDeletion(this)) {
158 // We're scheduled for deletion. Servicing the request would result in
159 // this->AddRef being invoked, even though the ref count is 0 and 'this' is
160 // about to be deleted. If the AddRef were allowed through, when 'this' is
161 // released it would be deleted again.
162 //
163 // This scenario occurs with DataSources that make history requests. Such
164 // DataSources do a history query in |StartDataRequest| and the request is
165 // live until the object is deleted (history requests don't up the ref
166 // count). This means it's entirely possible for the DataSource to invoke
167 // |SendResponse| between the time when there are no more refs and the time
168 // when the object is deleted.
169 return;
170 }
171 BrowserThread::PostTask(
172 BrowserThread::IO, FROM_HERE,
173 base::Bind(&URLDataSourceImpl::SendResponseOnIOThread, this, request_id,
174 bytes_ptr));
175 }
176
177 void URLDataSourceImpl::SendResponseOnIOThread(
178 int request_id,
179 scoped_refptr<base::RefCountedMemory> bytes) {
180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
181 if (backend_)
182 backend_->DataAvailable(request_id, bytes);
183 }
OLDNEW
« no previous file with comments | « content/browser/webui/url_data_manager.h ('k') | content/browser/webui/url_data_manager_backend.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698