blob: ca5041a06227f22a6a725f7ecbe2c776b046b858 (
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
101
102
103
104
105
106
107
108
|
// Copyright (c) 2012 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 CHROME_BROWSER_UI_WEBUI_NTP_SUGGESTIONS_COMBINER_H_
#define CHROME_BROWSER_UI_WEBUI_NTP_SUGGESTIONS_COMBINER_H_
#include <vector>
#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
class GURL;
class SuggestionsHandler;
class SuggestionsSource;
class Profile;
namespace base {
class DictionaryValue;
class ListValue;
}
// Combines many different sources of suggestions and generates data from it.
class SuggestionsCombiner {
public:
// Interface to be implemented by classes that will be notified of events from
// the SuggestionsCombiner.
class Delegate {
public:
virtual ~Delegate() {}
// Method that is called when new suggestions are ready from the
// SuggestionsCombiner.
virtual void OnSuggestionsReady() = 0;
};
virtual ~SuggestionsCombiner();
explicit SuggestionsCombiner(SuggestionsCombiner::Delegate* delegate,
Profile* profile);
// Add a new source. The SuggestionsCombiner takes ownership of |source|.
void AddSource(SuggestionsSource* source);
// Enables or disables debug mode. If debug mode is enabled, the sources are
// expected to provide additional data, which could be displayed, for example,
// in the chrome://suggestions-internals/ page.
void EnableDebug(bool enable);
// Fetch a new set of items from the various suggestion sources.
void FetchItems(Profile* profile);
base::ListValue* GetPageValues();
// Called by a source when its items are ready. Make sure suggestion sources
// call this method exactly once for each call to
// SuggestionsSource::FetchItems.
void OnItemsReady();
void SetSuggestionsCount(size_t suggestions_count);
private:
friend class SuggestionsCombinerTest;
// Fill the page values from the suggestion sources so they can be sent to
// the JavaScript side. This should only be called when all the suggestion
// sources have items ready.
void FillPageValues();
// Add extra information to page values that should be common across all
// suggestion sources.
void AddExtendedInformation(base::DictionaryValue* page_value);
// Checks if a URL is already open for the current profile. URLs open in an
// incognito window are not reported.
bool IsUrlAlreadyOpen(const GURL& url);
typedef ScopedVector<SuggestionsSource> SuggestionsSources;
// List of all the suggestions sources that will be combined to generate a
// single list of suggestions.
SuggestionsSources sources_;
// Counter tracking the number of sources that are currently asynchronously
// fetching their data.
int sources_fetching_count_;
// The delegate to notify once items are ready.
SuggestionsCombiner::Delegate* delegate_;
// Number of suggestions to generate. Used to distribute the suggestions
// between the various sources.
size_t suggestions_count_;
// Informations to send to the javascript side.
scoped_ptr<base::ListValue> page_values_;
// Whether debug mode is enabled or not (debug mode provides more data in the
// results).
bool debug_enabled_;
Profile* profile_;
DISALLOW_COPY_AND_ASSIGN(SuggestionsCombiner);
};
#endif // CHROME_BROWSER_UI_WEBUI_NTP_SUGGESTIONS_COMBINER_H_
|