Home | History | Annotate | Download | only in cloud
      1 // Copyright 2013 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 #ifndef COMPONENTS_POLICY_CORE_COMMON_CLOUD_POLICY_HEADER_IO_HELPER_H_
      6 #define COMPONENTS_POLICY_CORE_COMMON_CLOUD_POLICY_HEADER_IO_HELPER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/memory/ref_counted.h"
     11 #include "base/sequenced_task_runner.h"
     12 #include "components/policy/policy_export.h"
     13 #include "url/gurl.h"
     14 
     15 namespace net {
     16 class URLRequest;
     17 }
     18 
     19 namespace policy {
     20 
     21 // Helper class that lives on the I/O thread and adds policy headers to
     22 // outgoing requests. Instances of this class are created by
     23 // PolicyHeaderService on the UI thread, and that class is responsible for
     24 // notifying this class via UpdateHeaderFromUI() when the header changes.
     25 // Ownership is transferred to ProfileIOData, and this object is run and
     26 // destroyed on the I/O thread.
     27 class POLICY_EXPORT PolicyHeaderIOHelper {
     28  public:
     29   PolicyHeaderIOHelper(
     30       const std::string& server_url,
     31       const std::string& initial_header_value,
     32       const scoped_refptr<base::SequencedTaskRunner>& task_runner);
     33   ~PolicyHeaderIOHelper();
     34 
     35   // Sets any necessary policy headers for the specified URL on the passed
     36   // request. Should be invoked only from the I/O thread.
     37   void AddPolicyHeaders(const GURL& url,
     38                         net::URLRequest* request) const;
     39 
     40   // API invoked when the header changes. Can be called from any thread - calls
     41   // are marshalled via the TaskRunner to run on the appropriate thread.
     42   // If |new_header| is the empty string, no header will be added to
     43   // outgoing requests.
     44   void UpdateHeader(const std::string& new_header);
     45 
     46   // Test-only routine used to inject the server URL at runtime - this is
     47   // required because PolicyHeaderIOHelper is created very early in BrowserTest
     48   // initialization, before we startup the EmbeddedTestServer.
     49   void SetServerURLForTest(const std::string& server_url);
     50 
     51  private:
     52   // API invoked via the TaskRunner to update the header.
     53   void UpdateHeaderOnIOThread(const std::string& new_header);
     54 
     55   // Helper routine invoked via the TaskRunner to update the cached server URL.
     56   void SetServerURLOnIOThread(const std::string& new_header);
     57 
     58   // The URL we should add policy headers to.
     59   std::string server_url_;
     60 
     61   // The task runner assocated with the I/O thread that runs this object.
     62   scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
     63 
     64   // The current policy header value.
     65   std::string policy_header_;
     66 
     67   DISALLOW_COPY_AND_ASSIGN(PolicyHeaderIOHelper);
     68 };
     69 
     70 }  // namespace policy
     71 
     72 #endif  // COMPONENTS_POLICY_CORE_COMMON_CLOUD_POLICY_HEADER_IO_HELPER_H_
     73