• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 CHROME_BROWSER_UI_WEBUI_CHROME_URL_DATA_MANAGER_BACKEND_H_
6 #define CHROME_BROWSER_UI_WEBUI_CHROME_URL_DATA_MANAGER_BACKEND_H_
7 #pragma once
8 
9 #include "base/basictypes.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/task.h"
12 #include "chrome/browser/ui/webui/chrome_url_data_manager.h"
13 
14 #include <map>
15 #include <string>
16 #include <vector>
17 
18 class FilePath;
19 class GURL;
20 class URLRequestChromeJob;
21 
22 namespace net {
23 class URLRequest;
24 class URLRequestJob;
25 }
26 
27 // ChromeURLDataManagerBackend is used internally by ChromeURLDataManager on the
28 // IO thread. In most cases you can use the API in ChromeURLDataManager and
29 // ignore this class. ChromeURLDataManagerBackend is owned by
30 // ChromeURLRequestContext.
31 class ChromeURLDataManagerBackend {
32  public:
33   typedef int RequestID;
34 
35   ChromeURLDataManagerBackend();
36   ~ChromeURLDataManagerBackend();
37 
38   // Invoked to register the protocol factories.
39   static void Register();
40 
41   // Adds a DataSource to the collection of data sources.
42   void AddDataSource(ChromeURLDataManager::DataSource* source);
43 
44   // DataSource invokes this. Sends the data to the URLRequest.
45   void DataAvailable(RequestID request_id, RefCountedMemory* bytes);
46 
47   static net::URLRequestJob* Factory(net::URLRequest* request,
48                                      const std::string& scheme);
49 
50  private:
51   friend class URLRequestChromeJob;
52 
53   typedef std::map<std::string,
54       scoped_refptr<ChromeURLDataManager::DataSource> > DataSourceMap;
55   typedef std::map<RequestID, URLRequestChromeJob*> PendingRequestMap;
56 
57   // Called by the job when it's starting up.
58   // Returns false if |url| is not a URL managed by this object.
59   bool StartRequest(const GURL& url, URLRequestChromeJob* job);
60 
61   // Remove a request from the list of pending requests.
62   void RemoveRequest(URLRequestChromeJob* job);
63 
64   // Returns true if the job exists in |pending_requests_|. False otherwise.
65   // Called by ~URLRequestChromeJob to verify that |pending_requests_| is kept
66   // up to date.
67   bool HasPendingJob(URLRequestChromeJob* job) const;
68 
69   // Custom sources of data, keyed by source path (e.g. "favicon").
70   DataSourceMap data_sources_;
71 
72   // All pending URLRequestChromeJobs, keyed by ID of the request.
73   // URLRequestChromeJob calls into this object when it's constructed and
74   // destructed to ensure that the pointers in this map remain valid.
75   PendingRequestMap pending_requests_;
76 
77   // The ID we'll use for the next request we receive.
78   RequestID next_request_id_;
79 
80   DISALLOW_COPY_AND_ASSIGN(ChromeURLDataManagerBackend);
81 };
82 
83 #endif  // CHROME_BROWSER_UI_WEBUI_CHROME_URL_DATA_MANAGER_BACKEND_H_
84