OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ | 5 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ |
6 #define CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ | 6 #define CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/callback_forward.h" | |
14 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
15 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
16 #include "base/threading/non_thread_safe.h" | 15 #include "base/threading/non_thread_safe.h" |
17 #include "chrome/browser/prerender/prerender_final_status.h" | 16 #include "chrome/browser/prerender/prerender_final_status.h" |
18 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
19 | 18 |
20 namespace prerender { | 19 namespace prerender { |
21 | 20 |
22 class PrerenderManager; | 21 class PrerenderManager; |
23 struct RenderViewInfo; | 22 struct RenderViewInfo; |
24 | 23 |
25 // An URLCounter keeps track of the number of occurrences of a prerendered URL. | |
26 class URLCounter : public base::NonThreadSafe { | |
27 public: | |
28 URLCounter(); | |
29 ~URLCounter(); | |
30 | |
31 // Determines whether the URL is contained in the set. | |
32 bool MatchesURL(const GURL& url) const; | |
33 | |
34 // Adds a URL to the set. | |
35 void AddURL(const GURL& url); | |
36 | |
37 // Removes a number of URLs from the set. | |
38 void RemoveURLs(const std::vector<GURL>& urls); | |
39 | |
40 private: | |
41 typedef std::map<GURL, int> URLCountMap; | |
42 URLCountMap url_count_map_; | |
43 }; | |
44 | |
45 // PrerenderTracker is responsible for keeping track of all prerendering | 24 // PrerenderTracker is responsible for keeping track of all prerendering |
46 // RenderViews and their statuses. Its list is guaranteed to be up to date | 25 // RenderViews and their statuses. Its list is guaranteed to be up to date |
47 // and can be modified on any thread. | 26 // and can be modified on any thread. |
48 class PrerenderTracker { | 27 class PrerenderTracker { |
49 public: | 28 public: |
50 typedef base::Callback<void(bool /* proceed */)> CheckURLCallback; | |
51 | |
52 PrerenderTracker(); | 29 PrerenderTracker(); |
53 ~PrerenderTracker(); | 30 ~PrerenderTracker(); |
54 | 31 |
55 // Attempts to set the status of the specified RenderViewHost to | 32 // Attempts to set the status of the specified RenderViewHost to |
56 // FINAL_STATUS_USED. Returns true on success. Returns false if it has | 33 // FINAL_STATUS_USED. Returns true on success. Returns false if it has |
57 // already been cancelled for any reason or is no longer prerendering. | 34 // already been cancelled for any reason or is no longer prerendering. |
58 // Can only be called only on the IO thread. This method will not call | 35 // Can only be called only on the IO thread. This method will not call |
59 // PrerenderContents::set_final_status() on the corresponding | 36 // PrerenderContents::set_final_status() on the corresponding |
60 // PrerenderContents. | 37 // PrerenderContents. |
61 // | 38 // |
(...skipping 10 matching lines...) Expand all Loading... |
72 // When true is returned, it is guaranteed that the RenderView will never | 49 // When true is returned, it is guaranteed that the RenderView will never |
73 // be displayed. When false is returned, the RenderView has either been | 50 // be displayed. When false is returned, the RenderView has either been |
74 // swapped into a tab or has already been destroyed. | 51 // swapped into a tab or has already been destroyed. |
75 bool TryCancel(int child_id, int route_id, FinalStatus final_status); | 52 bool TryCancel(int child_id, int route_id, FinalStatus final_status); |
76 | 53 |
77 // Same as above, but can only called on the IO Thread. Does not acquire a | 54 // Same as above, but can only called on the IO Thread. Does not acquire a |
78 // lock when the RenderView is not being prerendered. | 55 // lock when the RenderView is not being prerendered. |
79 bool TryCancelOnIOThread(int child_id, int route_id, | 56 bool TryCancelOnIOThread(int child_id, int route_id, |
80 FinalStatus final_status); | 57 FinalStatus final_status); |
81 | 58 |
82 // Potentially delay a resource request on the IO thread to prevent a double | |
83 // get. When this method returns true, the callback will be run later to | |
84 // indicate if the request should be allowed or canceled. | |
85 bool PotentiallyDelayRequestOnIOThread( | |
86 const GURL& gurl, | |
87 int child_id, | |
88 int route_id, | |
89 const CheckURLCallback& callback); | |
90 | |
91 void AddPrerenderURLOnUIThread(const GURL& url); | 59 void AddPrerenderURLOnUIThread(const GURL& url); |
92 void RemovePrerenderURLsOnUIThread(const std::vector<GURL>& urls); | 60 void RemovePrerenderURLsOnUIThread(const std::vector<GURL>& urls); |
93 | 61 |
94 // Gets the FinalStatus of the specified prerendered RenderView. Returns | 62 // Gets the FinalStatus of the specified prerendered RenderView. Returns |
95 // |true| and sets |final_status| to the status of the RenderView if it | 63 // |true| and sets |final_status| to the status of the RenderView if it |
96 // is found, returns false otherwise. | 64 // is found, returns false otherwise. |
97 bool GetFinalStatus(int child_id, int route_id, | 65 bool GetFinalStatus(int child_id, int route_id, |
98 FinalStatus* final_status) const; | 66 FinalStatus* final_status) const; |
99 | 67 |
100 // Returns whether or not a RenderView is prerendering. Can only be called on | 68 // Returns whether or not a RenderView is prerendering. Can only be called on |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 // Map containing child/route id pairs and their final statuses. Must only be | 129 // Map containing child/route id pairs and their final statuses. Must only be |
162 // accessed while the lock is held. Values are always accurate and up to | 130 // accessed while the lock is held. Values are always accurate and up to |
163 // date. | 131 // date. |
164 FinalStatusMap final_status_map_; | 132 FinalStatusMap final_status_map_; |
165 | 133 |
166 // Superset of child/route id pairs that are prerendering. Can only access on | 134 // Superset of child/route id pairs that are prerendering. Can only access on |
167 // the IO thread. May contain entries that have since been displayed. Only | 135 // the IO thread. May contain entries that have since been displayed. Only |
168 // used to prevent locking when not needed. | 136 // used to prevent locking when not needed. |
169 PossiblyPrerenderingChildRouteIdPairs possibly_prerendering_io_thread_set_; | 137 PossiblyPrerenderingChildRouteIdPairs possibly_prerendering_io_thread_set_; |
170 | 138 |
171 // |url_counter_| keeps track of the top-level URLs which are being | |
172 // prerendered. It must only be accessed on the IO thread. | |
173 URLCounter url_counter_; | |
174 | |
175 DISALLOW_COPY_AND_ASSIGN(PrerenderTracker); | 139 DISALLOW_COPY_AND_ASSIGN(PrerenderTracker); |
176 }; | 140 }; |
177 | 141 |
178 } // namespace prerender | 142 } // namespace prerender |
179 | 143 |
180 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ | 144 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TRACKER_H_ |
OLD | NEW |