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