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_string_value_serializer.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 | 14 |
| 15 namespace ntp_snippets { | 15 namespace ntp_snippets { |
| 16 | 16 |
| 17 namespace { | |
| 18 | |
| 19 // TODO(treib): Add a command line param to control this. | |
| 20 const int kDefaultFetchingIntervalSeconds = 30 * 60; | |
|
Bernhard Bauer
2016/02/16 16:19:08
TBH, while we are testing this feature, I would re
Marc Treib
2016/02/16 16:54:27
Eh, IMO the value doesn't really matter yet - I wa
| |
| 21 | |
| 17 std::string ReadFileToString(const base::FilePath& path) { | 22 std::string ReadFileToString(const base::FilePath& path) { |
| 18 std::string data; | 23 std::string data; |
| 19 bool success = base::ReadFileToString(path, &data); | 24 bool success = base::ReadFileToString(path, &data); |
| 20 DLOG_IF(ERROR, !success) << "Error reading file " << path.LossyDisplayName(); | 25 DLOG_IF(ERROR, !success) << "Error reading file " << path.LossyDisplayName(); |
| 21 return data; | 26 return data; |
| 22 } | 27 } |
| 23 | 28 |
| 29 } // namespace | |
| 30 | |
| 24 NTPSnippetsService::NTPSnippetsService( | 31 NTPSnippetsService::NTPSnippetsService( |
| 25 scoped_refptr<base::SequencedTaskRunner> file_task_runner, | 32 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
| 26 const std::string& application_language_code, | 33 const std::string& application_language_code, |
| 34 NTPSnippetsScheduler* scheduler, | |
| 27 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher) | 35 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher) |
| 28 : loaded_(false), | 36 : loaded_(false), |
| 29 file_task_runner_(file_task_runner), | 37 file_task_runner_(file_task_runner), |
| 30 application_language_code_(application_language_code), | 38 application_language_code_(application_language_code), |
| 39 scheduler_(scheduler), | |
| 31 snippets_fetcher_(std::move(snippets_fetcher)), | 40 snippets_fetcher_(std::move(snippets_fetcher)), |
| 32 weak_ptr_factory_(this) { | 41 weak_ptr_factory_(this) { |
| 33 snippets_fetcher_callback_ = snippets_fetcher_->AddCallback( | 42 snippets_fetcher_callback_ = snippets_fetcher_->AddCallback( |
| 34 base::Bind(&NTPSnippetsService::OnSnippetsDownloaded, | 43 base::Bind(&NTPSnippetsService::OnSnippetsDownloaded, |
| 35 weak_ptr_factory_.GetWeakPtr())); | 44 weak_ptr_factory_.GetWeakPtr())); |
| 36 } | 45 } |
| 37 | 46 |
| 38 NTPSnippetsService::~NTPSnippetsService() {} | 47 NTPSnippetsService::~NTPSnippetsService() {} |
| 39 | 48 |
| 49 void NTPSnippetsService::Init(bool enabled) { | |
| 50 if (scheduler_) { | |
|
Bernhard Bauer
2016/02/16 16:19:08
When would the scheduler be null?
Marc Treib
2016/02/16 16:54:27
I was vaguely thinking of iOS, which has an NTPSni
| |
| 51 if (enabled) | |
| 52 scheduler_->Schedule(kDefaultFetchingIntervalSeconds); | |
| 53 else | |
| 54 scheduler_->Unschedule(); | |
| 55 } | |
| 56 } | |
| 57 | |
| 40 void NTPSnippetsService::Shutdown() { | 58 void NTPSnippetsService::Shutdown() { |
| 41 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 59 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
| 42 NTPSnippetsServiceShutdown(this)); | 60 NTPSnippetsServiceShutdown(this)); |
| 43 loaded_ = false; | 61 loaded_ = false; |
| 44 } | 62 } |
| 45 | 63 |
| 46 void NTPSnippetsService::FetchSnippets(bool overwrite) { | 64 void NTPSnippetsService::FetchSnippets(bool overwrite) { |
| 47 snippets_fetcher_->FetchSnippets(overwrite); | 65 snippets_fetcher_->FetchSnippets(overwrite); |
| 48 } | 66 } |
| 49 | 67 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 void NTPSnippetsService::OnSnippetsDownloaded( | 121 void NTPSnippetsService::OnSnippetsDownloaded( |
| 104 const base::FilePath& download_path) { | 122 const base::FilePath& download_path) { |
| 105 base::PostTaskAndReplyWithResult( | 123 base::PostTaskAndReplyWithResult( |
| 106 file_task_runner_.get(), FROM_HERE, | 124 file_task_runner_.get(), FROM_HERE, |
| 107 base::Bind(&ReadFileToString, download_path), | 125 base::Bind(&ReadFileToString, download_path), |
| 108 base::Bind(&NTPSnippetsService::OnFileReadDone, | 126 base::Bind(&NTPSnippetsService::OnFileReadDone, |
| 109 weak_ptr_factory_.GetWeakPtr())); | 127 weak_ptr_factory_.GetWeakPtr())); |
| 110 } | 128 } |
| 111 | 129 |
| 112 } // namespace ntp_snippets | 130 } // namespace ntp_snippets |
| OLD | NEW |