OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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_EXTENSIONS_USER_SCRIPT_LISTENER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LISTENER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LISTENER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LISTENER_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 | 9 |
10 #include "base/ref_counted.h" | 10 #include "base/ref_counted.h" |
11 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" | 11 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" |
12 #include "chrome/common/extensions/url_pattern.h" | 12 #include "chrome/common/extensions/url_pattern.h" |
13 #include "chrome/common/notification_registrar.h" | 13 #include "chrome/common/notification_registrar.h" |
14 | 14 |
15 class Extension; | 15 class Extension; |
16 class MessageLoop; | |
17 class URLRequest; | 16 class URLRequest; |
18 | 17 |
19 // This class handles delaying of resource loads that depend on unloaded user | 18 // This class handles delaying of resource loads that depend on unloaded user |
20 // scripts. For each request that comes in, we check if it depends on a user | 19 // scripts. For each request that comes in, we check if it depends on a user |
21 // script, and if so, whether that user script is ready; if not, we delay the | 20 // script, and if so, whether that user script is ready; if not, we delay the |
22 // request. | 21 // request. |
23 // | 22 // |
24 // This class lives mostly on the IO thread. It listens on the UI thread for | 23 // This class lives mostly on the IO thread. It listens on the UI thread for |
25 // updates to loaded extensions. | 24 // updates to loaded extensions. |
26 class UserScriptListener | 25 class UserScriptListener |
27 : public base::RefCountedThreadSafe<UserScriptListener>, | 26 : public base::RefCountedThreadSafe<UserScriptListener>, |
28 public NotificationObserver { | 27 public NotificationObserver { |
29 public: | 28 public: |
30 UserScriptListener(MessageLoop* ui_loop, | 29 UserScriptListener(ResourceDispatcherHost* rdh); |
31 MessageLoop* io_loop, | |
32 ResourceDispatcherHost* rdh); | |
33 | 30 |
34 void OnResourceDispatcherHostGone() { resource_dispatcher_host_ = NULL; } | 31 void OnResourceDispatcherHostGone() { resource_dispatcher_host_ = NULL; } |
35 | 32 |
36 // Returns true if we're ready to service the request. Otherwise, if the | 33 // Returns true if we're ready to service the request. Otherwise, if the |
37 // request URL depends on any user scripts that haven't been loaded yet, we | 34 // request URL depends on any user scripts that haven't been loaded yet, we |
38 // will delay the request until we're ready. | 35 // will delay the request until we're ready. |
39 bool ShouldStartRequest(URLRequest* request); | 36 bool ShouldStartRequest(URLRequest* request); |
40 | 37 |
41 private: | 38 private: |
42 typedef std::list<URLPattern> URLPatterns; | 39 typedef std::list<URLPattern> URLPatterns; |
43 | 40 |
44 // Resume any requests that we delayed in order to wait for user scripts. | 41 // Resume any requests that we delayed in order to wait for user scripts. |
45 void StartDelayedRequests(); | 42 void StartDelayedRequests(); |
46 | 43 |
47 // Appends new url patterns to our list, also setting user_scripts_ready_ | 44 // Appends new url patterns to our list, also setting user_scripts_ready_ |
48 // to false. | 45 // to false. |
49 void AppendNewURLPatterns(const URLPatterns& new_patterns); | 46 void AppendNewURLPatterns(const URLPatterns& new_patterns); |
50 | 47 |
51 // Replaces our url pattern list. This is only used when patterns have been | 48 // Replaces our url pattern list. This is only used when patterns have been |
52 // deleted, so user_scripts_ready_ remains unchanged. | 49 // deleted, so user_scripts_ready_ remains unchanged. |
53 void ReplaceURLPatterns(const URLPatterns& patterns); | 50 void ReplaceURLPatterns(const URLPatterns& patterns); |
54 | 51 |
55 MessageLoop* ui_loop_; | |
56 MessageLoop* io_loop_; | |
57 ResourceDispatcherHost* resource_dispatcher_host_; | 52 ResourceDispatcherHost* resource_dispatcher_host_; |
58 | 53 |
59 // A list of every request that we delayed. Will be flushed when user scripts | 54 // A list of every request that we delayed. Will be flushed when user scripts |
60 // are ready. | 55 // are ready. |
61 typedef std::list<ResourceDispatcherHost::GlobalRequestID> DelayedRequests; | 56 typedef std::list<ResourceDispatcherHost::GlobalRequestID> DelayedRequests; |
62 DelayedRequests delayed_request_ids_; | 57 DelayedRequests delayed_request_ids_; |
63 | 58 |
64 // TODO(mpcomplete): the rest of this stuff should really be per-profile, but | 59 // TODO(mpcomplete): the rest of this stuff should really be per-profile, but |
65 // the complexity doesn't seem worth it at this point. | 60 // the complexity doesn't seem worth it at this point. |
66 | 61 |
(...skipping 14 matching lines...) Expand all Loading... |
81 virtual void Observe(NotificationType type, | 76 virtual void Observe(NotificationType type, |
82 const NotificationSource& source, | 77 const NotificationSource& source, |
83 const NotificationDetails& details); | 78 const NotificationDetails& details); |
84 | 79 |
85 NotificationRegistrar registrar_; | 80 NotificationRegistrar registrar_; |
86 | 81 |
87 DISALLOW_EVIL_CONSTRUCTORS(UserScriptListener); | 82 DISALLOW_EVIL_CONSTRUCTORS(UserScriptListener); |
88 }; | 83 }; |
89 | 84 |
90 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LISTENER_H_ | 85 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LISTENER_H_ |
OLD | NEW |