| 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 |
| 55 // For the subclasses to process the result of a fetch. |
| 49 virtual void Unpack(const base::DictionaryValue& parsed_json) = 0; | 56 virtual void Unpack(const base::DictionaryValue& parsed_json) = 0; |
| 50 | 57 |
| 51 PrefService* prefs_; | 58 PrefService* prefs_; |
| 52 | 59 |
| 53 private: | 60 private: |
| 54 class UnpackerClient; | 61 class UnpackerClient; |
| 55 friend class base::RefCountedThreadSafe<WebResourceService>; | 62 friend class base::RefCountedThreadSafe<WebResourceService>; |
| 56 | 63 |
| 57 // net::URLFetcherDelegate implementation: | 64 // net::URLFetcherDelegate implementation: |
| 58 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 65 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 59 | 66 |
| 60 // Schedules a fetch after |delay_ms| milliseconds. | 67 // Schedules a fetch after |delay_ms| milliseconds. |
| 61 void ScheduleFetch(int64 delay_ms); | 68 void ScheduleFetch(int64 delay_ms); |
| 62 | 69 |
| 63 // Starts fetching data from the server. | 70 // Starts fetching data from the server. |
| 64 void StartFetch(); | 71 void StartFetch(); |
| 65 | 72 |
| 66 // Set |in_fetch_| to false, clean up temp directories (in the future). | 73 // Set |in_fetch_| to false, clean up temp directories (in the future). |
| 67 void EndFetch(); | 74 void EndFetch(); |
| 68 | 75 |
| 69 // So that we can delay our start so as not to affect start-up time; also, | 76 // 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. | 77 // so that we can schedule future cache updates. |
| 71 base::WeakPtrFactory<WebResourceService> weak_ptr_factory_; | 78 base::WeakPtrFactory<WebResourceService> weak_ptr_factory_; |
| 72 | 79 |
| 73 // The tool that fetches the url data from the server. | 80 // The tool that fetches the url data from the server. |
| 74 scoped_ptr<net::URLFetcher> url_fetcher_; | 81 scoped_ptr<net::URLFetcher> url_fetcher_; |
| 75 | 82 |
| 83 // The tool that parses and transform the json data. Weak reference as it |
| 84 // deletes itself once the unpack is done. |
| 85 JSONAsynchronousUnpacker* json_unpacker_; |
| 86 |
| 76 // True if we are currently fetching or unpacking data. If we are asked to | 87 // 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 | 88 // start a fetch when we are still fetching resource data, schedule another |
| 78 // one in |cache_update_delay_ms_| time, and silently exit. | 89 // one in |cache_update_delay_ms_| time, and silently exit. |
| 79 bool in_fetch_; | 90 bool in_fetch_; |
| 80 | 91 |
| 81 // URL that hosts the web resource. | 92 // URL that hosts the web resource. |
| 82 GURL web_resource_server_; | 93 GURL web_resource_server_; |
| 83 | 94 |
| 84 // Indicates whether we should append locale to the web resource server URL. | 95 // Indicates whether we should append locale to the web resource server URL. |
| 85 bool apply_locale_to_url_; | 96 bool apply_locale_to_url_; |
| 86 | 97 |
| 87 // Pref name to store the last update's time. | 98 // Pref name to store the last update's time. |
| 88 const char* last_update_time_pref_name_; | 99 const char* last_update_time_pref_name_; |
| 89 | 100 |
| 90 // Delay on first fetch so we don't interfere with startup. | 101 // Delay on first fetch so we don't interfere with startup. |
| 91 int start_fetch_delay_ms_; | 102 int start_fetch_delay_ms_; |
| 92 | 103 |
| 93 // Delay between calls to update the web resource cache. This delay may be | 104 // Delay between calls to update the web resource cache. This delay may be |
| 94 // different for different builds of Chrome. | 105 // different for different builds of Chrome. |
| 95 int cache_update_delay_ms_; | 106 int cache_update_delay_ms_; |
| 96 | 107 |
| 97 DISALLOW_COPY_AND_ASSIGN(WebResourceService); | 108 DISALLOW_COPY_AND_ASSIGN(WebResourceService); |
| 98 }; | 109 }; |
| 99 | 110 |
| 100 #endif // CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ | 111 #endif // CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ |
| OLD | NEW |