summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/file_based_policy_loader.cc
blob: 6ceea3e003959e7cef0c678271c3d3430c12a52e (plain)
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
// 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/policy/file_based_policy_loader.h"

#include "base/files/file_path_watcher.h"
#include "base/memory/ref_counted.h"
#include "chrome/browser/policy/policy_map.h"
#include "content/public/browser/browser_thread.h"

using ::base::files::FilePathWatcher;
using content::BrowserThread;

namespace {

// Amount of time we wait for the files on disk to settle before trying to load
// them. This alleviates the problem of reading partially written files and
// makes it possible to batch quasi-simultaneous changes.
const int kSettleIntervalSeconds = 5;

// The time interval for rechecking policy. This is our fallback in case the
// delegate never reports a change to the ReloadObserver.
const int kReloadIntervalMinutes = 15;

}  // namespace

namespace policy {

FileBasedPolicyLoader::FileBasedPolicyLoader(
    FileBasedPolicyProvider::ProviderDelegate* provider_delegate)
    : AsynchronousPolicyLoader(provider_delegate,
                               kReloadIntervalMinutes),
      config_file_path_(provider_delegate->config_file_path()),
      settle_interval_(base::TimeDelta::FromSeconds(kSettleIntervalSeconds)) {
}

FileBasedPolicyLoader::~FileBasedPolicyLoader() {}

class FileBasedPolicyWatcherDelegate : public FilePathWatcher::Delegate {
 public:
  explicit FileBasedPolicyWatcherDelegate(
      scoped_refptr<FileBasedPolicyLoader> loader)
      : loader_(loader) {}
  virtual ~FileBasedPolicyWatcherDelegate() {}

  // FilePathWatcher::Delegate implementation:
  virtual void OnFilePathChanged(const FilePath& path) OVERRIDE {
    loader_->OnFilePathChanged(path);
  }

  virtual void OnFilePathError(const FilePath& path) OVERRIDE {
    loader_->OnFilePathError(path);
  }

 private:
  scoped_refptr<FileBasedPolicyLoader> loader_;
  DISALLOW_COPY_AND_ASSIGN(FileBasedPolicyWatcherDelegate);
};

void FileBasedPolicyLoader::OnFilePathChanged(
    const FilePath& path) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
  Reload(false);
}

void FileBasedPolicyLoader::OnFilePathError(const FilePath& path) {
  LOG(ERROR) << "FilePathWatcher on " << path.value()
             << " failed.";
}

void FileBasedPolicyLoader::Reload(bool force) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));

  if (!delegate()) {
    PostUpdatePolicyTask(NULL);
    return;
  }

  // Check the directory time in order to see whether a reload is required.
  base::TimeDelta delay;
  base::Time now = base::Time::Now();
  if (!force && !IsSafeToReloadPolicy(now, &delay)) {
    ScheduleReloadTask(delay);
    return;
  }

  // Load the policy definitions.
  scoped_ptr<PolicyMap> new_policy(delegate()->Load());

  // Check again in case the directory has changed while reading it.
  if (!force && !IsSafeToReloadPolicy(now, &delay)) {
    ScheduleReloadTask(delay);
    return;
  }

  PostUpdatePolicyTask(new_policy.release());

  ScheduleFallbackReloadTask();
}

void FileBasedPolicyLoader::InitOnFileThread() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
  watcher_.reset(new FilePathWatcher);
  const FilePath& path = config_file_path();
  if (!path.empty() &&
      !watcher_->Watch(path, new FileBasedPolicyWatcherDelegate(this))) {
    OnFilePathError(path);
  }

  // There might have been changes to the directory in the time between
  // construction of the loader and initialization of the watcher. Call reload
  // to detect if that is the case.
  Reload(false);

  ScheduleFallbackReloadTask();
}

void FileBasedPolicyLoader::StopOnFileThread() {
  watcher_.reset();
  AsynchronousPolicyLoader::StopOnFileThread();
}

bool FileBasedPolicyLoader::IsSafeToReloadPolicy(
    const base::Time& now,
    base::TimeDelta* delay) {
  DCHECK(delay);

  // A null modification time indicates there's no data.
  FileBasedPolicyProvider::ProviderDelegate* provider_delegate =
      static_cast<FileBasedPolicyProvider::ProviderDelegate*>(delegate());
  base::Time last_modification(provider_delegate->GetLastModification());
  if (last_modification.is_null())
    return true;

  // If there was a change since the last recorded modification, wait some more.
  if (last_modification != last_modification_file_) {
    last_modification_file_ = last_modification;
    last_modification_clock_ = now;
    *delay = settle_interval_;
    return false;
  }

  // Check whether the settle interval has elapsed.
  base::TimeDelta age = now - last_modification_clock_;
  if (age < settle_interval_) {
    *delay = settle_interval_ - age;
    return false;
  }

  return true;
}

}  // namespace policy