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