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

Side by Side Diff: components/web_resource/web_resource_service.h

Issue 1291543004: Inject JSON parsing in WebResource as a callback rather than inheritance (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@webResources1
Patch Set: Created 5 years, 3 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
« no previous file with comments | « chrome/chrome_browser.gypi ('k') | components/web_resource/web_resource_service.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 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 COMPONENTS_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ 5 #ifndef COMPONENTS_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_
6 #define COMPONENTS_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ 6 #define COMPONENTS_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_
7 7
8 #include <string> 8 #include <string>
9 9
10 #include "base/callback_forward.h"
10 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
13 #include "components/web_resource/resource_request_allowed_notifier.h" 14 #include "components/web_resource/resource_request_allowed_notifier.h"
14 #include "net/url_request/url_fetcher_delegate.h" 15 #include "net/url_request/url_fetcher_delegate.h"
15 #include "url/gurl.h" 16 #include "url/gurl.h"
16 17
17 class PrefService; 18 class PrefService;
18 19
19 namespace base { 20 namespace base {
20 class DictionaryValue; 21 class DictionaryValue;
21 class Value; 22 class Value;
22 } 23 }
23 24
24 namespace net { 25 namespace net {
25 class URLFetcher; 26 class URLFetcher;
26 class URLRequestContextGetter; 27 class URLRequestContextGetter;
27 } 28 }
28 29
29 namespace web_resource { 30 namespace web_resource {
30 31
31 // A WebResourceService fetches JSON data from a web server and periodically 32 // A WebResourceService fetches JSON data from a web server and periodically
32 // refreshes it. 33 // refreshes it.
33 class WebResourceService 34 class WebResourceService
34 : public net::URLFetcherDelegate, 35 : public net::URLFetcherDelegate,
35 public ResourceRequestAllowedNotifier::Observer { 36 public ResourceRequestAllowedNotifier::Observer {
36 public: 37 public:
38 // Callbacks for JSON parsing.
39 using SuccessCallback = base::Callback<void(scoped_ptr<base::Value>)>;
40 using ErrorCallback = base::Callback<void(const std::string&)>;
41 using ParseJSONCallback = base::Callback<
42 void(const std::string&, const SuccessCallback&, const ErrorCallback&)>;
43
37 // Creates a new WebResourceService. 44 // Creates a new WebResourceService.
38 // If |application_locale| is not empty, it will be appended as a locale 45 // If |application_locale| is not empty, it will be appended as a locale
39 // parameter to the resource URL. 46 // parameter to the resource URL.
40 WebResourceService(PrefService* prefs, 47 WebResourceService(PrefService* prefs,
41 const GURL& web_resource_server, 48 const GURL& web_resource_server,
42 const std::string& application_locale, // May be empty 49 const std::string& application_locale, // May be empty
43 const char* last_update_time_pref_name, 50 const char* last_update_time_pref_name,
44 int start_fetch_delay_ms, 51 int start_fetch_delay_ms,
45 int cache_update_delay_ms, 52 int cache_update_delay_ms,
46 net::URLRequestContextGetter* request_context, 53 net::URLRequestContextGetter* request_context,
47 const char* disable_network_switch); 54 const char* disable_network_switch,
55 const ParseJSONCallback& parse_json_callback);
48 56
49 ~WebResourceService() override; 57 ~WebResourceService() override;
50 58
51 // Sleep until cache needs to be updated, but always for at least 59 // Sleep until cache needs to be updated, but always for at least
52 // |start_fetch_delay_ms| so we don't interfere with startup. 60 // |start_fetch_delay_ms| so we don't interfere with startup.
53 // Then begin updating resources. 61 // Then begin updating resources.
54 void StartAfterDelay(); 62 void StartAfterDelay();
55 63
56 protected: 64 protected:
57 // Callbacks for JSON parsing.
58 using SuccessCallback = base::Callback<void(scoped_ptr<base::Value>)>;
59 using ErrorCallback = base::Callback<void(const std::string&)>;
60
61 PrefService* prefs_; 65 PrefService* prefs_;
62 66
63 private: 67 private:
64 // For the subclasses to process the result of a fetch. 68 // For the subclasses to process the result of a fetch.
65 virtual void Unpack(const base::DictionaryValue& parsed_json) = 0; 69 virtual void Unpack(const base::DictionaryValue& parsed_json) = 0;
66 70
67 // Parses a JSON string |data| and invokes |success_callback| or
68 // |error_callback|.
69 virtual void ParseJSON(const std::string& data,
70 const SuccessCallback& success_callback,
71 const ErrorCallback& error_callback) = 0;
72
73 // net::URLFetcherDelegate implementation: 71 // net::URLFetcherDelegate implementation:
74 void OnURLFetchComplete(const net::URLFetcher* source) override; 72 void OnURLFetchComplete(const net::URLFetcher* source) override;
75 73
76 // Schedules a fetch after |delay_ms| milliseconds. 74 // Schedules a fetch after |delay_ms| milliseconds.
77 void ScheduleFetch(int64 delay_ms); 75 void ScheduleFetch(int64 delay_ms);
78 76
79 // Starts fetching data from the server. 77 // Starts fetching data from the server.
80 void StartFetch(); 78 void StartFetch();
81 79
82 // Set |in_fetch_| to false, clean up temp directories (in the future). 80 // Set |in_fetch_| to false, clean up temp directories (in the future).
(...skipping 30 matching lines...) Expand all
113 // Delay on first fetch so we don't interfere with startup. 111 // Delay on first fetch so we don't interfere with startup.
114 int start_fetch_delay_ms_; 112 int start_fetch_delay_ms_;
115 113
116 // Delay between calls to update the web resource cache. This delay may be 114 // Delay between calls to update the web resource cache. This delay may be
117 // different for different builds of Chrome. 115 // different for different builds of Chrome.
118 int cache_update_delay_ms_; 116 int cache_update_delay_ms_;
119 117
120 // The request context for the resource fetch. 118 // The request context for the resource fetch.
121 scoped_refptr<net::URLRequestContextGetter> request_context_; 119 scoped_refptr<net::URLRequestContextGetter> request_context_;
122 120
121 // Callback used to parse JSON.
122 ParseJSONCallback parse_json_callback_;
123
123 // So that we can delay our start so as not to affect start-up time; also, 124 // So that we can delay our start so as not to affect start-up time; also,
124 // so that we can schedule future cache updates. 125 // so that we can schedule future cache updates.
125 base::WeakPtrFactory<WebResourceService> weak_ptr_factory_; 126 base::WeakPtrFactory<WebResourceService> weak_ptr_factory_;
126 127
127 DISALLOW_COPY_AND_ASSIGN(WebResourceService); 128 DISALLOW_COPY_AND_ASSIGN(WebResourceService);
128 }; 129 };
129 130
130 } // namespace web_resource 131 } // namespace web_resource
131 132
132 #endif // COMPONENTS_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_ 133 #endif // COMPONENTS_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_
OLDNEW
« no previous file with comments | « chrome/chrome_browser.gypi ('k') | components/web_resource/web_resource_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698