Home | History | Annotate | Download | only in webui
      1 // Copyright (c) 2012 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 CONTENT_BROWSER_WEBUI_WEB_UI_DATA_SOURCE_IMPL_H_
      6 #define CONTENT_BROWSER_WEBUI_WEB_UI_DATA_SOURCE_IMPL_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/callback.h"
     13 #include "base/compiler_specific.h"
     14 #include "base/values.h"
     15 #include "content/browser/webui/url_data_manager.h"
     16 #include "content/browser/webui/url_data_source_impl.h"
     17 #include "content/common/content_export.h"
     18 #include "content/public/browser/url_data_source.h"
     19 #include "content/public/browser/web_ui_data_source.h"
     20 
     21 namespace content {
     22 
     23 // A data source that can help with implementing the common operations
     24 // needed by the chrome WEBUI settings/history/downloads pages.
     25 class CONTENT_EXPORT WebUIDataSourceImpl
     26     : public NON_EXPORTED_BASE(URLDataSourceImpl),
     27       public NON_EXPORTED_BASE(WebUIDataSource) {
     28  public:
     29   // WebUIDataSource implementation:
     30   virtual void AddString(const std::string& name,
     31                          const string16& value) OVERRIDE;
     32   virtual void AddString(const std::string& name,
     33                          const std::string& value) OVERRIDE;
     34   virtual void AddLocalizedString(const std::string& name, int ids) OVERRIDE;
     35   virtual void AddLocalizedStrings(
     36       const base::DictionaryValue& localized_strings) OVERRIDE;
     37   virtual void AddBoolean(const std::string& name, bool value) OVERRIDE;
     38   virtual void SetJsonPath(const std::string& path) OVERRIDE;
     39   virtual void SetUseJsonJSFormatV2() OVERRIDE;
     40   virtual void AddResourcePath(const std::string &path,
     41                                int resource_id) OVERRIDE;
     42   virtual void SetDefaultResource(int resource_id) OVERRIDE;
     43   virtual void SetRequestFilter(
     44       const WebUIDataSource::HandleRequestCallback& callback) OVERRIDE;
     45   virtual void DisableContentSecurityPolicy() OVERRIDE;
     46   virtual void OverrideContentSecurityPolicyObjectSrc(
     47       const std::string& data) OVERRIDE;
     48   virtual void OverrideContentSecurityPolicyFrameSrc(
     49       const std::string& data) OVERRIDE;
     50   virtual void DisableDenyXFrameOptions() OVERRIDE;
     51 
     52  protected:
     53   virtual ~WebUIDataSourceImpl();
     54 
     55   // Completes a request by sending our dictionary of localized strings.
     56   void SendLocalizedStringsAsJSON(
     57       const URLDataSource::GotDataCallback& callback);
     58 
     59   // Completes a request by sending the file specified by |idr|.
     60   void SendFromResourceBundle(
     61       const URLDataSource::GotDataCallback& callback, int idr);
     62 
     63  private:
     64   class InternalDataSource;
     65   friend class InternalDataSource;
     66   friend class WebUIDataSource;
     67   friend class WebUIDataSourceTest;
     68 
     69   explicit WebUIDataSourceImpl(const std::string& source_name);
     70 
     71   // Methods that match URLDataSource which are called by
     72   // InternalDataSource.
     73   std::string GetSource() const;
     74   std::string GetMimeType(const std::string& path) const;
     75   void StartDataRequest(
     76       const std::string& path,
     77       int render_process_id,
     78       int render_view_id,
     79       const URLDataSource::GotDataCallback& callback);
     80 
     81   void disable_set_font_strings_for_testing() {
     82     disable_set_font_strings_ = true;
     83   }
     84 
     85   // The name of this source.
     86   // E.g., for favicons, this could be "favicon", which results in paths for
     87   // specific resources like "favicon/34" getting sent to this source.
     88   std::string source_name_;
     89   int default_resource_;
     90   bool json_js_format_v2_;
     91   std::string json_path_;
     92   std::map<std::string, int> path_to_idr_map_;
     93   base::DictionaryValue localized_strings_;
     94   WebUIDataSource::HandleRequestCallback filter_callback_;
     95   bool add_csp_;
     96   bool object_src_set_;
     97   std::string object_src_;
     98   bool frame_src_set_;
     99   std::string frame_src_;
    100   bool deny_xframe_options_;
    101   bool disable_set_font_strings_;
    102 
    103   DISALLOW_COPY_AND_ASSIGN(WebUIDataSourceImpl);
    104 };
    105 
    106 }  // content
    107 
    108 #endif  // CONTENT_BROWSER_WEBUI_WEB_UI_DATA_SOURCE_IMPL_H_
    109