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