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 #ifndef IOS_INTERNAL_WEB_WEBUI_URL_DATA_SOURCE_IMPL_IOS_H_
6 #define IOS_INTERNAL_WEB_WEBUI_URL_DATA_SOURCE_IMPL_IOS_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/sequenced_task_runner_helpers.h"
11 #include "ios/web/webui/url_data_manager_ios.h"
14 class RefCountedMemory
;
18 class URLDataManagerIOSBackend
;
19 class URLDataSourceIOS
;
20 class URLDataSourceIOSImpl
;
22 // Trait used to handle deleting a URLDataSourceIOS. Deletion happens on the UI
25 // Implementation note: the normal shutdown sequence is for the UI loop to stop
26 // pumping events then the IO loop and thread are stopped. When the
27 // URLDataSourceIOSs are no longer referenced (which happens when IO thread
28 // stops) they get added to the UI message loop for deletion. But because the
29 // UI loop has stopped by the time this happens the URLDataSourceIOSs would be
32 // To make sure URLDataSourceIOSs are properly deleted URLDataManagerIOS
33 // manages deletion of the URLDataSourceIOSs. When a URLDataSourceIOS is no
34 // longer referenced it is added to |data_sources_| and a task is posted to the
35 // UI thread to handle the actual deletion. During shutdown |DeleteDataSources|
36 // is invoked so that all pending URLDataSourceIOSs are properly deleted.
37 struct DeleteURLDataSourceIOS
{
38 static void Destruct(const URLDataSourceIOSImpl
* data_source
) {
39 URLDataManagerIOS::DeleteDataSource(data_source
);
43 // A URLDataSourceIOS is an object that can answer requests for data
44 // asynchronously. URLDataSourceIOSs are collectively owned with refcounting
45 // smart pointers and should never be deleted on the IO thread, since their
46 // calls are handled almost always on the UI thread and there's a possibility
47 // of a data race. The |DeleteDataSource| trait above is used to enforce this.
48 class URLDataSourceIOSImpl
49 : public base::RefCountedThreadSafe
<URLDataSourceIOSImpl
,
50 DeleteURLDataSourceIOS
> {
52 // See source_name_ below for docs on that parameter. Takes ownership of
54 URLDataSourceIOSImpl(const std::string
& source_name
,
55 URLDataSourceIOS
* source
);
57 // Report that a request has resulted in the data |bytes|.
58 // If the request can't be satisfied, pass NULL for |bytes| to indicate
59 // the request is over.
60 virtual void SendResponse(int request_id
, base::RefCountedMemory
* bytes
);
62 const std::string
& source_name() const { return source_name_
; }
63 URLDataSourceIOS
* source() const { return source_
.get(); }
66 virtual ~URLDataSourceIOSImpl();
69 friend class URLDataManagerIOS
;
70 friend class URLDataManagerIOSBackend
;
71 friend class base::DeleteHelper
<URLDataSourceIOSImpl
>;
73 // SendResponse invokes this on the IO thread. Notifies the backend to
74 // handle the actual work of sending the data.
75 virtual void SendResponseOnIOThread(
77 scoped_refptr
<base::RefCountedMemory
> bytes
);
79 // The name of this source.
80 // E.g., for favicons, this could be "favicon", which results in paths for
81 // specific resources like "favicon/34" getting sent to this source.
82 const std::string source_name_
;
84 // This field is set and maintained by URLDataManagerIOSBackend. It is set
85 // when the DataSource is added, and unset if the DataSource is removed. A
86 // DataSource can be removed in two ways: the URLDataManagerIOSBackend is
87 // deleted, or another DataSource is registered with the same name. backend_
88 // should only be accessed on the IO thread. This reference can't be via a
89 // scoped_refptr else there would be a cycle between the backend and data
91 URLDataManagerIOSBackend
* backend_
;
93 scoped_ptr
<URLDataSourceIOS
> source_
;
98 #endif // IOS_INTERNAL_WEB_WEBUI_URL_DATA_SOURCE_IMPL_IOS_H_