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

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: 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
« no previous file with comments | « chrome/browser/android/ntp_snippets_bridge.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 30 matching lines...) Expand all
41 snippets_fetcher_(std::move(snippets_fetcher)), 41 snippets_fetcher_(std::move(snippets_fetcher)),
42 weak_ptr_factory_(this) { 42 weak_ptr_factory_(this) {
43 snippets_fetcher_callback_ = snippets_fetcher_->AddCallback( 43 snippets_fetcher_callback_ = snippets_fetcher_->AddCallback(
44 base::Bind(&NTPSnippetsService::OnSnippetsDownloaded, 44 base::Bind(&NTPSnippetsService::OnSnippetsDownloaded,
45 weak_ptr_factory_.GetWeakPtr())); 45 weak_ptr_factory_.GetWeakPtr()));
46 } 46 }
47 47
48 NTPSnippetsService::~NTPSnippetsService() {} 48 NTPSnippetsService::~NTPSnippetsService() {}
49 49
50 void NTPSnippetsService::Init(bool enabled) { 50 void NTPSnippetsService::Init(bool enabled) {
51 // If enabled, get snippets immediately. If we've downloaded them before,
52 // this will just read from disk.
53 if (enabled)
54 FetchSnippets(false);
55
51 // The scheduler only exists on Android so far, it's null on other platforms. 56 // The scheduler only exists on Android so far, it's null on other platforms.
52 if (!scheduler_) 57 if (!scheduler_)
53 return; 58 return;
54 59
55 if (enabled) 60 if (enabled)
56 scheduler_->Schedule(kDefaultFetchingIntervalSeconds); 61 scheduler_->Schedule(kDefaultFetchingIntervalSeconds);
57 else 62 else
58 scheduler_->Unschedule(); 63 scheduler_->Unschedule();
59 } 64 }
60 65
(...skipping 19 matching lines...) Expand all
80 85
81 void NTPSnippetsService::OnFileReadDone(const std::string* json, bool success) { 86 void NTPSnippetsService::OnFileReadDone(const std::string* json, bool success) {
82 if (!success) 87 if (!success)
83 return; 88 return;
84 89
85 DCHECK(json); 90 DCHECK(json);
86 LoadFromJSONString(*json); 91 LoadFromJSONString(*json);
87 } 92 }
88 93
89 bool NTPSnippetsService::LoadFromJSONString(const std::string& str) { 94 bool NTPSnippetsService::LoadFromJSONString(const std::string& str) {
95 snippets_.clear();
96
90 JSONStringValueDeserializer deserializer(str); 97 JSONStringValueDeserializer deserializer(str);
91 int error_code; 98 int error_code;
92 std::string error_message; 99 std::string error_message;
93 100
94 scoped_ptr<base::Value> deserialized = 101 scoped_ptr<base::Value> deserialized =
95 deserializer.Deserialize(&error_code, &error_message); 102 deserializer.Deserialize(&error_code, &error_message);
96 if (!deserialized) 103 if (!deserialized)
97 return false; 104 return false;
98 105
99 const base::DictionaryValue* top_dict = NULL; 106 const base::DictionaryValue* top_dict = NULL;
(...skipping 29 matching lines...) Expand all
129 const base::FilePath& download_path) { 136 const base::FilePath& download_path) {
130 std::string* downloaded_data = new std::string; 137 std::string* downloaded_data = new std::string;
131 base::PostTaskAndReplyWithResult( 138 base::PostTaskAndReplyWithResult(
132 file_task_runner_.get(), FROM_HERE, 139 file_task_runner_.get(), FROM_HERE,
133 base::Bind(&ReadFileToString, download_path, downloaded_data), 140 base::Bind(&ReadFileToString, download_path, downloaded_data),
134 base::Bind(&NTPSnippetsService::OnFileReadDone, 141 base::Bind(&NTPSnippetsService::OnFileReadDone,
135 weak_ptr_factory_.GetWeakPtr(), base::Owned(downloaded_data))); 142 weak_ptr_factory_.GetWeakPtr(), base::Owned(downloaded_data)));
136 } 143 }
137 144
138 } // namespace ntp_snippets 145 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « chrome/browser/android/ntp_snippets_bridge.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698