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

Side by Side Diff: chrome/browser/policy/async_policy_provider.cc

Issue 10448118: Add the AsyncPolicyProvider and AsyncPolicyLoader (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix unittest Created 8 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/policy/async_policy_provider.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "chrome/browser/policy/async_policy_loader.h"
10 #include "chrome/browser/policy/policy_bundle.h"
11 #include "content/public/browser/browser_thread.h"
12
13 using content::BrowserThread;
14
15 namespace policy {
16
17 namespace {
18
19 // Helper for a PostTaskAndReply used as a synchronization point between the
20 // UI and FILE threads. See AsyncPolicyProvider::RefreshPolicies.
21 void Nop() {}
22
23 } // namespace
24
25 AsyncPolicyProvider::AsyncPolicyProvider(
26 const PolicyDefinitionList* policy_list,
27 AsyncPolicyLoader* loader)
28 : ConfigurationPolicyProvider(policy_list),
29 loader_(loader),
30 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
31 pending_refreshes_(0) {
32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
Mattias Nissler (ping if slow) 2012/06/04 08:48:26 Rather use base::threading::NonThreadSafe? If you
Joao da Silva 2012/06/04 14:05:06 Done. This also required passing the MessageLoop o
33 // The FILE thread isn't ready early during startup. Post a task to UI to
34 // resume initialization on FILE once the loops are spinning.
35 BrowserThread::PostTask(
36 BrowserThread::UI, FROM_HERE,
37 base::Bind(&AsyncPolicyProvider::InitWithLoopsReady,
38 weak_factory_.GetWeakPtr()));
39 // Make an immediate synchronous load on startup.
40 OnLoaderReloaded(loader->InitialLoad());
41 }
42
43 AsyncPolicyProvider::~AsyncPolicyProvider() {
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
45 // Note on the lifetime of |loader_|:
46 // The |loader_| lives on the FILE thread, and is deleted from here. This
47 // means that posting tasks on the |loader_| to FILE from the
48 // AsyncPolicyProvider is always safe, since a potential DeleteSoon() is only
49 // posted from here. The |loader_| posts back to the AsyncPolicyProvider
50 // through the |update_callback_| on UI, which has a WeakPtr to |this|.
51 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, loader_);
52 }
53
54 void AsyncPolicyProvider::RefreshPolicies() {
55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
56 pending_refreshes_++;
57 // Subtle: RefreshPolicies() has a contract that requires the next policy
58 // update notification (triggered from UpdatePolicy()) to reflect any changes
59 // made before this call. So if a test has modified the policy settings and
Mattias Nissler (ping if slow) 2012/06/04 08:48:26 Seems like this doesn't only apply to tests.
Joao da Silva 2012/06/04 14:05:06 s/test/caller
60 // invoked RefreshPolicies(), then by the next notification these policies
61 // should already be provided.
62 // However, it's also possible that an asynchronous Reload() is in progress
63 // and just posted OnLoaderReloaded() to UI. Therefore a task is posted to
64 // FILE before posting the next Reload, to prevent a potential concurrent
65 // Reload() from triggered a notification too early.
Mattias Nissler (ping if slow) 2012/06/04 08:48:26 s/triggered/triggering/
Joao da Silva 2012/06/04 14:05:06 Done.
66 BrowserThread::PostTaskAndReply(
67 BrowserThread::FILE, FROM_HERE,
68 base::Bind(Nop),
69 base::Bind(&AsyncPolicyProvider::PostRefreshingReload,
70 weak_factory_.GetWeakPtr()));
71 }
72
73 void AsyncPolicyProvider::InitWithLoopsReady() {
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
75 BrowserThread::PostTask(
76 BrowserThread::FILE, FROM_HERE,
77 base::Bind(&AsyncPolicyLoader::Init,
78 base::Unretained(loader_),
79 base::Bind(&AsyncPolicyProvider::OnLoaderReloaded,
80 weak_factory_.GetWeakPtr())));
81 }
82
83 void AsyncPolicyProvider::PostRefreshingReload() {
Mattias Nissler (ping if slow) 2012/06/04 08:48:26 The name of this function is not very descriptive,
Joao da Silva 2012/06/04 14:05:06 Done.
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
85 DCHECK(pending_refreshes_ > 0);
86 pending_refreshes_--;
Mattias Nissler (ping if slow) 2012/06/04 08:48:26 Ah, counters. In this case, it's probably not an i
Joao da Silva 2012/06/04 14:05:06 Redone with a CancellableCallback as suggested off
87 if (pending_refreshes_ == 0) {
88 BrowserThread::PostTask(
89 BrowserThread::FILE, FROM_HERE,
90 base::Bind(&AsyncPolicyLoader::Reload,
91 base::Unretained(loader_),
92 true /* force */));
93 }
94 }
95
96 void AsyncPolicyProvider::OnLoaderReloaded(scoped_ptr<PolicyBundle> bundle) {
97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
98 if (pending_refreshes_ == 0)
99 UpdatePolicy(bundle.Pass());
100 }
101
102 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698