• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 AppCacheService;
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::AppCacheService* 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_view_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   // Custom sources of data, keyed by source path (e.g. "favicon").
92   DataSourceMap data_sources_;
93 
94   // All pending URLRequestChromeJobs, keyed by ID of the request.
95   // URLRequestChromeJob calls into this object when it's constructed and
96   // destructed to ensure that the pointers in this map remain valid.
97   PendingRequestMap pending_requests_;
98 
99   // The ID we'll use for the next request we receive.
100   RequestID next_request_id_;
101 
102   DISALLOW_COPY_AND_ASSIGN(URLDataManagerBackend);
103 };
104 
105 // Creates protocol handler for chrome-devtools://. |is_incognito| should be
106 // set for incognito profiles.
107 net::URLRequestJobFactory::ProtocolHandler*
108 CreateDevToolsProtocolHandler(content::ResourceContext* resource_context,
109                               bool is_incognito);
110 
111 }  // namespace content
112 
113 #endif  // CONTENT_BROWSER_WEBUI_URL_DATA_MANAGER_BACKEND_H_
114