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

Side by Side Diff: chrome/browser/resources/snippets_internals.js

Issue 1987333003: [NTP Snippets] Persist snippets in a LevelDB instead of prefs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix test memleaks Created 4 years, 6 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 cr.define('chrome.SnippetsInternals', function() { 5 cr.define('chrome.SnippetsInternals', function() {
6 'use strict'; 6 'use strict';
7 7
8 // Stores the list of snippets we received in receiveSnippets.
9 var lastSnippets = [];
10
8 function initialize() { 11 function initialize() {
9 $('submit-download').addEventListener('click', function(event) { 12 $('submit-download').addEventListener('click', function(event) {
10 chrome.send('download', [$('hosts-input').value]); 13 chrome.send('download', [$('hosts-input').value]);
11 event.preventDefault(); 14 event.preventDefault();
12 }); 15 });
13 16
14 $('submit-clear').addEventListener('click', function(event) { 17 $('submit-clear').addEventListener('click', function(event) {
15 chrome.send('clear'); 18 chrome.send('clear');
16 event.preventDefault(); 19 event.preventDefault();
17 }); 20 });
18 21
19 $('submit-dump').addEventListener('click', function(event) { 22 $('submit-dump').addEventListener('click', function(event) {
20 chrome.send('dump'); 23 downloadJson(JSON.stringify(lastSnippets));
21 event.preventDefault(); 24 event.preventDefault();
22 }); 25 });
23 26
24 $('last-json-button').addEventListener('click', function(event) { 27 $('last-json-button').addEventListener('click', function(event) {
25 $('last-json-container').classList.toggle('hidden'); 28 $('last-json-container').classList.toggle('hidden');
26 }); 29 });
27 30
28 $('last-json-dump').addEventListener('click', function(event) { 31 $('last-json-dump').addEventListener('click', function(event) {
29 receiveJsonToDownload($('last-json-text').innerText); 32 downloadJson($('last-json-text').innerText);
30 event.preventDefault(); 33 event.preventDefault();
31 }); 34 });
32 35
33 $('discarded-snippets-clear').addEventListener('click', function(event) { 36 $('discarded-snippets-clear').addEventListener('click', function(event) {
34 chrome.send('clearDiscarded'); 37 chrome.send('clearDiscarded');
35 event.preventDefault(); 38 event.preventDefault();
36 }); 39 });
37 40
38 chrome.send('loaded'); 41 chrome.send('loaded');
39 } 42 }
(...skipping 10 matching lines...) Expand all
50 } 53 }
51 54
52 function receiveHosts(hosts) { 55 function receiveHosts(hosts) {
53 displayList(hosts, 'hosts'); 56 displayList(hosts, 'hosts');
54 57
55 $('hosts-input').value = hosts.list.map( 58 $('hosts-input').value = hosts.list.map(
56 function(host) { return host.url;}).join(' '); 59 function(host) { return host.url;}).join(' ');
57 } 60 }
58 61
59 function receiveSnippets(snippets) { 62 function receiveSnippets(snippets) {
63 lastSnippets = snippets;
60 displayList(snippets, 'snippets', 'snippet-title'); 64 displayList(snippets, 'snippets', 'snippet-title');
61 } 65 }
62 66
63 function receiveDiscardedSnippets(discardedSnippets) { 67 function receiveDiscardedSnippets(discardedSnippets) {
64 displayList(discardedSnippets, 'discarded-snippets', 68 displayList(discardedSnippets, 'discarded-snippets',
65 'discarded-snippet-title'); 69 'discarded-snippet-title');
66 } 70 }
67 71
68 function receiveJson(json) { 72 function receiveJson(json) {
69 var trimmed = json.trim(); 73 var trimmed = json.trim();
70 var hasContent = (trimmed && trimmed != '{}'); 74 var hasContent = (trimmed && trimmed != '{}');
71 75
72 if (hasContent) { 76 if (hasContent) {
73 receiveProperty('last-json-text', trimmed); 77 receiveProperty('last-json-text', trimmed);
74 $('last-json').classList.remove('hidden'); 78 $('last-json').classList.remove('hidden');
75 } else { 79 } else {
76 $('last-json').classList.add('hidden'); 80 $('last-json').classList.add('hidden');
77 } 81 }
78 } 82 }
79 83
80 function receiveJsonToDownload(json) { 84 function downloadJson(json) {
81 // Redirect the browser to download data in |json| as a file "snippets.json" 85 // Redirect the browser to download data in |json| as a file "snippets.json"
82 // (Setting Content-Disposition: attachment via a data: URL is not possible; 86 // (Setting Content-Disposition: attachment via a data: URL is not possible;
83 // create a link with download attribute and simulate a click, instead.) 87 // create a link with download attribute and simulate a click, instead.)
84 var link = document.createElement('a'); 88 var link = document.createElement('a');
85 link.download = 'snippets.json'; 89 link.download = 'snippets.json';
86 link.href = 'data:,' + json; 90 link.href = 'data:,' + json;
87 link.click(); 91 link.click();
88 } 92 }
89 93
90 function displayList(object, domId, titleClass) { 94 function displayList(object, domId, titleClass) {
(...skipping 24 matching lines...) Expand all
115 119
116 // Return an object with all of the exports. 120 // Return an object with all of the exports.
117 return { 121 return {
118 initialize: initialize, 122 initialize: initialize,
119 setHostRestricted: setHostRestricted, 123 setHostRestricted: setHostRestricted,
120 receiveProperty: receiveProperty, 124 receiveProperty: receiveProperty,
121 receiveHosts: receiveHosts, 125 receiveHosts: receiveHosts,
122 receiveSnippets: receiveSnippets, 126 receiveSnippets: receiveSnippets,
123 receiveDiscardedSnippets: receiveDiscardedSnippets, 127 receiveDiscardedSnippets: receiveDiscardedSnippets,
124 receiveJson: receiveJson, 128 receiveJson: receiveJson,
125 receiveJsonToDownload: receiveJsonToDownload,
126 }; 129 };
127 }); 130 });
128 131
129 document.addEventListener('DOMContentLoaded', 132 document.addEventListener('DOMContentLoaded',
130 chrome.SnippetsInternals.initialize); 133 chrome.SnippetsInternals.initialize);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698