Home | History | Annotate | Download | only in web_resource
      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_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_
      6 #define CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_
      7 #pragma once
      8 
      9 #include <string>
     10 
     11 #include "chrome/browser/utility_process_host.h"
     12 #include "content/common/notification_type.h"
     13 
     14 class PrefService;
     15 class Profile;
     16 class ResourceDispatcherHost;
     17 
     18 // A WebResourceService fetches data from a web resource server and store
     19 // locally as user preference.
     20 class WebResourceService
     21     : public UtilityProcessHost::Client {
     22  public:
     23   // Pass notification_type = NOTIFICATION_TYPE_COUNT if notification is not
     24   // required.
     25   WebResourceService(Profile* profile,
     26                      PrefService* prefs,
     27                      const char* web_resource_server,
     28                      bool apply_locale_to_url_,
     29                      NotificationType::Type notification_type,
     30                      const char* last_update_time_pref_name,
     31                      int start_fetch_delay,
     32                      int cache_update_delay);
     33 
     34   // Sleep until cache needs to be updated, but always for at least 5 seconds
     35   // so we don't interfere with startup.  Then begin updating resources.
     36   void StartAfterDelay();
     37 
     38   // We have successfully pulled data from a resource server; now launch
     39   // the process that will parse the JSON, and then update the cache.
     40   void UpdateResourceCache(const std::string& json_data);
     41 
     42  protected:
     43   virtual ~WebResourceService();
     44 
     45   virtual void Unpack(const DictionaryValue& parsed_json) = 0;
     46 
     47   // If delay_ms is positive, schedule notification with the delay.
     48   // If delay_ms is 0, notify immediately by calling WebResourceStateChange().
     49   // If delay_ms is negative, do nothing.
     50   void PostNotification(int64 delay_ms);
     51 
     52   // We need to be able to load parsed resource data into preferences file,
     53   // and get proper install directory.
     54   PrefService* prefs_;
     55 
     56   Profile* profile_;
     57 
     58  private:
     59   class WebResourceFetcher;
     60   friend class WebResourceFetcher;
     61 
     62   class UnpackerClient;
     63 
     64   // Set in_fetch_ to false, clean up temp directories (in the future).
     65   void EndFetch();
     66 
     67   // Puts parsed json data in the right places, and writes to prefs file.
     68   void OnWebResourceUnpacked(const DictionaryValue& parsed_json);
     69 
     70   // Notify listeners that the state of a web resource has changed.
     71   void WebResourceStateChange();
     72 
     73   scoped_ptr<WebResourceFetcher> web_resource_fetcher_;
     74 
     75   ResourceDispatcherHost* resource_dispatcher_host_;
     76 
     77   // Allows the creation of tasks to send a WEB_RESOURCE_STATE_CHANGED
     78   // notification. This allows the WebResourceService to notify the New Tab
     79   // Page immediately when a new web resource should be shown or removed.
     80   ScopedRunnableMethodFactory<WebResourceService> service_factory_;
     81 
     82   // True if we are currently mid-fetch.  If we are asked to start a fetch
     83   // when we are still fetching resource data, schedule another one in
     84   // kCacheUpdateDelay time, and silently exit.
     85   bool in_fetch_;
     86 
     87   // URL that hosts the web resource.
     88   const char* web_resource_server_;
     89 
     90   // Indicates whether we should append locale to the web resource server URL.
     91   bool apply_locale_to_url_;
     92 
     93   // Notification type when an update is done.
     94   NotificationType::Type notification_type_;
     95 
     96   // Pref name to store the last update's time.
     97   const char* last_update_time_pref_name_;
     98 
     99   // Delay on first fetch so we don't interfere with startup.
    100   int start_fetch_delay_;
    101 
    102   // Delay between calls to update the web resource cache. This delay may be
    103   // different for different builds of Chrome.
    104   int cache_update_delay_;
    105 
    106   // True if a task has been set to update the cache when a new web resource
    107   // becomes available.
    108   bool web_resource_update_scheduled_;
    109 
    110   DISALLOW_COPY_AND_ASSIGN(WebResourceService);
    111 };
    112 
    113 #endif  // CHROME_BROWSER_WEB_RESOURCE_WEB_RESOURCE_SERVICE_H_
    114