Home | History | Annotate | Download | only in policy
      1 // Copyright (c) 2011 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/configuration_policy_loader_win.h"
      6 
      7 #include <userenv.h>
      8 
      9 #include "content/browser/browser_thread.h"
     10 
     11 namespace policy {
     12 
     13 ConfigurationPolicyLoaderWin::ConfigurationPolicyLoaderWin(
     14     AsynchronousPolicyProvider::Delegate* delegate,
     15     int reload_interval_minutes)
     16     : AsynchronousPolicyLoader(delegate, reload_interval_minutes),
     17       user_policy_changed_event_(false, false),
     18       machine_policy_changed_event_(false, false),
     19       user_policy_watcher_failed_(false),
     20       machine_policy_watcher_failed_(false) {
     21   if (!RegisterGPNotification(user_policy_changed_event_.handle(), false)) {
     22     PLOG(WARNING) << "Failed to register user group policy notification";
     23     user_policy_watcher_failed_ = true;
     24   }
     25   if (!RegisterGPNotification(machine_policy_changed_event_.handle(), true)) {
     26     PLOG(WARNING) << "Failed to register machine group policy notification.";
     27     machine_policy_watcher_failed_ = true;
     28   }
     29 }
     30 
     31 void ConfigurationPolicyLoaderWin::InitOnFileThread() {
     32   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
     33   AsynchronousPolicyLoader::InitOnFileThread();
     34   SetupWatches();
     35 }
     36 
     37 void ConfigurationPolicyLoaderWin::StopOnFileThread() {
     38   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
     39   user_policy_watcher_.StopWatching();
     40   machine_policy_watcher_.StopWatching();
     41   AsynchronousPolicyLoader::StopOnFileThread();
     42 }
     43 
     44 void ConfigurationPolicyLoaderWin::SetupWatches() {
     45   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
     46   CancelReloadTask();
     47 
     48   if (!user_policy_watcher_failed_ &&
     49       !user_policy_watcher_.GetWatchedObject() &&
     50       !user_policy_watcher_.StartWatching(
     51           user_policy_changed_event_.handle(), this)) {
     52     LOG(WARNING) << "Failed to start watch for user policy change event";
     53     user_policy_watcher_failed_ = true;
     54   }
     55   if (!machine_policy_watcher_failed_ &&
     56       !machine_policy_watcher_.GetWatchedObject() &&
     57       !machine_policy_watcher_.StartWatching(
     58           machine_policy_changed_event_.handle(), this)) {
     59     LOG(WARNING) << "Failed to start watch for machine policy change event";
     60     machine_policy_watcher_failed_ = true;
     61    }
     62 
     63   if (user_policy_watcher_failed_ || machine_policy_watcher_failed_)
     64     ScheduleFallbackReloadTask();
     65 }
     66 
     67 void ConfigurationPolicyLoaderWin::Reload() {
     68   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
     69   AsynchronousPolicyLoader::Reload();
     70   SetupWatches();
     71 }
     72 
     73 void ConfigurationPolicyLoaderWin::OnObjectSignaled(HANDLE object) {
     74   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
     75   DCHECK(object == user_policy_changed_event_.handle() ||
     76          object == machine_policy_changed_event_.handle())
     77       << "unexpected object signaled policy reload, obj = "
     78       << std::showbase << std::hex << object;
     79   Reload();
     80 }
     81 
     82 }  // namespace policy
     83