- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / loader / resource_scheduler.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_LOADER_RESOURCE_SCHEDULER_H_
6 #define CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_
7
8 #include <map>
9 #include <set>
10
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "content/common/content_export.h"
16 #include "net/base/priority_queue.h"
17 #include "net/base/request_priority.h"
18
19 namespace net {
20 class URLRequest;
21 }
22
23 namespace content {
24 class ResourceThrottle;
25
26 // There is one ResourceScheduler. All renderer-initiated HTTP requests are
27 // expected to pass through it.
28 //
29 // There are two types of input to the scheduler:
30 // 1. Requests to start, cancel, or finish fetching a resource.
31 // 2. Notifications for renderer events, such as new tabs, navigation and
32 //    painting.
33 //
34 // These input come from different threads, so they may not be in sync. The UI
35 // thread is considered the authority on renderer lifetime, which means some
36 // IPCs may be meaningless if they arrive after the UI thread signals a renderer
37 // has been deleted.
38 //
39 // The ResourceScheduler tracks many Clients, which should correlate with tabs.
40 // A client is uniquely identified by its child_id and route_id.
41 //
42 // Each Client may have many Requests in flight. Requests are uniquely
43 // identified within a Client by its ScheduledResourceRequest.
44 //
45 // Users should call ScheduleRequest() to notify this ResourceScheduler of a
46 // new request. The returned ResourceThrottle should be destroyed when the load
47 // finishes or is canceled.
48 //
49 // The scheduler may defer issuing the request via the ResourceThrottle
50 // interface or it may alter the request's priority by calling set_priority() on
51 // the URLRequest.
52 class CONTENT_EXPORT ResourceScheduler : public base::NonThreadSafe {
53  public:
54   ResourceScheduler();
55   ~ResourceScheduler();
56
57   // Requests that this ResourceScheduler schedule, and eventually loads, the
58   // specified |url_request|. Caller should delete the returned ResourceThrottle
59   // when the load completes or is canceled.
60   scoped_ptr<ResourceThrottle> ScheduleRequest(
61       int child_id, int route_id, net::URLRequest* url_request);
62
63   // Signals from the UI thread, posted as tasks on the IO thread:
64
65   // Called when a renderer is created.
66   void OnClientCreated(int child_id, int route_id);
67
68   // Called when a renderer is destroyed.
69   void OnClientDeleted(int child_id, int route_id);
70
71   // Signals from IPC messages directly from the renderers:
72
73   // Called when a client navigates to a new main document.
74   void OnNavigate(int child_id, int route_id);
75
76   // Called when the client has parsed the <body> element. This is a signal that
77   // resource loads won't interfere with first paint.
78   void OnWillInsertBody(int child_id, int route_id);
79
80  private:
81   class RequestQueue;
82   class ScheduledResourceRequest;
83   struct Client;
84
85   typedef int64 ClientId;
86   typedef std::map<ClientId, Client*> ClientMap;
87   typedef std::set<ScheduledResourceRequest*> RequestSet;
88
89   // Called when a ScheduledResourceRequest is destroyed.
90   void RemoveRequest(ScheduledResourceRequest* request);
91
92   // Unthrottles the |request| and adds it to |client|.
93   void StartRequest(ScheduledResourceRequest* request, Client* client);
94
95   // Update the queue position for |request|, possibly causing it to start
96   // loading.
97   //
98   // Queues are maintained for each priority level. When |request| is
99   // reprioritized, it will move to the end of the queue for that priority
100   // level.
101   void ReprioritizeRequest(ScheduledResourceRequest* request,
102                            net::RequestPriority new_priority);
103
104   // Attempts to load any pending requests in |client|, based on the
105   // results of ShouldStartRequest().
106   void LoadAnyStartablePendingRequests(Client* client);
107
108   // Returns the number of requests with priority < LOW that are currently in
109   // flight.
110   size_t GetNumDelayableRequestsInFlight(Client* client) const;
111
112   // Returns true if the request should start. This is the core scheduling
113   // algorithm.
114   bool ShouldStartRequest(ScheduledResourceRequest* request,
115                           Client* client) const;
116
117   // Returns the client ID for the given |child_id| and |route_id| combo.
118   ClientId MakeClientId(int child_id, int route_id);
119
120   ClientMap client_map_;
121   RequestSet unowned_requests_;
122 };
123
124 }  // namespace content
125
126 #endif  // CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_