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.
5 #ifndef CONTENT_BROWSER_WEBUI_URL_DATA_MANAGER_BACKEND_H_
6 #define CONTENT_BROWSER_WEBUI_URL_DATA_MANAGER_BACKEND_H_
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/supports_user_data.h"
16 #include "content/browser/webui/url_data_manager.h"
17 #include "content/public/browser/url_data_source.h"
18 #include "net/url_request/url_request_job_factory.h"
23 class RefCountedMemory
;
28 class AppCacheServiceImpl
;
29 class ChromeBlobStorageContext
;
30 class ResourceContext
;
31 class URLDataManagerBackend
;
32 class URLDataSourceImpl
;
33 class URLRequestChromeJob
;
35 // URLDataManagerBackend is used internally by ChromeURLDataManager on the IO
36 // thread. In most cases you can use the API in ChromeURLDataManager and ignore
37 // this class. URLDataManagerBackend is owned by ResourceContext.
38 class URLDataManagerBackend
: public base::SupportsUserData::Data
{
40 typedef int RequestID
;
42 URLDataManagerBackend();
43 ~URLDataManagerBackend() override
;
45 // Invoked to create the protocol handler for chrome://. |is_incognito| should
46 // be set for incognito profiles. Called on the UI thread.
47 CONTENT_EXPORT
static scoped_ptr
<net::URLRequestJobFactory::ProtocolHandler
>
48 CreateProtocolHandler(content::ResourceContext
* resource_context
,
50 AppCacheServiceImpl
* appcache_service
,
51 ChromeBlobStorageContext
* blob_storage_context
);
53 // Adds a DataSource to the collection of data sources.
54 void AddDataSource(URLDataSourceImpl
* source
);
56 // DataSource invokes this. Sends the data to the URLRequest.
57 void DataAvailable(RequestID request_id
, base::RefCountedMemory
* bytes
);
59 static net::URLRequestJob
* Factory(net::URLRequest
* request
,
60 const std::string
& scheme
);
63 friend class URLRequestChromeJob
;
65 typedef std::map
<std::string
,
66 scoped_refptr
<URLDataSourceImpl
> > DataSourceMap
;
67 typedef std::map
<RequestID
, URLRequestChromeJob
*> PendingRequestMap
;
69 // Called by the job when it's starting up.
70 // Returns false if |url| is not a URL managed by this object.
71 bool StartRequest(const net::URLRequest
* request
, URLRequestChromeJob
* job
);
73 // Helper function to call StartDataRequest on |source|'s delegate. This is
74 // needed because while we want to call URLDataSourceDelegate's method, we
75 // need to add a refcount on the source.
76 static void CallStartRequest(scoped_refptr
<URLDataSourceImpl
> source
,
77 const std::string
& path
,
78 int render_process_id
,
82 // Remove a request from the list of pending requests.
83 void RemoveRequest(URLRequestChromeJob
* job
);
85 // Returns true if the job exists in |pending_requests_|. False otherwise.
86 // Called by ~URLRequestChromeJob to verify that |pending_requests_| is kept
88 bool HasPendingJob(URLRequestChromeJob
* job
) const;
90 // Look up the data source for the request. Returns the source if it is found,
92 URLDataSourceImpl
* GetDataSourceFromURL(const GURL
& url
);
94 // Custom sources of data, keyed by source path (e.g. "favicon").
95 DataSourceMap data_sources_
;
97 // All pending URLRequestChromeJobs, keyed by ID of the request.
98 // URLRequestChromeJob calls into this object when it's constructed and
99 // destructed to ensure that the pointers in this map remain valid.
100 PendingRequestMap pending_requests_
;
102 // The ID we'll use for the next request we receive.
103 RequestID next_request_id_
;
105 DISALLOW_COPY_AND_ASSIGN(URLDataManagerBackend
);
108 // Creates protocol handler for chrome-devtools://. |is_incognito| should be
109 // set for incognito profiles.
110 net::URLRequestJobFactory::ProtocolHandler
*
111 CreateDevToolsProtocolHandler(content::ResourceContext
* resource_context
,
114 } // namespace content
116 #endif // CONTENT_BROWSER_WEBUI_URL_DATA_MANAGER_BACKEND_H_