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

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

Issue 1723493002: [NTP Snippets] Fetch/load snippets on startup, and pass them on NTP creation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@gcmtaskservice
Patch Set: 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"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 snippets_fetcher_(std::move(snippets_fetcher)), 42 snippets_fetcher_(std::move(snippets_fetcher)),
43 weak_ptr_factory_(this) { 43 weak_ptr_factory_(this) {
44 snippets_fetcher_callback_ = snippets_fetcher_->AddCallback( 44 snippets_fetcher_callback_ = snippets_fetcher_->AddCallback(
45 base::Bind(&NTPSnippetsService::OnSnippetsDownloaded, 45 base::Bind(&NTPSnippetsService::OnSnippetsDownloaded,
46 weak_ptr_factory_.GetWeakPtr())); 46 weak_ptr_factory_.GetWeakPtr()));
47 } 47 }
48 48
49 NTPSnippetsService::~NTPSnippetsService() {} 49 NTPSnippetsService::~NTPSnippetsService() {}
50 50
51 void NTPSnippetsService::Init(bool enabled) { 51 void NTPSnippetsService::Init(bool enabled) {
52 // If enabled, get snippets immediately. If we've downloaded them before,
53 // this will just read from disk.
54 if (enabled)
55 FetchSnippets(false);
56
52 // The scheduler only exists on Android so far, it's null on other platforms. 57 // The scheduler only exists on Android so far, it's null on other platforms.
53 if (!scheduler_) 58 if (!scheduler_)
54 return; 59 return;
55 60
56 if (enabled) 61 if (enabled)
57 scheduler_->Schedule(kDefaultFetchingIntervalSeconds); 62 scheduler_->Schedule(kDefaultFetchingIntervalSeconds);
58 else 63 else
59 scheduler_->Unschedule(); 64 scheduler_->Unschedule();
60 } 65 }
61 66
(...skipping 15 matching lines...) Expand all
77 82
78 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { 83 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) {
79 observers_.RemoveObserver(observer); 84 observers_.RemoveObserver(observer);
80 } 85 }
81 86
82 void NTPSnippetsService::OnFileReadDone(const std::string& json) { 87 void NTPSnippetsService::OnFileReadDone(const std::string& json) {
83 LoadFromJSONString(json); 88 LoadFromJSONString(json);
84 } 89 }
85 90
86 bool NTPSnippetsService::LoadFromJSONString(const std::string& str) { 91 bool NTPSnippetsService::LoadFromJSONString(const std::string& str) {
92 snippets_.clear();
93
87 JSONStringValueDeserializer deserializer(str); 94 JSONStringValueDeserializer deserializer(str);
88 int error_code; 95 int error_code;
89 std::string error_message; 96 std::string error_message;
90 97
91 scoped_ptr<base::Value> deserialized = 98 scoped_ptr<base::Value> deserialized =
92 deserializer.Deserialize(&error_code, &error_message); 99 deserializer.Deserialize(&error_code, &error_message);
93 if (!deserialized) 100 if (!deserialized)
94 return false; 101 return false;
95 102
96 const base::DictionaryValue* top_dict = NULL; 103 const base::DictionaryValue* top_dict = NULL;
(...skipping 28 matching lines...) Expand all
125 void NTPSnippetsService::OnSnippetsDownloaded( 132 void NTPSnippetsService::OnSnippetsDownloaded(
126 const base::FilePath& download_path) { 133 const base::FilePath& download_path) {
127 base::PostTaskAndReplyWithResult( 134 base::PostTaskAndReplyWithResult(
128 file_task_runner_.get(), FROM_HERE, 135 file_task_runner_.get(), FROM_HERE,
129 base::Bind(&ReadFileToString, download_path), 136 base::Bind(&ReadFileToString, download_path),
130 base::Bind(&NTPSnippetsService::OnFileReadDone, 137 base::Bind(&NTPSnippetsService::OnFileReadDone,
131 weak_ptr_factory_.GetWeakPtr())); 138 weak_ptr_factory_.GetWeakPtr()));
132 } 139 }
133 140
134 } // namespace ntp_snippets 141 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698