Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(187)

Side by Side Diff: chrome/browser/web_resource/web_resource_service.h

Issue 10690096: Extracted inner class doing process dispatch. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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; 55 virtual void Unpack(const base::DictionaryValue& parsed_json) = 0;
Miranda Callahan 2012/07/09 15:28:11 Great solution. Could you just add a comment here
noyau (Ping after 24h) 2012/07/09 15:41:42 Done.
50 56
51 PrefService* prefs_; 57 PrefService* prefs_;
52 58
53 private: 59 private:
54 class UnpackerClient; 60 class UnpackerClient;
55 friend class base::RefCountedThreadSafe<WebResourceService>; 61 friend class base::RefCountedThreadSafe<WebResourceService>;
56 62
57 // net::URLFetcherDelegate implementation: 63 // net::URLFetcherDelegate implementation:
58 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; 64 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
59 65
60 // Schedules a fetch after |delay_ms| milliseconds. 66 // Schedules a fetch after |delay_ms| milliseconds.
61 void ScheduleFetch(int64 delay_ms); 67 void ScheduleFetch(int64 delay_ms);
62 68
63 // Starts fetching data from the server. 69 // Starts fetching data from the server.
64 void StartFetch(); 70 void StartFetch();
65 71
66 // Set |in_fetch_| to false, clean up temp directories (in the future). 72 // Set |in_fetch_| to false, clean up temp directories (in the future).
67 void EndFetch(); 73 void EndFetch();
68 74
69 // So that we can delay our start so as not to affect start-up time; also, 75 // 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. 76 // so that we can schedule future cache updates.
71 base::WeakPtrFactory<WebResourceService> weak_ptr_factory_; 77 base::WeakPtrFactory<WebResourceService> weak_ptr_factory_;
72 78
73 // The tool that fetches the url data from the server. 79 // The tool that fetches the url data from the server.
74 scoped_ptr<net::URLFetcher> url_fetcher_; 80 scoped_ptr<net::URLFetcher> url_fetcher_;
75 81
82 // The tool that parses and transform the json data. Weak reference as it
83 // deletes itself once the unpack is done.
84 JSONAsynchronousUnpacker* json_unpacker_;
85
76 // True if we are currently fetching or unpacking data. If we are asked to 86 // 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 87 // start a fetch when we are still fetching resource data, schedule another
78 // one in |cache_update_delay_ms_| time, and silently exit. 88 // one in |cache_update_delay_ms_| time, and silently exit.
79 bool in_fetch_; 89 bool in_fetch_;
80 90
81 // URL that hosts the web resource. 91 // URL that hosts the web resource.
82 GURL web_resource_server_; 92 GURL web_resource_server_;
83 93
84 // Indicates whether we should append locale to the web resource server URL. 94 // Indicates whether we should append locale to the web resource server URL.
85 bool apply_locale_to_url_; 95 bool apply_locale_to_url_;
86 96
87 // Pref name to store the last update's time. 97 // Pref name to store the last update's time.
88 const char* last_update_time_pref_name_; 98 const char* last_update_time_pref_name_;
89 99
90 // Delay on first fetch so we don't interfere with startup. 100 // Delay on first fetch so we don't interfere with startup.
91 int start_fetch_delay_ms_; 101 int start_fetch_delay_ms_;
92 102
93 // Delay between calls to update the web resource cache. This delay may be 103 // Delay between calls to update the web resource cache. This delay may be
94 // different for different builds of Chrome. 104 // different for different builds of Chrome.
95 int cache_update_delay_ms_; 105 int cache_update_delay_ms_;
96 106
97 DISALLOW_COPY_AND_ASSIGN(WebResourceService); 107 DISALLOW_COPY_AND_ASSIGN(WebResourceService);
98 }; 108 };
99 109
100 #endif // CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ 110 #endif // CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_
OLDNEW
« no previous file with comments | « chrome/browser/web_resource/promo_resource_service.h ('k') | chrome/browser/web_resource/web_resource_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698