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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
// Copyright (c) 2012 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/managed_mode/managed_mode.h"
#include "base/command_line.h"
#include "base/prefs/public/pref_change_registrar.h"
#include "base/sequenced_task_runner.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/managed_mode/managed_mode_site_list.h"
#include "chrome/browser/managed_mode/managed_mode_url_filter.h"
#include "chrome/browser/policy/url_blacklist_manager.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/prefs/scoped_user_pref_update.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension_set.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
using content::BrowserThread;
// A bridge from ManagedMode (which lives on the UI thread) to
// ManagedModeURLFilter (which might live on a different thread).
class ManagedMode::URLFilterContext {
public:
explicit URLFilterContext(
scoped_refptr<base::SequencedTaskRunner> task_runner)
: task_runner_(task_runner) {}
~URLFilterContext() {}
const ManagedModeURLFilter* url_filter() const {
DCHECK(task_runner_->RunsTasksOnCurrentThread());
return &url_filter_;
}
void SetDefaultFilteringBehavior(
ManagedModeURLFilter::FilteringBehavior behavior) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// Because ManagedMode is a singleton, we can pass the pointer to
// |url_filter_| unretained.
task_runner_->PostTask(
FROM_HERE,
base::Bind(&ManagedModeURLFilter::SetDefaultFilteringBehavior,
base::Unretained(&url_filter_),
behavior));
}
void LoadWhitelists(ScopedVector<ManagedModeSiteList> site_lists) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
task_runner_->PostTask(FROM_HERE,
base::Bind(&ManagedModeURLFilter::LoadWhitelists,
base::Unretained(&url_filter_),
base::Passed(&site_lists),
base::Bind(&base::DoNothing)));
}
void SetManualLists(scoped_ptr<ListValue> whitelist,
scoped_ptr<ListValue> blacklist) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
task_runner_->PostTask(FROM_HERE,
base::Bind(&ManagedModeURLFilter::SetManualLists,
base::Unretained(&url_filter_),
base::Passed(&whitelist),
base::Passed(&blacklist)));
}
void AddURLPatternToManualList(bool is_whitelist,
const std::string& url) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
task_runner_->PostTask(FROM_HERE,
base::Bind(&ManagedModeURLFilter::AddURLPatternToManualList,
base::Unretained(&url_filter_),
is_whitelist,
url));
}
void ShutdownOnUIThread() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
bool result = task_runner_->DeleteSoon(FROM_HERE, this);
DCHECK(result);
}
private:
ManagedModeURLFilter url_filter_;
scoped_refptr<base::SequencedTaskRunner> task_runner_;
DISALLOW_COPY_AND_ASSIGN(URLFilterContext);
};
// static
ManagedMode* ManagedMode::GetInstance() {
return Singleton<ManagedMode, LeakySingletonTraits<ManagedMode> >::get();
}
// static
void ManagedMode::RegisterPrefs(PrefServiceSimple* prefs) {
prefs->RegisterBooleanPref(prefs::kInManagedMode, false);
}
// static
void ManagedMode::RegisterUserPrefs(PrefServiceSyncable* prefs) {
prefs->RegisterIntegerPref(prefs::kDefaultManagedModeFilteringBehavior,
2,
PrefServiceSyncable::UNSYNCABLE_PREF);
prefs->RegisterListPref(prefs::kManagedModeWhitelist,
PrefServiceSyncable::UNSYNCABLE_PREF);
prefs->RegisterListPref(prefs::kManagedModeBlacklist,
PrefServiceSyncable::UNSYNCABLE_PREF);
}
// static
void ManagedMode::Init(Profile* profile) {
GetInstance()->InitImpl(profile);
}
void ManagedMode::InitImpl(Profile* profile) {
DCHECK(g_browser_process);
DCHECK(g_browser_process->local_state());
Profile* original_profile = profile->GetOriginalProfile();
// Set the value directly in the PrefService instead of using
// CommandLinePrefStore so we can change it at runtime.
if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoManaged)) {
SetInManagedMode(NULL);
} else if (IsInManagedModeImpl() ||
CommandLine::ForCurrentProcess()->HasSwitch(switches::kManaged)) {
SetInManagedMode(original_profile);
}
}
// static
bool ManagedMode::IsInManagedMode() {
return GetInstance()->IsInManagedModeImpl();
}
bool ManagedMode::IsInManagedModeImpl() const {
// |g_browser_process| can be NULL during startup.
if (!g_browser_process)
return false;
// Local State can be NULL during unit tests.
if (!g_browser_process->local_state())
return false;
return g_browser_process->local_state()->GetBoolean(prefs::kInManagedMode);
}
// static
void ManagedMode::EnterManagedMode(Profile* profile,
const EnterCallback& callback) {
GetInstance()->EnterManagedModeImpl(profile, callback);
}
void ManagedMode::EnterManagedModeImpl(Profile* profile,
const EnterCallback& callback) {
Profile* original_profile = profile->GetOriginalProfile();
if (IsInManagedModeImpl()) {
callback.Run(original_profile == managed_profile_);
return;
}
if (!callbacks_.empty()) {
// We are already in the process of entering managed mode, waiting for
// browsers to close. Don't allow entering managed mode again for a
// different profile, and queue the callback for the same profile.
if (original_profile != managed_profile_)
callback.Run(false);
else
callbacks_.push_back(callback);
return;
}
if (!PlatformConfirmEnter()) {
callback.Run(false);
return;
}
// Close all other profiles.
// At this point, we shouldn't be waiting for other browsers to close (yet).
DCHECK_EQ(0u, browsers_to_close_.size());
for (BrowserList::const_iterator i = BrowserList::begin();
i != BrowserList::end(); ++i) {
if ((*i)->profile()->GetOriginalProfile() != original_profile)
browsers_to_close_.insert(*i);
}
if (browsers_to_close_.empty()) {
SetInManagedMode(original_profile);
callback.Run(true);
return;
}
// Remember the profile we're trying to manage while we wait for other
// browsers to close.
managed_profile_ = original_profile;
callbacks_.push_back(callback);
registrar_.Add(this, chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
content::NotificationService::AllSources());
registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED,
content::NotificationService::AllSources());
for (std::set<Browser*>::const_iterator i = browsers_to_close_.begin();
i != browsers_to_close_.end(); ++i) {
(*i)->window()->Close();
}
}
// static
void ManagedMode::LeaveManagedMode() {
GetInstance()->LeaveManagedModeImpl();
}
void ManagedMode::LeaveManagedModeImpl() {
bool confirmed = PlatformConfirmLeave();
if (confirmed)
SetInManagedMode(NULL);
}
// static
const ManagedModeURLFilter* ManagedMode::GetURLFilterForIOThread() {
return GetInstance()->GetURLFilterForIOThreadImpl();
}
// static
const ManagedModeURLFilter* ManagedMode::GetURLFilterForUIThread() {
return GetInstance()->GetURLFilterForUIThreadImpl();
}
const ManagedModeURLFilter* ManagedMode::GetURLFilterForIOThreadImpl() {
return io_url_filter_context_->url_filter();
}
const ManagedModeURLFilter* ManagedMode::GetURLFilterForUIThreadImpl() {
return ui_url_filter_context_->url_filter();
}
// static
void ManagedMode::AddToManualList(bool is_whitelist,
const base::ListValue& list) {
GetInstance()->AddToManualListImpl(is_whitelist, list);
}
void ManagedMode::AddToManualListImpl(bool is_whitelist,
const base::ListValue& list) {
if (!managed_profile_)
return;
ListPrefUpdate pref_update(managed_profile_->GetPrefs(),
is_whitelist ? prefs::kManagedModeWhitelist :
prefs::kManagedModeBlacklist);
ListValue* pref_list = pref_update.Get();
for (size_t i = 0; i < list.GetSize(); ++i) {
std::string url_pattern;
list.GetString(i, &url_pattern);
if (!IsInManualList(is_whitelist, url_pattern)) {
pref_list->AppendString(url_pattern);
AddURLPatternToManualList(is_whitelist, url_pattern);
}
}
}
// static
void ManagedMode::RemoveFromManualList(bool is_whitelist,
const base::ListValue& list) {
GetInstance()->RemoveFromManualListImpl(is_whitelist, list);
}
void ManagedMode::RemoveFromManualListImpl(bool is_whitelist,
const base::ListValue& list) {
ListPrefUpdate pref_update(managed_profile_->GetPrefs(),
is_whitelist ? prefs::kManagedModeWhitelist :
prefs::kManagedModeBlacklist);
ListValue* pref_list = pref_update.Get();
for (size_t i = 0; i < list.GetSize(); ++i) {
std::string pattern;
size_t out_index;
list.GetString(i, &pattern);
StringValue value_to_remove(pattern);
pref_list->Remove(value_to_remove, &out_index);
}
}
// static
bool ManagedMode::IsInManualList(bool is_whitelist,
const std::string& url_pattern) {
return GetInstance()->IsInManualListImpl(is_whitelist, url_pattern);
}
bool ManagedMode::IsInManualListImpl(bool is_whitelist,
const std::string& url_pattern) {
StringValue pattern(url_pattern);
const ListValue* list = managed_profile_->GetPrefs()->GetList(
is_whitelist ? prefs::kManagedModeWhitelist :
prefs::kManagedModeBlacklist);
return list->Find(pattern) != list->end();
}
// static
scoped_ptr<base::ListValue> ManagedMode::GetBlacklist() {
return scoped_ptr<base::ListValue>(
GetInstance()->managed_profile_->GetPrefs()->GetList(
prefs::kManagedModeBlacklist)->DeepCopy()).Pass();
}
std::string ManagedMode::GetDebugPolicyProviderName() const {
// Save the string space in official builds.
#ifdef NDEBUG
NOTREACHED();
return std::string();
#else
return "Managed Mode";
#endif
}
bool ManagedMode::UserMayLoad(const extensions::Extension* extension,
string16* error) const {
string16 tmp_error;
if (ExtensionManagementPolicyImpl(&tmp_error))
return true;
// If the extension is already loaded, we allow it, otherwise we'd unload
// all existing extensions.
ExtensionService* extension_service =
extensions::ExtensionSystem::Get(managed_profile_)->extension_service();
// |extension_service| can be NULL in a unit test.
if (extension_service &&
extension_service->GetInstalledExtension(extension->id()))
return true;
if (error)
*error = tmp_error;
return false;
}
bool ManagedMode::UserMayModifySettings(const extensions::Extension* extension,
string16* error) const {
return ExtensionManagementPolicyImpl(error);
}
bool ManagedMode::ExtensionManagementPolicyImpl(string16* error) const {
if (!IsInManagedModeImpl())
return true;
if (error)
*error = l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOCKED_MANAGED_MODE);
return false;
}
void ManagedMode::OnBrowserAdded(Browser* browser) {
// Return early if we don't have any queued callbacks.
if (callbacks_.empty())
return;
DCHECK(managed_profile_);
if (browser->profile()->GetOriginalProfile() != managed_profile_)
FinalizeEnter(false);
}
void ManagedMode::OnBrowserRemoved(Browser* browser) {
// Return early if we don't have any queued callbacks.
if (callbacks_.empty())
return;
DCHECK(managed_profile_);
if (browser->profile()->GetOriginalProfile() == managed_profile_) {
// Ignore closing browser windows that are in managed mode.
return;
}
size_t count = browsers_to_close_.erase(browser);
DCHECK_EQ(1u, count);
if (browsers_to_close_.empty())
FinalizeEnter(true);
}
ManagedMode::ManagedMode()
: managed_profile_(NULL),
io_url_filter_context_(
new URLFilterContext(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))),
ui_url_filter_context_(
new URLFilterContext(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI))) {
BrowserList::AddObserver(this);
}
ManagedMode::~ManagedMode() {
// This class usually is a leaky singleton, so this destructor shouldn't be
// called. We still do some cleanup, in case we're owned by a unit test.
BrowserList::RemoveObserver(this);
DCHECK_EQ(0u, callbacks_.size());
DCHECK_EQ(0u, browsers_to_close_.size());
io_url_filter_context_.release()->ShutdownOnUIThread();
ui_url_filter_context_.release()->ShutdownOnUIThread();
}
void ManagedMode::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
// Return early if we don't have any queued callbacks.
if (callbacks_.empty())
return;
switch (type) {
case chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST: {
FinalizeEnter(false);
return;
}
case chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED: {
Browser* browser = content::Source<Browser>(source).ptr();
if (browsers_to_close_.find(browser) != browsers_to_close_.end())
FinalizeEnter(false);
return;
}
default:
NOTREACHED();
}
}
void ManagedMode::FinalizeEnter(bool result) {
if (result)
SetInManagedMode(managed_profile_);
for (std::vector<EnterCallback>::iterator it = callbacks_.begin();
it != callbacks_.end(); ++it) {
it->Run(result);
}
callbacks_.clear();
browsers_to_close_.clear();
registrar_.RemoveAll();
}
bool ManagedMode::PlatformConfirmEnter() {
// TODO(bauerb): Show platform-specific confirmation dialog.
return true;
}
bool ManagedMode::PlatformConfirmLeave() {
// TODO(bauerb): Show platform-specific confirmation dialog.
return true;
}
void ManagedMode::SetInManagedMode(Profile* newly_managed_profile) {
// Register the ManagementPolicy::Provider before changing the pref when
// setting it, and unregister it after changing the pref when clearing it,
// so pref observers see the correct ManagedMode state.
bool in_managed_mode = !!newly_managed_profile;
if (in_managed_mode) {
DCHECK(!managed_profile_ || managed_profile_ == newly_managed_profile);
extensions::ExtensionSystem::Get(
newly_managed_profile)->management_policy()->RegisterProvider(this);
pref_change_registrar_.reset(new PrefChangeRegistrar());
pref_change_registrar_->Init(newly_managed_profile->GetPrefs());
pref_change_registrar_->Add(
prefs::kDefaultManagedModeFilteringBehavior,
base::Bind(
&ManagedMode::OnDefaultFilteringBehaviorChanged,
base::Unretained(this)));
} else {
extensions::ExtensionSystem::Get(
managed_profile_)->management_policy()->UnregisterProvider(this);
pref_change_registrar_.reset();
}
managed_profile_ = newly_managed_profile;
ManagedModeURLFilter::FilteringBehavior behavior =
ManagedModeURLFilter::ALLOW;
if (in_managed_mode) {
int behavior_value = managed_profile_->GetPrefs()->GetInteger(
prefs::kDefaultManagedModeFilteringBehavior);
behavior = ManagedModeURLFilter::BehaviorFromInt(behavior_value);
}
io_url_filter_context_->SetDefaultFilteringBehavior(behavior);
ui_url_filter_context_->SetDefaultFilteringBehavior(behavior);
g_browser_process->local_state()->SetBoolean(prefs::kInManagedMode,
in_managed_mode);
if (in_managed_mode)
UpdateManualListsImpl();
// This causes the avatar and the profile menu to get updated.
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
content::NotificationService::AllBrowserContextsAndSources(),
content::NotificationService::NoDetails());
}
ScopedVector<ManagedModeSiteList> ManagedMode::GetActiveSiteLists() {
DCHECK(managed_profile_);
ScopedVector<ManagedModeSiteList> site_lists;
// TODO(bauerb): Get site lists from all extensions.
return site_lists.Pass();
}
void ManagedMode::OnDefaultFilteringBehaviorChanged() {
DCHECK(IsInManagedModeImpl());
int behavior_value = managed_profile_->GetPrefs()->GetInteger(
prefs::kDefaultManagedModeFilteringBehavior);
ManagedModeURLFilter::FilteringBehavior behavior =
ManagedModeURLFilter::BehaviorFromInt(behavior_value);
io_url_filter_context_->SetDefaultFilteringBehavior(behavior);
ui_url_filter_context_->SetDefaultFilteringBehavior(behavior);
}
// Static
void ManagedMode::UpdateManualLists() {
GetInstance()->UpdateManualListsImpl();
}
void ManagedMode::UpdateManualListsImpl() {
io_url_filter_context_->LoadWhitelists(GetActiveSiteLists());
ui_url_filter_context_->LoadWhitelists(GetActiveSiteLists());
io_url_filter_context_->SetManualLists(GetWhitelist(), GetBlacklist());
ui_url_filter_context_->SetManualLists(GetWhitelist(), GetBlacklist());
}
scoped_ptr<base::ListValue> ManagedMode::GetWhitelist() {
return make_scoped_ptr(
managed_profile_->GetPrefs()->GetList(
prefs::kManagedModeWhitelist)->DeepCopy());
}
void ManagedMode::AddURLPatternToManualList(
bool is_whitelist,
const std::string& url_pattern) {
io_url_filter_context_->AddURLPatternToManualList(true, url_pattern);
ui_url_filter_context_->AddURLPatternToManualList(true, url_pattern);
}
|