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_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ |
6 #define CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ | 6 #define CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "chrome/browser/web_resource/json_asynchronous_unpacker.h" | |
14 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
15 #include "net/url_request/url_fetcher_delegate.h" | 16 #include "net/url_request/url_fetcher_delegate.h" |
16 | 17 |
17 class PrefService; | 18 class PrefService; |
18 class ResourceDispatcherHost; | 19 class ResourceDispatcherHost; |
19 | 20 |
20 namespace base { | 21 namespace base { |
21 class DictionaryValue; | 22 class DictionaryValue; |
22 } | 23 } |
23 | 24 |
24 namespace net { | 25 namespace net { |
25 class URLFetcher; | 26 class URLFetcher; |
26 } | 27 } |
27 | 28 |
28 // A WebResourceService fetches JSON data from a web server and periodically | 29 // A WebResourceService fetches JSON data from a web server and periodically |
29 // refreshes it. | 30 // refreshes it. |
30 class WebResourceService | 31 class WebResourceService |
31 : public base::RefCountedThreadSafe<WebResourceService>, | 32 : public net::URLFetcherDelegate, |
32 public net::URLFetcherDelegate { | 33 public JSONAsynchronousUnpackerDelegate, |
34 public base::RefCountedThreadSafe<WebResourceService> { | |
33 public: | 35 public: |
34 WebResourceService(PrefService* prefs, | 36 WebResourceService(PrefService* prefs, |
35 const GURL& web_resource_server, | 37 const GURL& web_resource_server, |
36 bool apply_locale_to_url_, | 38 bool apply_locale_to_url_, |
37 const char* last_update_time_pref_name, | 39 const char* last_update_time_pref_name, |
38 int start_fetch_delay_ms, | 40 int start_fetch_delay_ms, |
39 int cache_update_delay_ms); | 41 int cache_update_delay_ms); |
40 | 42 |
41 // Sleep until cache needs to be updated, but always for at least | 43 // Sleep until cache needs to be updated, but always for at least |
42 // |start_fetch_delay_ms| so we don't interfere with startup. | 44 // |start_fetch_delay_ms| so we don't interfere with startup. |
43 // Then begin updating resources. | 45 // Then begin updating resources. |
44 void StartAfterDelay(); | 46 void StartAfterDelay(); |
45 | 47 |
48 // JSONAsynchronousUnpackerDelegate methods. | |
49 virtual void OnUnpackFinished(const DictionaryValue& parsed_json) OVERRIDE; | |
50 virtual void OnUnpackError(const std::string& error_message) OVERRIDE; | |
51 | |
46 protected: | 52 protected: |
47 virtual ~WebResourceService(); | 53 virtual ~WebResourceService(); |
48 | 54 |
49 virtual void Unpack(const base::DictionaryValue& parsed_json) = 0; | |
50 | |
51 PrefService* prefs_; | 55 PrefService* prefs_; |
52 | 56 |
53 private: | 57 private: |
54 class UnpackerClient; | 58 class UnpackerClient; |
55 friend class base::RefCountedThreadSafe<WebResourceService>; | 59 friend class base::RefCountedThreadSafe<WebResourceService>; |
56 | 60 |
57 // net::URLFetcherDelegate implementation: | 61 // net::URLFetcherDelegate implementation: |
58 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 62 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
59 | 63 |
60 // Schedules a fetch after |delay_ms| milliseconds. | 64 // Schedules a fetch after |delay_ms| milliseconds. |
61 void ScheduleFetch(int64 delay_ms); | 65 void ScheduleFetch(int64 delay_ms); |
62 | 66 |
63 // Starts fetching data from the server. | 67 // Starts fetching data from the server. |
64 void StartFetch(); | 68 void StartFetch(); |
65 | 69 |
66 // Set |in_fetch_| to false, clean up temp directories (in the future). | 70 // Set |in_fetch_| to false, clean up temp directories (in the future). |
67 void EndFetch(); | 71 void EndFetch(); |
68 | 72 |
69 // So that we can delay our start so as not to affect start-up time; also, | 73 // So that we can delay our start so as not to affect start-up time; also, |
70 // so that we can schedule future cache updates. | 74 // so that we can schedule future cache updates. |
71 base::WeakPtrFactory<WebResourceService> weak_ptr_factory_; | 75 base::WeakPtrFactory<WebResourceService> weak_ptr_factory_; |
72 | 76 |
73 // The tool that fetches the url data from the server. | 77 // The tool that fetches the url data from the server. |
74 scoped_ptr<net::URLFetcher> url_fetcher_; | 78 scoped_ptr<net::URLFetcher> url_fetcher_; |
75 | 79 |
80 // The tool that parse and transform the json data. Weak reference as it | |
Miranda Callahan
2012/07/09 14:24:30
nit:s/parse/parses.
noyau (Ping after 24h)
2012/07/09 15:23:57
Done.
| |
81 // deletes itself once the unpack is done. | |
82 JSONAsynchronousUnpacker* json_unpacker_; | |
83 | |
76 // True if we are currently fetching or unpacking data. If we are asked to | 84 // True if we are currently fetching or unpacking data. If we are asked to |
77 // start a fetch when we are still fetching resource data, schedule another | 85 // start a fetch when we are still fetching resource data, schedule another |
78 // one in |cache_update_delay_ms_| time, and silently exit. | 86 // one in |cache_update_delay_ms_| time, and silently exit. |
79 bool in_fetch_; | 87 bool in_fetch_; |
80 | 88 |
81 // URL that hosts the web resource. | 89 // URL that hosts the web resource. |
82 GURL web_resource_server_; | 90 GURL web_resource_server_; |
83 | 91 |
84 // Indicates whether we should append locale to the web resource server URL. | 92 // Indicates whether we should append locale to the web resource server URL. |
85 bool apply_locale_to_url_; | 93 bool apply_locale_to_url_; |
86 | 94 |
87 // Pref name to store the last update's time. | 95 // Pref name to store the last update's time. |
88 const char* last_update_time_pref_name_; | 96 const char* last_update_time_pref_name_; |
89 | 97 |
90 // Delay on first fetch so we don't interfere with startup. | 98 // Delay on first fetch so we don't interfere with startup. |
91 int start_fetch_delay_ms_; | 99 int start_fetch_delay_ms_; |
92 | 100 |
93 // Delay between calls to update the web resource cache. This delay may be | 101 // Delay between calls to update the web resource cache. This delay may be |
94 // different for different builds of Chrome. | 102 // different for different builds of Chrome. |
95 int cache_update_delay_ms_; | 103 int cache_update_delay_ms_; |
96 | 104 |
97 DISALLOW_COPY_AND_ASSIGN(WebResourceService); | 105 DISALLOW_COPY_AND_ASSIGN(WebResourceService); |
98 }; | 106 }; |
99 | 107 |
100 #endif // CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ | 108 #endif // CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ |
OLD | NEW |