ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / web_resource / promo_resource_service.h
blob2908286634c3c596edb9bcb8d715dadb98bbf390
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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_WEB_RESOURCE_PROMO_RESOURCE_SERVICE_H_
6 #define CHROME_BROWSER_WEB_RESOURCE_PROMO_RESOURCE_SERVICE_H_
8 #include <string>
10 #include "base/callback_forward.h"
11 #include "base/callback_list.h"
12 #include "base/memory/weak_ptr.h"
13 #include "components/web_resource/web_resource_service.h"
15 class NotificationPromo;
16 class PrefRegistrySimple;
17 class PrefService;
19 namespace base {
20 class DictionaryValue;
23 namespace user_prefs {
24 class PrefRegistrySyncable;
27 namespace version_info {
28 enum class Channel;
31 // A PromoResourceService fetches data from a web resource server to be used to
32 // dynamically change the appearance of the New Tab Page. For example, it has
33 // been used to fetch "tips" to be displayed on the NTP, or to display
34 // promotional messages to certain groups of Chrome users.
35 class PromoResourceService : public web_resource::WebResourceService {
36 public:
37 using StateChangedCallbackList = base::CallbackList<void()>;
38 using StateChangedSubscription = StateChangedCallbackList::Subscription;
40 static void RegisterPrefs(PrefRegistrySimple* registry);
41 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
42 static void MigrateUserPrefs(PrefService* user_prefs);
44 PromoResourceService(PrefService* local_state,
45 version_info::Channel channel,
46 const std::string& application_locale,
47 net::URLRequestContextGetter* request_context,
48 const char* disable_network_switch,
49 const ParseJSONCallback& parse_json_callback);
50 ~PromoResourceService() override;
52 // Registers a callback called when the state of a web resource has been
53 // changed. A resource may have been added, removed, or altered.
54 scoped_ptr<StateChangedSubscription> RegisterStateChangedCallback(
55 const base::Closure& closure);
57 private:
58 // Schedule a notification that a web resource is either going to become
59 // available or be no longer valid.
60 void ScheduleNotification(const NotificationPromo& notification_promo);
62 // Schedules the initial notification for when the web resource is going
63 // to become available or no longer valid. This performs a few additional
64 // checks than ScheduleNotification, namely it schedules updates immediately
65 // if the promo service or Chrome locale has changed.
66 void ScheduleNotificationOnInit();
68 // If delay_ms is positive, schedule notification with the delay.
69 // If delay_ms is 0, notify immediately by calling WebResourceStateChange().
70 // If delay_ms is negative, do nothing.
71 void PostNotification(int64 delay_ms);
73 // Notify listeners that the state of a web resource has changed.
74 void PromoResourceStateChange();
76 // WebResourceService override to process the parsed information.
77 void Unpack(const base::DictionaryValue& parsed_json) override;
79 // List of callbacks called when the state of a web resource has changed.
80 StateChangedCallbackList callback_list_;
82 // Allows the creation of tasks to send a notification.
83 // This allows the PromoResourceService to notify the New Tab Page immediately
84 // when a new web resource should be shown or removed.
85 base::WeakPtrFactory<PromoResourceService> weak_ptr_factory_;
87 DISALLOW_COPY_AND_ASSIGN(PromoResourceService);
90 #endif // CHROME_BROWSER_WEB_RESOURCE_PROMO_RESOURCE_SERVICE_H_