1 // Copyright (c) 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. 4 5 #ifndef CHROME_BROWSER_UI_WEBUI_NTP_SUGGESTIONS_COMBINER_H_ 6 #define CHROME_BROWSER_UI_WEBUI_NTP_SUGGESTIONS_COMBINER_H_ 7 8 #include <vector> 9 10 #include "base/basictypes.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_vector.h" 13 14 class GURL; 15 class SuggestionsHandler; 16 class SuggestionsSource; 17 class Profile; 18 19 namespace base { 20 class DictionaryValue; 21 class ListValue; 22 } 23 24 // Combines many different sources of suggestions and generates data from it. 25 class SuggestionsCombiner { 26 public: 27 // Interface to be implemented by classes that will be notified of events from 28 // the SuggestionsCombiner. 29 class Delegate { 30 public: 31 virtual ~Delegate() {} 32 33 // Method that is called when new suggestions are ready from the 34 // SuggestionsCombiner. 35 virtual void OnSuggestionsReady() = 0; 36 }; 37 38 virtual ~SuggestionsCombiner(); 39 40 explicit SuggestionsCombiner(SuggestionsCombiner::Delegate* delegate, 41 Profile* profile); 42 43 // Add a new source. The SuggestionsCombiner takes ownership of |source|. 44 void AddSource(SuggestionsSource* source); 45 46 // Enables or disables debug mode. If debug mode is enabled, the sources are 47 // expected to provide additional data, which could be displayed, for example, 48 // in the chrome://suggestions-internals/ page. 49 void EnableDebug(bool enable); 50 51 // Fetch a new set of items from the various suggestion sources. 52 void FetchItems(Profile* profile); 53 54 base::ListValue* GetPageValues(); 55 56 // Called by a source when its items are ready. Make sure suggestion sources 57 // call this method exactly once for each call to 58 // SuggestionsSource::FetchItems. 59 void OnItemsReady(); 60 61 void SetSuggestionsCount(size_t suggestions_count); 62 63 // Creates a new instance of the SuggestionsCombiner (owned by the callee), 64 // and sets up the default sources. 65 static SuggestionsCombiner* Create(SuggestionsCombiner::Delegate* delegate, 66 Profile* profile); 67 68 private: 69 friend class SuggestionsCombinerTest; 70 71 // Fill the page values from the suggestion sources so they can be sent to 72 // the JavaScript side. This should only be called when all the suggestion 73 // sources have items ready. 74 void FillPageValues(); 75 76 // Add extra information to page values that should be common across all 77 // suggestion sources. 78 void AddExtendedInformation(base::DictionaryValue* page_value); 79 80 // Checks if a URL is already open for the current profile. URLs open in an 81 // incognito window are not reported. 82 bool IsUrlAlreadyOpen(const GURL& url); 83 84 typedef ScopedVector<SuggestionsSource> SuggestionsSources; 85 86 // List of all the suggestions sources that will be combined to generate a 87 // single list of suggestions. 88 SuggestionsSources sources_; 89 90 // Counter tracking the number of sources that are currently asynchronously 91 // fetching their data. 92 int sources_fetching_count_; 93 94 // The delegate to notify once items are ready. 95 SuggestionsCombiner::Delegate* delegate_; 96 97 // Number of suggestions to generate. Used to distribute the suggestions 98 // between the various sources. 99 size_t suggestions_count_; 100 101 // Informations to send to the javascript side. 102 scoped_ptr<base::ListValue> page_values_; 103 104 // Whether debug mode is enabled or not (debug mode provides more data in the 105 // results). 106 bool debug_enabled_; 107 108 Profile* profile_; 109 110 DISALLOW_COPY_AND_ASSIGN(SuggestionsCombiner); 111 }; 112 113 #endif // CHROME_BROWSER_UI_WEBUI_NTP_SUGGESTIONS_COMBINER_H_ 114