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