1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/background_page_tracker.h"
#include <set>
#include <string>
#include <vector>
#include "base/command_line.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/background_application_list_model.h"
#include "chrome/browser/background_contents_service.h"
#include "chrome/browser/background_mode_manager.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"
#include "chrome/common/pref_names.h"
///////////////////////////////////////////////////////////////////////////////
// BackgroundPageTracker keeps a single DictionaryValue (stored at
// prefs::kKnownBackgroundPages). We keep only two pieces of information for
// each background page: the parent application/extension ID, and a boolean
// flag that is true if the user has acknowledged this page.
//
// kKnownBackgroundPages:
// DictionaryValue {
// <appid_1>: false,
// <appid_2>: true,
// ... etc ...
// }
// static
void BackgroundPageTracker::RegisterPrefs(PrefService* prefs) {
prefs->RegisterDictionaryPref(prefs::kKnownBackgroundPages);
}
// static
BackgroundPageTracker* BackgroundPageTracker::GetInstance() {
return Singleton<BackgroundPageTracker>::get();
}
int BackgroundPageTracker::GetBackgroundPageCount() {
if (!IsEnabled())
return 0;
PrefService* prefs = GetPrefService();
const DictionaryValue* contents =
prefs->GetDictionary(prefs::kKnownBackgroundPages);
return contents ? contents->size() : 0;
}
int BackgroundPageTracker::GetUnacknowledgedBackgroundPageCount() {
if (!IsEnabled())
return 0;
PrefService* prefs = GetPrefService();
const DictionaryValue* contents =
prefs->GetDictionary(prefs::kKnownBackgroundPages);
if (!contents)
return 0;
int count = 0;
for (DictionaryValue::key_iterator it = contents->begin_keys();
it != contents->end_keys(); ++it) {
Value* value;
bool found = contents->GetWithoutPathExpansion(*it, &value);
DCHECK(found);
bool acknowledged = true;
bool valid = value->GetAsBoolean(&acknowledged);
DCHECK(valid);
if (!acknowledged)
count++;
}
return count;
}
void BackgroundPageTracker::AcknowledgeBackgroundPages() {
if (!IsEnabled())
return;
PrefService* prefs = GetPrefService();
DictionaryValue* contents =
prefs->GetMutableDictionary(prefs::kKnownBackgroundPages);
if (!contents)
return;
bool prefs_modified = false;
for (DictionaryValue::key_iterator it = contents->begin_keys();
it != contents->end_keys(); ++it) {
contents->SetWithoutPathExpansion(*it, Value::CreateBooleanValue(true));
prefs_modified = true;
}
if (prefs_modified) {
prefs->ScheduleSavePersistentPrefs();
SendChangeNotification();
}
}
BackgroundPageTracker::BackgroundPageTracker() {
// If background mode is disabled, just exit - don't load information from
// prefs or listen for any notifications so we will act as if there are no
// background pages, effectively disabling any associated badging.
if (!IsEnabled())
return;
// Check to make sure all of the extensions are loaded - once they are loaded
// we can update the list.
Profile* profile = g_browser_process->profile_manager()->GetDefaultProfile();
if (profile->GetExtensionService() &&
profile->GetExtensionService()->is_ready()) {
UpdateExtensionList();
// We do not send any change notifications here, because the object was
// just created (it doesn't seem appropriate to send a change notification
// at initialization time). Also, since this is a singleton object, sending
// a notification in the constructor can lead to deadlock if one of the
// observers tries to get the singleton.
} else {
// Extensions aren't loaded yet - register to be notified when they are
// ready.
registrar_.Add(this, NotificationType::EXTENSIONS_READY,
NotificationService::AllSources());
}
}
BackgroundPageTracker::~BackgroundPageTracker() {
}
PrefService* BackgroundPageTracker::GetPrefService() {
PrefService* service = g_browser_process->local_state();
DCHECK(service);
return service;
}
bool BackgroundPageTracker::IsEnabled() {
// Disable the background page tracker for unittests.
if (!g_browser_process->local_state())
return false;
// BackgroundPageTracker is enabled if background mode is enabled.
CommandLine* command_line = CommandLine::ForCurrentProcess();
return BackgroundModeManager::IsBackgroundModeEnabled(command_line);
}
void BackgroundPageTracker::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
switch (type.value) {
case NotificationType::EXTENSIONS_READY:
if (UpdateExtensionList())
SendChangeNotification();
break;
case NotificationType::BACKGROUND_CONTENTS_OPENED: {
std::string id = UTF16ToUTF8(
Details<BackgroundContentsOpenedDetails>(details)->application_id);
OnBackgroundPageLoaded(id);
break;
}
case NotificationType::EXTENSION_LOADED: {
const Extension* extension = Details<const Extension>(details).ptr();
if (extension->background_url().is_valid())
OnBackgroundPageLoaded(extension->id());
break;
}
case NotificationType::EXTENSION_UNLOADED: {
std::string id = Details<UnloadedExtensionInfo>(details)->extension->id();
OnExtensionUnloaded(id);
break;
}
default:
NOTREACHED();
}
}
bool BackgroundPageTracker::UpdateExtensionList() {
// Extensions are loaded - update our list.
Profile* profile = g_browser_process->profile_manager()->GetDefaultProfile();
ExtensionService* extensions_service = profile->GetExtensionService();
DCHECK(extensions_service);
// We will make two passes to update the list:
// 1) Walk our list, and make sure that there's a corresponding extension for
// each item in the list. If not, delete it (extension was uninstalled).
// 2) Walk the set of currently loaded extensions and background contents, and
// make sure there's an entry in our list for each one. If not, create one.
PrefService* prefs = GetPrefService();
std::set<std::string> keys_to_delete;
bool pref_modified = false;
// If we've never set any prefs, then this is the first launch ever, so we
// want to automatically mark all existing extensions as acknowledged.
bool first_launch =
prefs->GetDictionary(prefs::kKnownBackgroundPages) == NULL;
DictionaryValue* contents =
prefs->GetMutableDictionary(prefs::kKnownBackgroundPages);
for (DictionaryValue::key_iterator it = contents->begin_keys();
it != contents->end_keys(); ++it) {
// Check to make sure that the parent extension is still enabled.
const Extension* extension = extensions_service->GetExtensionById(
*it, false);
// If the extension is not loaded, add the id to our list of keys to delete
// later (can't delete now since we're still iterating).
if (!extension) {
keys_to_delete.insert(*it);
pref_modified = true;
}
}
for (std::set<std::string>::const_iterator iter = keys_to_delete.begin();
iter != keys_to_delete.end();
++iter) {
contents->RemoveWithoutPathExpansion(*iter, NULL);
}
// Look for new extensions/background contents.
const ExtensionList* list = extensions_service->extensions();
for (ExtensionList::const_iterator iter = list->begin();
iter != list->begin();
++iter) {
// Any extension with a background page should be in our list.
if ((*iter)->background_url().is_valid()) {
// If we have not seen this extension ID before, add it to our list.
if (!contents->HasKey((*iter)->id())) {
contents->SetWithoutPathExpansion(
(*iter)->id(), Value::CreateBooleanValue(first_launch));
pref_modified = true;
}
}
}
// Add all apps with background contents also.
BackgroundContentsService* background_contents_service =
profile->GetBackgroundContentsService();
std::vector<BackgroundContents*> background_contents =
background_contents_service->GetBackgroundContents();
for (std::vector<BackgroundContents*>::const_iterator iter =
background_contents.begin();
iter != background_contents.end();
++iter) {
std::string application_id = UTF16ToUTF8(
background_contents_service->GetParentApplicationId(*iter));
if (!contents->HasKey(application_id)) {
contents->SetWithoutPathExpansion(
application_id, Value::CreateBooleanValue(first_launch));
pref_modified = true;
}
}
// Register for when new pages are loaded/unloaded so we can update our list.
registrar_.Add(this, NotificationType::EXTENSION_LOADED,
NotificationService::AllSources());
registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
NotificationService::AllSources());
registrar_.Add(this, NotificationType::BACKGROUND_CONTENTS_OPENED,
NotificationService::AllSources());
// If we modified the list, save it to prefs and let our caller know.
if (pref_modified)
prefs->ScheduleSavePersistentPrefs();
return pref_modified;
}
void BackgroundPageTracker::OnBackgroundPageLoaded(const std::string& id) {
DCHECK(IsEnabled());
PrefService* prefs = GetPrefService();
DictionaryValue* contents =
prefs->GetMutableDictionary(prefs::kKnownBackgroundPages);
// No need to update our list if this extension was already known.
if (!contents || contents->HasKey(id))
return;
// Update our list with this new as-yet-unacknowledged page.
contents->SetWithoutPathExpansion(id, Value::CreateBooleanValue(false));
prefs->ScheduleSavePersistentPrefs();
SendChangeNotification();
}
void BackgroundPageTracker::OnExtensionUnloaded(const std::string& id) {
DCHECK(IsEnabled());
PrefService* prefs = GetPrefService();
DictionaryValue* contents =
prefs->GetMutableDictionary(prefs::kKnownBackgroundPages);
if (!contents || !contents->HasKey(id))
return;
contents->RemoveWithoutPathExpansion(id, NULL);
prefs->ScheduleSavePersistentPrefs();
SendChangeNotification();
}
void BackgroundPageTracker::SendChangeNotification() {
NotificationService::current()->Notify(
NotificationType::BACKGROUND_PAGE_TRACKER_CHANGED,
Source<BackgroundPageTracker>(this),
NotificationService::NoDetails());
}
|