OLD | NEW |
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 "chrome/browser/ui/webui/chrome_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" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "content/browser/webui/url_data_manager_backend.h" |
17 #include "chrome/browser/ui/webui/chrome_url_data_manager_factory.h" | 17 #include "content/browser/webui/web_ui_data_source.h" |
18 #include "chrome/browser/ui/webui/chrome_url_data_manager_backend.h" | 18 #include "content/browser/resource_context_impl.h" |
19 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.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 using content::BrowserThread; | 24 using content::BrowserThread; |
24 | 25 |
25 static base::LazyInstance<base::Lock>::Leaky | 26 namespace { |
26 g_delete_lock = LAZY_INSTANCE_INITIALIZER; | 27 |
| 28 const char kURLDataManagerKeyName[] = "url_data_manager"; |
| 29 |
| 30 base::LazyInstance<base::Lock>::Leaky g_delete_lock = LAZY_INSTANCE_INITIALIZER; |
| 31 |
| 32 ChromeURLDataManager* GetFromBrowserContext(BrowserContext* context) { |
| 33 if (!context->GetUserData(kURLDataManagerKeyName)) { |
| 34 context->SetUserData(kURLDataManagerKeyName, |
| 35 new ChromeURLDataManager(context)); |
| 36 } |
| 37 return static_cast<ChromeURLDataManager*>( |
| 38 context->GetUserData(kURLDataManagerKeyName)); |
| 39 } |
| 40 |
| 41 // Invoked on the IO thread to do the actual adding of the DataSource. |
| 42 static void AddDataSourceOnIOThread( |
| 43 content::ResourceContext* resource_context, |
| 44 scoped_refptr<URLDataSourceImpl> data_source) { |
| 45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 46 GetURLDataManagerForResourceContext(resource_context)->AddDataSource( |
| 47 data_source.get()); |
| 48 } |
| 49 |
| 50 } // namespace |
| 51 |
27 | 52 |
28 // static | 53 // static |
29 ChromeURLDataManager::URLDataSources* ChromeURLDataManager::data_sources_ = | 54 ChromeURLDataManager::URLDataSources* ChromeURLDataManager::data_sources_ = |
30 NULL; | 55 NULL; |
31 | 56 |
32 // Invoked on the IO thread to do the actual adding of the DataSource. | |
33 static void AddDataSourceOnIOThread( | |
34 const base::Callback<ChromeURLDataManagerBackend*(void)>& backend, | |
35 scoped_refptr<URLDataSourceImpl> data_source) { | |
36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
37 backend.Run()->AddDataSource(data_source.get()); | |
38 } | |
39 | |
40 ChromeURLDataManager::ChromeURLDataManager( | 57 ChromeURLDataManager::ChromeURLDataManager( |
41 const base::Callback<ChromeURLDataManagerBackend*(void)>& backend) | 58 content::BrowserContext* browser_context) |
42 : backend_(backend) { | 59 : browser_context_(browser_context) { |
43 } | 60 } |
44 | 61 |
45 ChromeURLDataManager::~ChromeURLDataManager() { | 62 ChromeURLDataManager::~ChromeURLDataManager() { |
46 } | 63 } |
47 | 64 |
48 void ChromeURLDataManager::AddDataSource(URLDataSourceImpl* source) { | 65 void ChromeURLDataManager::AddDataSource(URLDataSourceImpl* source) { |
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
50 BrowserThread::PostTask( | 67 BrowserThread::PostTask( |
51 BrowserThread::IO, FROM_HERE, | 68 BrowserThread::IO, FROM_HERE, |
52 base::Bind(&AddDataSourceOnIOThread, | 69 base::Bind(&AddDataSourceOnIOThread, |
53 backend_, make_scoped_refptr(source))); | 70 browser_context_->GetResourceContext(), |
| 71 make_scoped_refptr(source))); |
54 } | 72 } |
55 | 73 |
56 // static | 74 // static |
57 void ChromeURLDataManager::DeleteDataSources() { | 75 void ChromeURLDataManager::DeleteDataSources() { |
58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
59 URLDataSources sources; | 77 URLDataSources sources; |
60 { | 78 { |
61 base::AutoLock lock(g_delete_lock.Get()); | 79 base::AutoLock lock(g_delete_lock.Get()); |
62 if (!data_sources_) | 80 if (!data_sources_) |
63 return; | 81 return; |
(...skipping 26 matching lines...) Expand all Loading... |
90 if (schedule_delete) { | 108 if (schedule_delete) { |
91 // Schedule a task to delete the DataSource back on the UI thread. | 109 // Schedule a task to delete the DataSource back on the UI thread. |
92 BrowserThread::PostTask( | 110 BrowserThread::PostTask( |
93 BrowserThread::UI, FROM_HERE, | 111 BrowserThread::UI, FROM_HERE, |
94 base::Bind(&ChromeURLDataManager::DeleteDataSources)); | 112 base::Bind(&ChromeURLDataManager::DeleteDataSources)); |
95 } | 113 } |
96 } | 114 } |
97 | 115 |
98 // static | 116 // static |
99 void ChromeURLDataManager::AddDataSource( | 117 void ChromeURLDataManager::AddDataSource( |
100 Profile* profile, | 118 content::BrowserContext* browser_context, |
101 content::URLDataSource* source) { | 119 content::URLDataSource* source) { |
102 ChromeURLDataManagerFactory::GetForProfile(profile)->AddDataSource( | 120 GetFromBrowserContext(browser_context)-> |
103 new URLDataSourceImpl(source->GetSource(), source)); | 121 AddDataSource(new URLDataSourceImpl(source->GetSource(), source)); |
104 } | 122 } |
105 | 123 |
106 // static | 124 // static |
107 void ChromeURLDataManager::AddWebUIDataSource( | 125 void ChromeURLDataManager::AddWebUIDataSource( |
108 Profile* profile, | 126 content::BrowserContext* browser_context, |
109 content::WebUIDataSource* source) { | 127 content::WebUIDataSource* source) { |
110 ChromeWebUIDataSource* impl = static_cast<ChromeWebUIDataSource*>(source); | 128 ChromeWebUIDataSource* impl = static_cast<ChromeWebUIDataSource*>(source); |
111 ChromeURLDataManagerFactory::GetForProfile(profile)->AddDataSource(impl); | 129 GetFromBrowserContext(browser_context)->AddDataSource(impl); |
112 } | 130 } |
113 | 131 |
114 // static | 132 // static |
115 bool ChromeURLDataManager::IsScheduledForDeletion( | 133 bool ChromeURLDataManager::IsScheduledForDeletion( |
116 const URLDataSourceImpl* data_source) { | 134 const URLDataSourceImpl* data_source) { |
117 base::AutoLock lock(g_delete_lock.Get()); | 135 base::AutoLock lock(g_delete_lock.Get()); |
118 if (!data_sources_) | 136 if (!data_sources_) |
119 return false; | 137 return false; |
120 return std::find(data_sources_->begin(), data_sources_->end(), data_source) != | 138 return std::find(data_sources_->begin(), data_sources_->end(), data_source) != |
121 data_sources_->end(); | 139 data_sources_->end(); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 bytes_ptr)); | 174 bytes_ptr)); |
157 } | 175 } |
158 | 176 |
159 void URLDataSourceImpl::SendResponseOnIOThread( | 177 void URLDataSourceImpl::SendResponseOnIOThread( |
160 int request_id, | 178 int request_id, |
161 scoped_refptr<base::RefCountedMemory> bytes) { | 179 scoped_refptr<base::RefCountedMemory> bytes) { |
162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
163 if (backend_) | 181 if (backend_) |
164 backend_->DataAvailable(request_id, bytes); | 182 backend_->DataAvailable(request_id, bytes); |
165 } | 183 } |
OLD | NEW |