Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / components / web_resource / promo_resource_service.h
blobd36e07dd318a86ed1da2ebc39ad186eae5cf394d
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 COMPONENTS_WEB_RESOURCE_PROMO_RESOURCE_SERVICE_H_
6 #define COMPONENTS_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 PrefRegistrySimple;
16 class PrefService;
18 namespace base {
19 class DictionaryValue;
22 namespace user_prefs {
23 class PrefRegistrySyncable;
26 namespace version_info {
27 enum class Channel;
30 namespace web_resource {
32 class NotificationPromo;
34 // A PromoResourceService fetches data from a web resource server to be used to
35 // dynamically change the appearance of the New Tab Page. For example, it has
36 // been used to fetch "tips" to be displayed on the NTP, or to display
37 // promotional messages to certain groups of Chrome users.
38 class PromoResourceService : public WebResourceService {
39 public:
40 using StateChangedCallbackList = base::CallbackList<void()>;
41 using StateChangedSubscription = StateChangedCallbackList::Subscription;
43 static void RegisterPrefs(PrefRegistrySimple* registry);
44 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
45 static void MigrateUserPrefs(PrefService* user_prefs);
47 PromoResourceService(PrefService* local_state,
48 version_info::Channel channel,
49 const std::string& application_locale,
50 net::URLRequestContextGetter* request_context,
51 const char* disable_network_switch,
52 const ParseJSONCallback& parse_json_callback);
53 ~PromoResourceService() override;
55 // Registers a callback called when the state of a web resource has been
56 // changed. A resource may have been added, removed, or altered.
57 scoped_ptr<StateChangedSubscription> RegisterStateChangedCallback(
58 const base::Closure& closure);
60 private:
61 // Schedule a notification that a web resource is either going to become
62 // available or be no longer valid.
63 void ScheduleNotification(const NotificationPromo& notification_promo);
65 // Schedules the initial notification for when the web resource is going
66 // to become available or no longer valid. This performs a few additional
67 // checks than ScheduleNotification, namely it schedules updates immediately
68 // if the promo service or Chrome locale has changed.
69 void ScheduleNotificationOnInit();
71 // If delay_ms is positive, schedule notification with the delay.
72 // If delay_ms is 0, notify immediately by calling WebResourceStateChange().
73 // If delay_ms is negative, do nothing.
74 void PostNotification(int64 delay_ms);
76 // Notify listeners that the state of a web resource has changed.
77 void PromoResourceStateChange();
79 // WebResourceService override to process the parsed information.
80 void Unpack(const base::DictionaryValue& parsed_json) override;
82 // List of callbacks called when the state of a web resource has changed.
83 StateChangedCallbackList callback_list_;
85 // Allows the creation of tasks to send a notification.
86 // This allows the PromoResourceService to notify the New Tab Page immediately
87 // when a new web resource should be shown or removed.
88 base::WeakPtrFactory<PromoResourceService> weak_ptr_factory_;
90 DISALLOW_COPY_AND_ASSIGN(PromoResourceService);
93 } // namespace web_resource
95 #endif // COMPONENTS_WEB_RESOURCE_PROMO_RESOURCE_SERVICE_H_