1 // Copyright 2014 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.
5 #include "ios/web/webui/url_data_manager_ios.h"
10 #include "base/lazy_instance.h"
11 #include "base/memory/ref_counted_memory.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/strings/string_util.h"
14 #include "base/synchronization/lock.h"
15 #include "ios/web/public/browser_state.h"
16 #include "ios/web/public/url_data_source_ios.h"
17 #include "ios/web/public/web_thread.h"
18 #include "ios/web/webui/url_data_manager_ios_backend.h"
19 #include "ios/web/webui/url_data_source_ios_impl.h"
20 #include "ios/web/webui/web_ui_ios_data_source_impl.h"
25 const char kURLDataManagerIOSKeyName
[] = "url_data_manager";
27 base::LazyInstance
<base::Lock
>::Leaky g_delete_lock
= LAZY_INSTANCE_INITIALIZER
;
29 URLDataManagerIOS
* GetFromBrowserState(BrowserState
* browser_state
) {
30 if (!browser_state
->GetUserData(kURLDataManagerIOSKeyName
)) {
31 browser_state
->SetUserData(kURLDataManagerIOSKeyName
,
32 new URLDataManagerIOS(browser_state
));
34 return static_cast<URLDataManagerIOS
*>(
35 browser_state
->GetUserData(kURLDataManagerIOSKeyName
));
41 URLDataManagerIOS::URLDataSources
* URLDataManagerIOS::data_sources_
= NULL
;
44 void URLDataManagerIOS::AddDataSourceOnIOThread(
45 BrowserState
* browser_state
,
46 scoped_refptr
<URLDataSourceIOSImpl
> data_source
) {
47 DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::IO
);
48 browser_state
->GetURLDataManagerIOSBackendOnIOThread()->AddDataSource(
52 URLDataManagerIOS::URLDataManagerIOS(BrowserState
* browser_state
)
53 : browser_state_(browser_state
) {
56 URLDataManagerIOS::~URLDataManagerIOS() {
59 void URLDataManagerIOS::AddDataSource(URLDataSourceIOSImpl
* source
) {
60 DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI
);
61 web::WebThread::PostTask(
62 web::WebThread::IO
, FROM_HERE
,
63 base::Bind(&AddDataSourceOnIOThread
, base::Unretained(browser_state_
),
64 make_scoped_refptr(source
)));
68 void URLDataManagerIOS::DeleteDataSources() {
69 DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI
);
70 URLDataSources sources
;
72 base::AutoLock
lock(g_delete_lock
.Get());
75 data_sources_
->swap(sources
);
77 for (size_t i
= 0; i
< sources
.size(); ++i
)
82 void URLDataManagerIOS::DeleteDataSource(
83 const URLDataSourceIOSImpl
* data_source
) {
84 // Invoked when a DataSource is no longer referenced and needs to be deleted.
85 if (web::WebThread::CurrentlyOn(web::WebThread::UI
)) {
86 // We're on the UI thread, delete right away.
91 // We're not on the UI thread, add the DataSource to the list of DataSources
93 bool schedule_delete
= false;
95 base::AutoLock
lock(g_delete_lock
.Get());
97 data_sources_
= new URLDataSources();
98 schedule_delete
= data_sources_
->empty();
99 data_sources_
->push_back(data_source
);
101 if (schedule_delete
) {
102 // Schedule a task to delete the DataSource back on the UI thread.
103 web::WebThread::PostTask(web::WebThread::UI
, FROM_HERE
,
104 base::Bind(&URLDataManagerIOS::DeleteDataSources
));
109 void URLDataManagerIOS::AddDataSource(BrowserState
* browser_state
,
110 URLDataSourceIOS
* source
) {
111 GetFromBrowserState(browser_state
)
112 ->AddDataSource(new URLDataSourceIOSImpl(source
->GetSource(), source
));
116 void URLDataManagerIOS::AddWebUIIOSDataSource(BrowserState
* browser_state
,
117 WebUIIOSDataSource
* source
) {
118 WebUIIOSDataSourceImpl
* impl
= static_cast<WebUIIOSDataSourceImpl
*>(source
);
119 GetFromBrowserState(browser_state
)->AddDataSource(impl
);
123 bool URLDataManagerIOS::IsScheduledForDeletion(
124 const URLDataSourceIOSImpl
* data_source
) {
125 base::AutoLock
lock(g_delete_lock
.Get());
128 return std::find(data_sources_
->begin(), data_sources_
->end(), data_source
) !=
129 data_sources_
->end();