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_MANAGER_BACKEND_IOS_H_
6 #define IOS_INTERNAL_WEB_WEBUI_URL_DATA_MANAGER_BACKEND_IOS_H_
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/supports_user_data.h"
15 #include "ios/web/public/url_data_source_ios.h"
16 #include "ios/web/webui/url_data_manager_ios.h"
17 #include "net/url_request/url_request_job_factory.h"
22 class RefCountedMemory
;
27 class URLDataSourceIOSImpl
;
28 class URLRequestChromeJob
;
30 // URLDataManagerIOSBackend is used internally by URLDataManagerIOS on
31 // the IO thread. In most cases you can use the API in URLDataManagerIOS
32 // and ignore this class. URLDataManagerIOSBackend is owned by BrowserState.
33 class URLDataManagerIOSBackend
: public base::SupportsUserData::Data
{
35 typedef int RequestID
;
37 URLDataManagerIOSBackend();
38 ~URLDataManagerIOSBackend() override
;
40 // Invoked to create the protocol handler for chrome://. |is_incognito| should
41 // be set for incognito browser states. Called on the UI thread.
42 static net::URLRequestJobFactory::ProtocolHandler
* CreateProtocolHandler(
43 BrowserState
* browser_state
);
45 // Adds a DataSource to the collection of data sources.
46 void AddDataSource(URLDataSourceIOSImpl
* source
);
48 // DataSource invokes this. Sends the data to the URLRequest.
49 void DataAvailable(RequestID request_id
, base::RefCountedMemory
* bytes
);
51 static net::URLRequestJob
* Factory(net::URLRequest
* request
,
52 const std::string
& scheme
);
55 friend class URLRequestChromeJob
;
57 typedef std::map
<std::string
, scoped_refptr
<URLDataSourceIOSImpl
> >
59 typedef std::map
<RequestID
, URLRequestChromeJob
*> PendingRequestMap
;
61 // Called by the job when it's starting up.
62 // Returns false if |url| is not a URL managed by this object.
63 bool StartRequest(const net::URLRequest
* request
, URLRequestChromeJob
* job
);
65 // Helper function to call StartDataRequest on |source|'s delegate. This is
66 // needed because while we want to call URLDataSourceIOSDelegate's method, we
67 // need to add a refcount on the source.
68 static void CallStartRequest(scoped_refptr
<URLDataSourceIOSImpl
> source
,
69 const std::string
& path
,
72 // Remove a request from the list of pending requests.
73 void RemoveRequest(URLRequestChromeJob
* job
);
75 // Returns true if the job exists in |pending_requests_|. False otherwise.
76 // Called by ~URLRequestChromeJob to verify that |pending_requests_| is kept
78 bool HasPendingJob(URLRequestChromeJob
* job
) const;
80 // Look up the data source for the request. Returns the source if it is found,
82 URLDataSourceIOSImpl
* GetDataSourceFromURL(const GURL
& url
);
84 // Custom sources of data, keyed by source path (e.g. "favicon").
85 DataSourceMap data_sources_
;
87 // All pending URLRequestChromeJobs, keyed by ID of the request.
88 // URLRequestChromeJob calls into this object when it's constructed and
89 // destructed to ensure that the pointers in this map remain valid.
90 PendingRequestMap pending_requests_
;
92 // The ID we'll use for the next request we receive.
93 RequestID next_request_id_
;
95 DISALLOW_COPY_AND_ASSIGN(URLDataManagerIOSBackend
);
100 #endif // IOS_INTERNAL_WEB_WEBUI_URL_DATA_MANAGER_BACKEND_IOS_H_