Chromium Code Reviews| Index: components/policy/core/browser/policy_header_io_helper.cc |
| diff --git a/components/policy/core/browser/policy_header_io_helper.cc b/components/policy/core/browser/policy_header_io_helper.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e1680189f71cc1e7951051ef3b907c19ac944bfc |
| --- /dev/null |
| +++ b/components/policy/core/browser/policy_header_io_helper.cc |
| @@ -0,0 +1,60 @@ |
| +// Copyright (c) 2013 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 "components/policy/core/browser/policy_header_io_helper.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/location.h" |
| +#include "base/sequenced_task_runner.h" |
| +#include "net/url_request/url_request.h" |
| + |
| +namespace { |
| + |
| +// The name of the header containing the policy information. |
| +const char kChromePolicyHeader[] = "Chrome-Policy-Posture"; |
| + |
| +} // namespace |
| + |
| +namespace policy { |
| + |
| +PolicyHeaderIOHelper::PolicyHeaderIOHelper( |
| + const std::string& server_url, |
| + const std::string& initial_header_value, |
| + scoped_refptr<base::SequencedTaskRunner> task_runner) |
| + : server_url_(server_url), |
| + task_runner_(task_runner), |
| + policy_header_(initial_header_value), |
| + weak_factory_(this) { |
| +} |
| + |
| +PolicyHeaderIOHelper::~PolicyHeaderIOHelper() { |
| +} |
| + |
| +// Sets any necessary policy headers on the passed request. |
| +void PolicyHeaderIOHelper::AddPolicyHeaders(net::URLRequest* request) const { |
| + DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| + const GURL& url = request->url(); |
| + if (!policy_header_.empty() && |
| + url.spec().compare(0, server_url_.size(), server_url_) == 0) { |
| + request->SetExtraRequestHeaderByName(kChromePolicyHeader, |
| + policy_header_, |
| + true /* overwrite */); |
| + } |
| +} |
| + |
| +void PolicyHeaderIOHelper::UpdateHeader(const std::string& new_header) { |
| + // Post a task to the IO thread to modify this. |
| + task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&PolicyHeaderIOHelper::UpdateHeaderOnTaskRunner, |
| + weak_factory_.GetWeakPtr(), new_header)); |
|
willchan no longer on Chromium
2013/12/10 23:27:09
Is this correct? It looks like the WeakPtr is acqu
Andrew T Wilson (Slow)
2013/12/10 23:53:06
Done. Makes sense to me.
|
| +} |
| + |
| +void PolicyHeaderIOHelper::UpdateHeaderOnTaskRunner( |
| + const std::string& new_header) { |
| + DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| + policy_header_ = new_header; |
| +} |
| + |
| +} // namespace policy |