Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/ntp_snippets/ntp_snippets_service.h" | 5 #include "components/ntp_snippets/ntp_snippets_service.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "base/task_runner_util.h" | 12 #include "base/task_runner_util.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "components/ntp_snippets/pref_names.h" | |
| 15 #include "components/pref_registry/pref_registry_syncable.h" | |
| 16 #include "components/prefs/pref_service.h" | |
| 14 | 17 |
| 15 namespace { | 18 namespace { |
| 16 | 19 |
| 17 // TODO(crbug.com/587857): This is an extremely small value, for development. | 20 // TODO(crbug.com/587857): This is an extremely small value, for development. |
| 18 // Replace it by something sensible and add a command line param to control it. | 21 // Replace it by something sensible and add a command line param to control it. |
| 19 const int kDefaultFetchingIntervalSeconds = 60; | 22 const int kDefaultFetchingIntervalSeconds = 60; |
| 20 | 23 |
| 21 bool ReadFileToString(const base::FilePath& path, std::string* data) { | 24 bool ReadFileToString(const base::FilePath& path, std::string* data) { |
| 22 DCHECK(data); | 25 DCHECK(data); |
| 23 bool success = base::ReadFileToString(path, data); | 26 bool success = base::ReadFileToString(path, data); |
| 24 DLOG_IF(ERROR, !success) << "Error reading file " << path.LossyDisplayName(); | 27 DLOG_IF(ERROR, !success) << "Error reading file " << path.LossyDisplayName(); |
| 25 return success; | 28 return success; |
| 26 } | 29 } |
| 27 | 30 |
| 28 } // namespace | 31 } // namespace |
| 29 | 32 |
| 30 namespace ntp_snippets { | 33 namespace ntp_snippets { |
| 31 | 34 |
| 32 NTPSnippetsService::NTPSnippetsService( | 35 NTPSnippetsService::NTPSnippetsService( |
| 36 PrefService* pref_service, | |
| 33 scoped_refptr<base::SequencedTaskRunner> file_task_runner, | 37 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
| 34 const std::string& application_language_code, | 38 const std::string& application_language_code, |
| 35 NTPSnippetsScheduler* scheduler, | 39 NTPSnippetsScheduler* scheduler, |
| 36 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher) | 40 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher) |
| 37 : loaded_(false), | 41 : pref_service_(pref_service), |
| 42 loaded_(false), | |
| 38 file_task_runner_(file_task_runner), | 43 file_task_runner_(file_task_runner), |
| 39 application_language_code_(application_language_code), | 44 application_language_code_(application_language_code), |
| 40 scheduler_(scheduler), | 45 scheduler_(scheduler), |
| 41 snippets_fetcher_(std::move(snippets_fetcher)), | 46 snippets_fetcher_(std::move(snippets_fetcher)), |
| 42 weak_ptr_factory_(this) { | 47 weak_ptr_factory_(this) { |
| 43 snippets_fetcher_callback_ = snippets_fetcher_->AddCallback( | 48 snippets_fetcher_callback_ = snippets_fetcher_->AddCallback( |
| 44 base::Bind(&NTPSnippetsService::OnSnippetsDownloaded, | 49 base::Bind(&NTPSnippetsService::OnSnippetsDownloaded, |
| 45 weak_ptr_factory_.GetWeakPtr())); | 50 weak_ptr_factory_.GetWeakPtr())); |
| 46 } | 51 } |
| 47 | 52 |
| 48 NTPSnippetsService::~NTPSnippetsService() {} | 53 NTPSnippetsService::~NTPSnippetsService() {} |
| 49 | 54 |
| 55 // static | |
| 56 void NTPSnippetsService::RegisterProfilePrefs( | |
| 57 user_prefs::PrefRegistrySyncable* registry) { | |
| 58 registry->RegisterListPref(prefs::kSnippets); | |
| 59 } | |
| 60 | |
| 50 void NTPSnippetsService::Init(bool enabled) { | 61 void NTPSnippetsService::Init(bool enabled) { |
| 51 // If enabled, get snippets immediately. If we've downloaded them before, | 62 // If enabled, get any existing snippets immediately from prefs. |
| 52 // this will just read from disk. | 63 if (enabled) { |
| 53 if (enabled) | 64 LoadFromPrefs(); |
| 54 FetchSnippets(false); | 65 |
| 66 // If we don't have any snippets yet, start a fetch. | |
| 67 if (snippets_.empty()) | |
| 68 FetchSnippets(false); | |
| 69 } | |
| 55 | 70 |
| 56 // The scheduler only exists on Android so far, it's null on other platforms. | 71 // The scheduler only exists on Android so far, it's null on other platforms. |
| 57 if (!scheduler_) | 72 if (!scheduler_) |
| 58 return; | 73 return; |
| 59 | 74 |
| 60 if (enabled) | 75 if (enabled) |
| 61 scheduler_->Schedule(kDefaultFetchingIntervalSeconds); | 76 scheduler_->Schedule(kDefaultFetchingIntervalSeconds); |
| 62 else | 77 else |
| 63 scheduler_->Unschedule(); | 78 scheduler_->Unschedule(); |
| 64 } | 79 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 76 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { | 91 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { |
| 77 observers_.AddObserver(observer); | 92 observers_.AddObserver(observer); |
| 78 if (loaded_) | 93 if (loaded_) |
| 79 observer->NTPSnippetsServiceLoaded(this); | 94 observer->NTPSnippetsServiceLoaded(this); |
| 80 } | 95 } |
| 81 | 96 |
| 82 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { | 97 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { |
| 83 observers_.RemoveObserver(observer); | 98 observers_.RemoveObserver(observer); |
| 84 } | 99 } |
| 85 | 100 |
| 101 void NTPSnippetsService::OnSnippetsDownloaded( | |
| 102 const base::FilePath& download_path) { | |
| 103 std::string* downloaded_data = new std::string; | |
| 104 base::PostTaskAndReplyWithResult( | |
| 105 file_task_runner_.get(), FROM_HERE, | |
| 106 base::Bind(&ReadFileToString, download_path, downloaded_data), | |
| 107 base::Bind(&NTPSnippetsService::OnFileReadDone, | |
| 108 weak_ptr_factory_.GetWeakPtr(), base::Owned(downloaded_data))); | |
| 109 } | |
| 110 | |
| 86 void NTPSnippetsService::OnFileReadDone(const std::string* json, bool success) { | 111 void NTPSnippetsService::OnFileReadDone(const std::string* json, bool success) { |
| 87 if (!success) | 112 if (!success) |
| 88 return; | 113 return; |
| 89 | 114 |
| 90 DCHECK(json); | 115 DCHECK(json); |
| 91 LoadFromJSONString(*json); | 116 LoadFromJSONString(*json); |
| 117 StoreToPrefs(); | |
| 92 } | 118 } |
| 93 | 119 |
| 94 bool NTPSnippetsService::LoadFromJSONString(const std::string& str) { | 120 bool NTPSnippetsService::LoadFromJSONString(const std::string& str) { |
| 95 snippets_.clear(); | 121 scoped_ptr<base::Value> deserialized = base::JSONReader::Read(str); |
| 96 | |
| 97 JSONStringValueDeserializer deserializer(str); | |
| 98 int error_code; | |
| 99 std::string error_message; | |
| 100 | |
| 101 scoped_ptr<base::Value> deserialized = | |
| 102 deserializer.Deserialize(&error_code, &error_message); | |
| 103 if (!deserialized) | 122 if (!deserialized) |
| 104 return false; | 123 return false; |
| 105 | 124 |
| 106 const base::DictionaryValue* top_dict = NULL; | 125 const base::DictionaryValue* top_dict = nullptr; |
| 107 if (!deserialized->GetAsDictionary(&top_dict)) | 126 if (!deserialized->GetAsDictionary(&top_dict)) |
| 108 return false; | 127 return false; |
| 109 | 128 |
| 110 const base::ListValue* list = NULL; | 129 const base::ListValue* list = nullptr; |
| 111 if (!top_dict->GetList("recos", &list)) | 130 if (!top_dict->GetList("recos", &list)) |
| 112 return false; | 131 return false; |
| 113 | 132 |
| 114 for (base::Value* const value : *list) { | 133 return LoadFromJSONList(*list); |
| 115 const base::DictionaryValue* dict = NULL; | 134 } |
| 135 | |
| 136 bool NTPSnippetsService::LoadFromJSONList(const base::ListValue& list) { | |
| 137 for (const base::Value* const value : list) { | |
| 138 const base::DictionaryValue* dict = nullptr; | |
| 116 if (!value->GetAsDictionary(&dict)) | 139 if (!value->GetAsDictionary(&dict)) |
| 117 return false; | 140 return false; |
| 118 | 141 |
| 119 const base::DictionaryValue* content = NULL; | 142 const base::DictionaryValue* content = nullptr; |
| 120 if (!dict->GetDictionary("contentInfo", &content)) | 143 if (!dict->GetDictionary("contentInfo", &content)) |
| 121 return false; | 144 return false; |
| 122 scoped_ptr<NTPSnippet> snippet = | 145 scoped_ptr<NTPSnippet> snippet = NTPSnippet::CreateFromDictionary(*content); |
| 123 NTPSnippet::NTPSnippetFromDictionary(*content); | |
| 124 if (!snippet) | 146 if (!snippet) |
| 125 return false; | 147 return false; |
| 126 snippets_.push_back(std::move(snippet)); | 148 // Check if we already have a snippet with the same URL. If so, replace it |
| 149 // rather than adding a duplicate. | |
| 150 const GURL& url = snippet->url(); | |
| 151 auto it = std::find_if(snippets_.begin(), snippets_.end(), | |
| 152 [&url](const scoped_ptr<NTPSnippet>& old_snippet) { | |
| 153 return old_snippet->url() == url; | |
| 154 }); | |
| 155 if (it != snippets_.end()) | |
| 156 *it = std::move(snippet); | |
| 157 else | |
| 158 snippets_.push_back(std::move(snippet)); | |
| 127 } | 159 } |
| 128 loaded_ = true; | 160 loaded_ = true; |
| 129 | 161 |
| 130 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 162 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
| 131 NTPSnippetsServiceLoaded(this)); | 163 NTPSnippetsServiceLoaded(this)); |
| 132 return true; | 164 return true; |
| 133 } | 165 } |
| 134 | 166 |
| 135 void NTPSnippetsService::OnSnippetsDownloaded( | 167 void NTPSnippetsService::LoadFromPrefs() { |
| 136 const base::FilePath& download_path) { | 168 // |pref_service_| can be null in tests. |
| 137 std::string* downloaded_data = new std::string; | 169 if (!pref_service_) |
| 138 base::PostTaskAndReplyWithResult( | 170 return; |
| 139 file_task_runner_.get(), FROM_HERE, | 171 if (!LoadFromJSONList(*pref_service_->GetList(prefs::kSnippets))) |
| 140 base::Bind(&ReadFileToString, download_path, downloaded_data), | 172 LOG(ERROR) << "Failed to parse " << prefs::kSnippets << " pref"; |
|
Bernhard Bauer
2016/02/26 17:30:48
Nit: printing the value of the pref key is not goi
Marc Treib
2016/02/29 10:04:42
Done.
| |
| 141 base::Bind(&NTPSnippetsService::OnFileReadDone, | 173 } |
| 142 weak_ptr_factory_.GetWeakPtr(), base::Owned(downloaded_data))); | 174 |
| 175 void NTPSnippetsService::StoreToPrefs() { | |
| 176 // |pref_service_| can be null in tests. | |
| 177 if (!pref_service_) | |
| 178 return; | |
| 179 base::ListValue list; | |
| 180 for (const auto& snippet : snippets_) { | |
| 181 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); | |
| 182 dict->Set("contentInfo", snippet->ToDictionary()); | |
| 183 list.Append(std::move(dict)); | |
| 184 } | |
| 185 pref_service_->Set(prefs::kSnippets, list); | |
| 143 } | 186 } |
| 144 | 187 |
| 145 } // namespace ntp_snippets | 188 } // namespace ntp_snippets |
| OLD | NEW |