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_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 registry->RegisterListPref(prefs::kSnippets); | 60 registry->RegisterListPref(prefs::kSnippets); |
61 } | 61 } |
62 | 62 |
63 void NTPSnippetsService::Init(bool enabled) { | 63 void NTPSnippetsService::Init(bool enabled) { |
64 // If enabled, get any existing snippets immediately from prefs. | 64 // If enabled, get any existing snippets immediately from prefs. |
65 if (enabled) { | 65 if (enabled) { |
66 LoadFromPrefs(); | 66 LoadFromPrefs(); |
67 | 67 |
68 // If we don't have any snippets yet, start a fetch. | 68 // If we don't have any snippets yet, start a fetch. |
69 if (snippets_.empty()) | 69 if (snippets_.empty()) |
70 FetchSnippets(false); | 70 FetchSnippets(); |
71 } | 71 } |
72 | 72 |
73 // The scheduler only exists on Android so far, it's null on other platforms. | 73 // The scheduler only exists on Android so far, it's null on other platforms. |
74 if (!scheduler_) | 74 if (!scheduler_) |
75 return; | 75 return; |
76 | 76 |
77 if (enabled) | 77 if (enabled) |
78 scheduler_->Schedule(kDefaultFetchingIntervalSeconds); | 78 scheduler_->Schedule(kDefaultFetchingIntervalSeconds); |
79 else | 79 else |
80 scheduler_->Unschedule(); | 80 scheduler_->Unschedule(); |
81 } | 81 } |
82 | 82 |
83 void NTPSnippetsService::Shutdown() { | 83 void NTPSnippetsService::Shutdown() { |
84 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 84 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
85 NTPSnippetsServiceShutdown(this)); | 85 NTPSnippetsServiceShutdown(this)); |
86 loaded_ = false; | 86 loaded_ = false; |
87 } | 87 } |
88 | 88 |
89 void NTPSnippetsService::FetchSnippets(bool overwrite) { | 89 void NTPSnippetsService::FetchSnippets() { |
90 snippets_fetcher_->FetchSnippets(overwrite); | 90 snippets_fetcher_->FetchSnippets(); |
91 } | 91 } |
92 | 92 |
93 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { | 93 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { |
94 observers_.AddObserver(observer); | 94 observers_.AddObserver(observer); |
95 if (loaded_) | 95 if (loaded_) |
96 observer->NTPSnippetsServiceLoaded(this); | 96 observer->NTPSnippetsServiceLoaded(this); |
97 } | 97 } |
98 | 98 |
99 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { | 99 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { |
100 observers_.RemoveObserver(observer); | 100 observers_.RemoveObserver(observer); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 if (snippet->expiry_date() < next_expiry) | 220 if (snippet->expiry_date() < next_expiry) |
221 next_expiry = snippet->expiry_date(); | 221 next_expiry = snippet->expiry_date(); |
222 } | 222 } |
223 DCHECK_GT(next_expiry, expiry); | 223 DCHECK_GT(next_expiry, expiry); |
224 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, | 224 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, |
225 base::Bind(&NTPSnippetsService::RemoveExpiredSnippets, | 225 base::Bind(&NTPSnippetsService::RemoveExpiredSnippets, |
226 base::Unretained(this))); | 226 base::Unretained(this))); |
227 } | 227 } |
228 | 228 |
229 } // namespace ntp_snippets | 229 } // namespace ntp_snippets |
OLD | NEW |