summaryrefslogtreecommitdiffstats
path: root/components/ntp_snippets/ntp_snippets_service.h
blob: 76cb3d3be853bca2f9e274d841878437ec683a3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_
#define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_

#include <stddef.h>

#include <string>
#include <vector>

#include "base/macros.h"
#include "base/observer_list.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/ntp_snippets/inner_iterator.h"
#include "components/ntp_snippets/ntp_snippet.h"

namespace ntp_snippets {

class NTPSnippetsServiceObserver;

// Stores and vend fresh content data for the NTP.
class NTPSnippetsService : public KeyedService {
 public:
  using NTPSnippetStorage = std::vector<std::unique_ptr<NTPSnippet>>;
  using const_iterator =
      InnerIterator<NTPSnippetStorage::const_iterator, const NTPSnippet>;

  // |application_language_code| should be a ISO 639-1 compliant string. Aka
  // 'en' or 'en-US'. Note that this code should only specify the language, not
  // the locale, so 'en_US' (english language with US locale) and 'en-GB_US'
  // (British english person in the US) are not language code.
  explicit NTPSnippetsService(const std::string& application_language_code);
  ~NTPSnippetsService() override;

  // Inherited from KeyedService.
  void Shutdown() override;

  // True once the data is loaded in memory and available to loop over.
  bool is_loaded() { return loaded_; }

  // Observer accessors.
  void AddObserver(NTPSnippetsServiceObserver* observer);
  void RemoveObserver(NTPSnippetsServiceObserver* observer);

  // Expects the JSON to be a list of dictionaries with keys matching the
  // properties of a snippet (url, title, site_title, etc...). The url is the
  // only mandatory value.
  bool LoadFromJSONString(const std::string& str);

  // Number of snippets available. Can only be called when is_loaded() is true.
  NTPSnippetStorage::size_type size() {
    DCHECK(loaded_);
    return snippets_.size();
  }

  // The snippets can be iterated upon only via a const_iterator. Recommended
  // way to iterate is as follow:
  //
  // NTPSnippetsService service; // Assume is set.
  //  for (auto& snippet : *service) {
  //    // Snippet here is a const object.
  //  }
  const_iterator begin() {
    DCHECK(loaded_);
    return const_iterator(snippets_.begin());
  }
  const_iterator end() {
    DCHECK(loaded_);
    return const_iterator(snippets_.end());
  }

 private:
  // True if the suggestions are loaded.
  bool loaded_;
  // All the suggestions.
  NTPSnippetStorage snippets_;
  // The ISO 639-1 code of the language used by the application.
  const std::string application_language_code_;
  // The observers.
  base::ObserverList<NTPSnippetsServiceObserver> observers_;

  DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService);
};

class NTPSnippetsServiceObserver {
 public:
  // Send everytime the service loads a new set of data.
  virtual void NTPSnippetsServiceLoaded(NTPSnippetsService* service) = 0;
  // Send when the service is shutting down.
  virtual void NTPSnippetsServiceShutdown(NTPSnippetsService* service) = 0;

 protected:
  virtual ~NTPSnippetsServiceObserver() {}
};

}  // namespace ntp_snippets

#endif  // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_