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 base::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 DisableReplaceExistingSource() OVERRIDE;
     46   virtual void DisableContentSecurityPolicy() OVERRIDE;
     47   virtual void OverrideContentSecurityPolicyObjectSrc(
     48       const std::string& data) OVERRIDE;
     49   virtual void OverrideContentSecurityPolicyFrameSrc(
     50       const std::string& data) OVERRIDE;
     51   virtual void DisableDenyXFrameOptions() OVERRIDE;
     52 
     53  protected:
     54   virtual ~WebUIDataSourceImpl();
     55 
     56   // Completes a request by sending our dictionary of localized strings.
     57   void SendLocalizedStringsAsJSON(
     58       const URLDataSource::GotDataCallback& callback);
     59 
     60   // Completes a request by sending the file specified by |idr|.
     61   void SendFromResourceBundle(
     62       const URLDataSource::GotDataCallback& callback, int idr);
     63 
     64  private:
     65   class InternalDataSource;
     66   friend class InternalDataSource;
     67   friend class WebUIDataSource;
     68   friend class WebUIDataSourceTest;
     69 
     70   explicit WebUIDataSourceImpl(const std::string& source_name);
     71 
     72   // Methods that match URLDataSource which are called by
     73   // InternalDataSource.
     74   std::string GetSource() const;
     75   std::string GetMimeType(const std::string& path) const;
     76   void StartDataRequest(
     77       const std::string& path,
     78       int render_process_id,
     79       int render_view_id,
     80       const URLDataSource::GotDataCallback& callback);
     81 
     82   void disable_set_font_strings_for_testing() {
     83     disable_set_font_strings_ = true;
     84   }
     85 
     86   // The name of this source.
     87   // E.g., for favicons, this could be "favicon", which results in paths for
     88   // specific resources like "favicon/34" getting sent to this source.
     89   std::string source_name_;
     90   int default_resource_;
     91   bool json_js_format_v2_;
     92   std::string json_path_;
     93   std::map<std::string, int> path_to_idr_map_;
     94   base::DictionaryValue localized_strings_;
     95   WebUIDataSource::HandleRequestCallback filter_callback_;
     96   bool add_csp_;
     97   bool object_src_set_;
     98   std::string object_src_;
     99   bool frame_src_set_;
    100   std::string frame_src_;
    101   bool deny_xframe_options_;
    102   bool disable_set_font_strings_;
    103   bool replace_existing_source_;
    104 
    105   DISALLOW_COPY_AND_ASSIGN(WebUIDataSourceImpl);
    106 };
    107 
    108 }  // content
    109 
    110 #endif  // CONTENT_BROWSER_WEBUI_WEB_UI_DATA_SOURCE_IMPL_H_
    111