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 #ifndef CHROME_BROWSER_POLICY_DEVICE_MANAGEMENT_SERVICE_H_
      6 #define CHROME_BROWSER_POLICY_DEVICE_MANAGEMENT_SERVICE_H_
      7 #pragma once
      8 
      9 #include <deque>
     10 #include <map>
     11 #include <string>
     12 
     13 #include "base/basictypes.h"
     14 #include "chrome/browser/policy/device_management_backend.h"
     15 #include "chrome/common/net/url_fetcher.h"
     16 #include "googleurl/src/gurl.h"
     17 
     18 class Profile;
     19 
     20 namespace net {
     21 class URLRequestContextGetter;
     22 }
     23 
     24 namespace policy {
     25 
     26 class DeviceManagementBackendImpl;
     27 
     28 // The device management service is responsible for everything related to
     29 // communication with the device management server. It creates the backends
     30 // objects that the device management policy provider and friends use to issue
     31 // requests.
     32 class DeviceManagementService : public URLFetcher::Delegate {
     33  public:
     34   // Describes a device management job handled by the service.
     35   class DeviceManagementJob {
     36    public:
     37     virtual ~DeviceManagementJob() {}
     38 
     39     // Handles the URL request response.
     40     virtual void HandleResponse(const net::URLRequestStatus& status,
     41                                 int response_code,
     42                                 const ResponseCookies& cookies,
     43                                 const std::string& data) = 0;
     44 
     45     // Gets the URL to contact.
     46     virtual GURL GetURL(const std::string& server_url) = 0;
     47 
     48     // Configures the fetcher, setting up payload and headers.
     49     virtual void ConfigureRequest(URLFetcher* fetcher) = 0;
     50   };
     51 
     52   explicit DeviceManagementService(const std::string& server_url);
     53   virtual ~DeviceManagementService();
     54 
     55   // Constructs a device management backend for use by client code. Ownership of
     56   // the returned backend object is transferred to the caller.
     57   // Marked virtual for the benefit of tests.
     58   virtual DeviceManagementBackend* CreateBackend();
     59 
     60   // Provides the backend with a request context so it can make actual network
     61   // requests. This will also fire any requests queued earlier.
     62   void Initialize(net::URLRequestContextGetter* request_context_getter);
     63 
     64   // Makes the service stop all requests and drop the reference to the request
     65   // context.
     66   void Shutdown();
     67 
     68   // Adds a job. Caller must make sure the job pointer stays valid until the job
     69   // completes or gets cancelled via RemoveJob().
     70   void AddJob(DeviceManagementJob* job);
     71 
     72   // Removes a job. The job will be removed and won't receive a completion
     73   // callback.
     74   void RemoveJob(DeviceManagementJob* job);
     75 
     76  private:
     77   typedef std::map<const URLFetcher*, DeviceManagementJob*> JobFetcherMap;
     78   typedef std::deque<DeviceManagementJob*> JobQueue;
     79 
     80   // Starts the given job.
     81   void StartJob(DeviceManagementJob* job);
     82 
     83   // URLFetcher::Delegate override.
     84   virtual void OnURLFetchComplete(const URLFetcher* source,
     85                                   const GURL& url,
     86                                   const net::URLRequestStatus& status,
     87                                   int response_code,
     88                                   const ResponseCookies& cookies,
     89                                   const std::string& data);
     90 
     91   // Server at which to contact the service.
     92   const std::string server_url_;
     93 
     94   // The request context we use.
     95   scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
     96 
     97   // The jobs we currently have in flight.
     98   JobFetcherMap pending_jobs_;
     99 
    100   // Jobs that are registered, but not started yet.
    101   JobQueue queued_jobs_;
    102 
    103   DISALLOW_COPY_AND_ASSIGN(DeviceManagementService);
    104 };
    105 
    106 }  // namespace policy
    107 
    108 #endif  // CHROME_BROWSER_POLICY_DEVICE_MANAGEMENT_SERVICE_H_
    109