Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(195)

Side by Side Diff: components/ntp_snippets/ntp_snippets_service.cc

Issue 1699143002: [NTP Snippets] Schedule periodic fetching (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@snippets_feature
Patch Set: rebase Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 {
noyau (Ping after 24h) 2016/02/22 11:58:49 nit: I would avoid nesting the namespaces, this li
Marc Treib 2016/02/22 12:44:32 Done. Nesting is a somewhat common pattern though.
16 16
17 namespace {
18
19 // TODO(treib): This is an extremely small value, for ease of development.
noyau (Ping after 24h) 2016/02/22 11:58:49 Please use the format TODO(crbug/XXXX): and add a
Bernhard Bauer 2016/02/22 12:05:12 Nit: `TODO(crbug.com/1234)`, to make it clickable
Marc Treib 2016/02/22 12:44:31 Done.
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 if (!success) 26 if (!success)
21 DLOG(ERROR) << "Error reading file " << path.LossyDisplayName(); 27 DLOG(ERROR) << "Error reading file " << path.LossyDisplayName();
22 return data; 28 return data;
23 } 29 }
24 30
31 } // namespace
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
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698