Home | History | Annotate | Download | only in web_resource
      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 CHROME_BROWSER_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_
      6 #define CHROME_BROWSER_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_
      7 
      8 #include "chrome/browser/web_resource/eula_accepted_notifier.h"
      9 #include "net/base/network_change_notifier.h"
     10 
     11 // This class informs an interested observer when resource requests over the
     12 // network are permitted.
     13 //
     14 // Currently, the criteria for allowing resource requests are:
     15 //  1. The network is currently available,
     16 //  2. The EULA was accepted by the user (ChromeOS only), and
     17 //  3. The --disable-background-networking command line switch is not set.
     18 //
     19 // Interested services should add themselves as an observer of
     20 // ResourceRequestAllowedNotifier and check ResourceRequestsAllowed() to see if
     21 // requests are permitted. If it returns true, they can go ahead and make their
     22 // request. If it returns false, ResourceRequestAllowedNotifier will notify the
     23 // service when the criteria is met.
     24 //
     25 // If ResourceRequestsAllowed returns true the first time,
     26 // ResourceRequestAllowedNotifier will not notify the service in the future.
     27 //
     28 // Note that this class handles the criteria state for a single service, so
     29 // services should keep their own instance of this class rather than sharing a
     30 // global instance.
     31 class ResourceRequestAllowedNotifier
     32     : public EulaAcceptedNotifier::Observer,
     33       public net::NetworkChangeNotifier::ConnectionTypeObserver {
     34  public:
     35   // Observes resource request allowed state changes.
     36   class Observer {
     37    public:
     38     virtual void OnResourceRequestsAllowed() = 0;
     39   };
     40 
     41   // Specifies the resource request allowed state.
     42   enum State {
     43     ALLOWED,
     44     DISALLOWED_EULA_NOT_ACCEPTED,
     45     DISALLOWED_NETWORK_DOWN,
     46     DISALLOWED_COMMAND_LINE_DISABLED,
     47   };
     48 
     49   ResourceRequestAllowedNotifier();
     50   virtual ~ResourceRequestAllowedNotifier();
     51 
     52   // Sets |observer| as the service to be notified by this instance, and
     53   // performs initial checks on the criteria. |observer| may not be NULL.
     54   // This is to be called immediately after construction of an instance of
     55   // ResourceRequestAllowedNotifier to pass it the interested service.
     56   void Init(Observer* observer);
     57 
     58   // Returns whether resource requests are allowed, per the various criteria.
     59   // If not, this call will set some flags so it knows to notify the observer
     60   // if the criteria change. Note that the observer will not be notified unless
     61   // it calls this method first.
     62   // This is virtual so it can be overridden for tests.
     63   virtual State GetResourceRequestsAllowedState();
     64 
     65   // Convenience function, equivalent to:
     66   //   GetResourceRequestsAllowedState() == ALLOWED.
     67   bool ResourceRequestsAllowed();
     68 
     69   void SetWaitingForNetworkForTesting(bool waiting);
     70   void SetWaitingForEulaForTesting(bool waiting);
     71   void SetObserverRequestedForTesting(bool requested);
     72 
     73  protected:
     74   // Notifies the observer if all criteria needed for resource requests are met.
     75   // This is protected so it can be called from subclasses for testing.
     76   void MaybeNotifyObserver();
     77 
     78  private:
     79   // Creates the EulaAcceptNotifier or NULL if one is not needed. Virtual so
     80   // that it can be overridden by test subclasses.
     81   virtual EulaAcceptedNotifier* CreateEulaNotifier();
     82 
     83   // EulaAcceptedNotifier::Observer overrides:
     84   virtual void OnEulaAccepted() OVERRIDE;
     85 
     86   // net::NetworkChangeNotifier::ConnectionTypeObserver overrides:
     87   virtual void OnConnectionTypeChanged(
     88       net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
     89 
     90   // Tracks whether or not the observer/service depending on this class actually
     91   // requested permission to make a request or not. If it did not, then this
     92   // class should not notify it even if the criteria is met.
     93   bool observer_requested_permission_;
     94 
     95   // Tracks network connectivity criteria.
     96   bool waiting_for_network_;
     97 
     98   // Tracks EULA acceptance criteria.
     99   bool waiting_for_user_to_accept_eula_;
    100 
    101   // Platform-specific notifier of EULA acceptance, or NULL if not needed.
    102   scoped_ptr<EulaAcceptedNotifier> eula_notifier_;
    103 
    104   // Observing service interested in request permissions.
    105   Observer* observer_;
    106 
    107   DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifier);
    108 };
    109 
    110 #endif  // CHROME_BROWSER_WEB_RESOURCE_RESOURCE_REQUEST_ALLOWED_NOTIFIER_H_
    111