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): This is an extremely small value, for ease of development. |
| 20 // Replace it by something sensible and add a command line param to control it. |
| 21 const int kDefaultFetchingIntervalSeconds = 60; |
| 22 |
17 std::string ReadFileToString(const base::FilePath& path) { | 23 std::string ReadFileToString(const base::FilePath& path) { |
18 std::string data; | 24 std::string data; |
19 bool success = base::ReadFileToString(path, &data); | 25 bool success = base::ReadFileToString(path, &data); |
20 DLOG_IF(ERROR, !success) << "Error reading file " << path.LossyDisplayName(); | 26 DLOG_IF(ERROR, !success) << "Error reading file " << path.LossyDisplayName(); |
21 return data; | 27 return data; |
22 } | 28 } |
23 | 29 |
| 30 } // namespace |
| 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 if (enabled) |
| 52 scheduler_->Schedule(kDefaultFetchingIntervalSeconds); |
| 53 else |
| 54 scheduler_->Unschedule(); |
| 55 } |
| 56 |
40 void NTPSnippetsService::Shutdown() { | 57 void NTPSnippetsService::Shutdown() { |
41 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 58 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
42 NTPSnippetsServiceShutdown(this)); | 59 NTPSnippetsServiceShutdown(this)); |
43 loaded_ = false; | 60 loaded_ = false; |
44 } | 61 } |
45 | 62 |
46 void NTPSnippetsService::FetchSnippets(bool overwrite) { | 63 void NTPSnippetsService::FetchSnippets(bool overwrite) { |
47 snippets_fetcher_->FetchSnippets(overwrite); | 64 snippets_fetcher_->FetchSnippets(overwrite); |
48 } | 65 } |
49 | 66 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 void NTPSnippetsService::OnSnippetsDownloaded( | 120 void NTPSnippetsService::OnSnippetsDownloaded( |
104 const base::FilePath& download_path) { | 121 const base::FilePath& download_path) { |
105 base::PostTaskAndReplyWithResult( | 122 base::PostTaskAndReplyWithResult( |
106 file_task_runner_.get(), FROM_HERE, | 123 file_task_runner_.get(), FROM_HERE, |
107 base::Bind(&ReadFileToString, download_path), | 124 base::Bind(&ReadFileToString, download_path), |
108 base::Bind(&NTPSnippetsService::OnFileReadDone, | 125 base::Bind(&NTPSnippetsService::OnFileReadDone, |
109 weak_ptr_factory_.GetWeakPtr())); | 126 weak_ptr_factory_.GetWeakPtr())); |
110 } | 127 } |
111 | 128 |
112 } // namespace ntp_snippets | 129 } // namespace ntp_snippets |
OLD | NEW |