web_resource_service.h revision dc0f95d653279beabeb9817299e2902918ba123e
1// Copyright (c) 2011 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#pragma once
8
9#include <string>
10
11#include "chrome/browser/utility_process_host.h"
12#include "chrome/common/notification_type.h"
13
14class PrefService;
15class Profile;
16
17// A WebResourceService fetches data from a web resource server and store
18// locally as user preference.
19class WebResourceService
20    : public UtilityProcessHost::Client {
21 public:
22  // Pass notification_type = NOTIFICATION_TYPE_COUNT if notification is not
23  // required.
24  WebResourceService(Profile* profile,
25                     PrefService* prefs,
26                     const char* web_resource_server,
27                     bool apply_locale_to_url_,
28                     NotificationType::Type notification_type,
29                     const char* last_update_time_pref_name,
30                     int start_fetch_delay,
31                     int cache_update_delay);
32
33  // Sleep until cache needs to be updated, but always for at least 5 seconds
34  // so we don't interfere with startup.  Then begin updating resources.
35  void StartAfterDelay();
36
37  // We have successfully pulled data from a resource server; now launch
38  // the process that will parse the JSON, and then update the cache.
39  void UpdateResourceCache(const std::string& json_data);
40
41 protected:
42  virtual ~WebResourceService();
43
44  virtual void Unpack(const DictionaryValue& parsed_json) = 0;
45
46  // If delay_ms is positive, schedule notification with the delay.
47  // If delay_ms is 0, notify immediately by calling WebResourceStateChange().
48  // If delay_ms is negative, do nothing.
49  void PostNotification(int64 delay_ms);
50
51  // We need to be able to load parsed resource data into preferences file,
52  // and get proper install directory.
53  PrefService* prefs_;
54
55 private:
56  class WebResourceFetcher;
57  friend class WebResourceFetcher;
58
59  class UnpackerClient;
60
61  // Set in_fetch_ to false, clean up temp directories (in the future).
62  void EndFetch();
63
64  // Puts parsed json data in the right places, and writes to prefs file.
65  void OnWebResourceUnpacked(const DictionaryValue& parsed_json);
66
67  // Notify listeners that the state of a web resource has changed.
68  void WebResourceStateChange();
69
70  Profile* profile_;
71
72  scoped_ptr<WebResourceFetcher> web_resource_fetcher_;
73
74  ResourceDispatcherHost* resource_dispatcher_host_;
75
76  // Allows the creation of tasks to send a WEB_RESOURCE_STATE_CHANGED
77  // notification. This allows the WebResourceService to notify the New Tab
78  // Page immediately when a new web resource should be shown or removed.
79  ScopedRunnableMethodFactory<WebResourceService> service_factory_;
80
81  // True if we are currently mid-fetch.  If we are asked to start a fetch
82  // when we are still fetching resource data, schedule another one in
83  // kCacheUpdateDelay time, and silently exit.
84  bool in_fetch_;
85
86  // URL that hosts the web resource.
87  const char* web_resource_server_;
88
89  // Indicates whether we should append locale to the web resource server URL.
90  bool apply_locale_to_url_;
91
92  // Notification type when an update is done.
93  NotificationType::Type notification_type_;
94
95  // Pref name to store the last update's time.
96  const char* last_update_time_pref_name_;
97
98  // Delay on first fetch so we don't interfere with startup.
99  int start_fetch_delay_;
100
101  // Delay between calls to update the web resource cache. This delay may be
102  // different for different builds of Chrome.
103  int cache_update_delay_;
104
105  // True if a task has been set to update the cache when a new web resource
106  // becomes available.
107  bool web_resource_update_scheduled_;
108
109  DISALLOW_COPY_AND_ASSIGN(WebResourceService);
110};
111
112#endif  // CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_
113