| 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 { |
| 16 |
| 17 // 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. |
| 19 const int kDefaultFetchingIntervalSeconds = 60; |
| 16 | 20 |
| 17 std::string ReadFileToString(const base::FilePath& path) { | 21 std::string ReadFileToString(const base::FilePath& path) { |
| 18 std::string data; | 22 std::string data; |
| 19 bool success = base::ReadFileToString(path, &data); | 23 bool success = base::ReadFileToString(path, &data); |
| 20 if (!success) | 24 if (!success) |
| 21 DLOG(ERROR) << "Error reading file " << path.LossyDisplayName(); | 25 DLOG(ERROR) << "Error reading file " << path.LossyDisplayName(); |
| 22 return data; | 26 return data; |
| 23 } | 27 } |
| 24 | 28 |
| 29 } // namespace |
| 30 |
| 31 namespace ntp_snippets { |
| 32 |
| 25 NTPSnippetsService::NTPSnippetsService( | 33 NTPSnippetsService::NTPSnippetsService( |
| 26 scoped_refptr<base::SequencedTaskRunner> file_task_runner, | 34 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
| 27 const std::string& application_language_code, | 35 const std::string& application_language_code, |
| 36 NTPSnippetsScheduler* scheduler, |
| 28 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher) | 37 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher) |
| 29 : loaded_(false), | 38 : loaded_(false), |
| 30 file_task_runner_(file_task_runner), | 39 file_task_runner_(file_task_runner), |
| 31 application_language_code_(application_language_code), | 40 application_language_code_(application_language_code), |
| 41 scheduler_(scheduler), |
| 32 snippets_fetcher_(std::move(snippets_fetcher)), | 42 snippets_fetcher_(std::move(snippets_fetcher)), |
| 33 weak_ptr_factory_(this) { | 43 weak_ptr_factory_(this) { |
| 34 snippets_fetcher_callback_ = snippets_fetcher_->AddCallback( | 44 snippets_fetcher_callback_ = snippets_fetcher_->AddCallback( |
| 35 base::Bind(&NTPSnippetsService::OnSnippetsDownloaded, | 45 base::Bind(&NTPSnippetsService::OnSnippetsDownloaded, |
| 36 weak_ptr_factory_.GetWeakPtr())); | 46 weak_ptr_factory_.GetWeakPtr())); |
| 37 } | 47 } |
| 38 | 48 |
| 39 NTPSnippetsService::~NTPSnippetsService() {} | 49 NTPSnippetsService::~NTPSnippetsService() {} |
| 40 | 50 |
| 51 void NTPSnippetsService::Init(bool enabled) { |
| 52 // The scheduler only exists on Android so far, it's null on other platforms. |
| 53 if (!scheduler_) |
| 54 return; |
| 55 |
| 56 if (enabled) |
| 57 scheduler_->Schedule(kDefaultFetchingIntervalSeconds); |
| 58 else |
| 59 scheduler_->Unschedule(); |
| 60 } |
| 61 |
| 41 void NTPSnippetsService::Shutdown() { | 62 void NTPSnippetsService::Shutdown() { |
| 42 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 63 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
| 43 NTPSnippetsServiceShutdown(this)); | 64 NTPSnippetsServiceShutdown(this)); |
| 44 loaded_ = false; | 65 loaded_ = false; |
| 45 } | 66 } |
| 46 | 67 |
| 47 void NTPSnippetsService::FetchSnippets(bool overwrite) { | 68 void NTPSnippetsService::FetchSnippets(bool overwrite) { |
| 48 snippets_fetcher_->FetchSnippets(overwrite); | 69 snippets_fetcher_->FetchSnippets(overwrite); |
| 49 } | 70 } |
| 50 | 71 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 void NTPSnippetsService::OnSnippetsDownloaded( | 125 void NTPSnippetsService::OnSnippetsDownloaded( |
| 105 const base::FilePath& download_path) { | 126 const base::FilePath& download_path) { |
| 106 base::PostTaskAndReplyWithResult( | 127 base::PostTaskAndReplyWithResult( |
| 107 file_task_runner_.get(), FROM_HERE, | 128 file_task_runner_.get(), FROM_HERE, |
| 108 base::Bind(&ReadFileToString, download_path), | 129 base::Bind(&ReadFileToString, download_path), |
| 109 base::Bind(&NTPSnippetsService::OnFileReadDone, | 130 base::Bind(&NTPSnippetsService::OnFileReadDone, |
| 110 weak_ptr_factory_.GetWeakPtr())); | 131 weak_ptr_factory_.GetWeakPtr())); |
| 111 } | 132 } |
| 112 | 133 |
| 113 } // namespace ntp_snippets | 134 } // namespace ntp_snippets |
| OLD | NEW |