Home | History | Annotate | Download | only in content_settings
      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 CHROME_BROWSER_UI_CONTENT_SETTINGS_CONTENT_SETTING_BUBBLE_MODEL_H_
      6 #define CHROME_BROWSER_UI_CONTENT_SETTINGS_CONTENT_SETTING_BUBBLE_MODEL_H_
      7 
      8 #include <map>
      9 #include <set>
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "base/compiler_specific.h"
     14 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
     15 #include "chrome/common/content_settings.h"
     16 #include "chrome/common/custom_handlers/protocol_handler.h"
     17 #include "content/public/browser/notification_observer.h"
     18 #include "content/public/browser/notification_registrar.h"
     19 #include "content/public/common/media_stream_request.h"
     20 #include "ui/gfx/image/image.h"
     21 #include "url/gurl.h"
     22 
     23 class ContentSettingBubbleModelDelegate;
     24 class Profile;
     25 class ProtocolHandlerRegistry;
     26 
     27 namespace content {
     28 class WebContents;
     29 }
     30 
     31 // This model provides data for ContentSettingBubble, and also controls
     32 // the action triggered when the allow / block radio buttons are triggered.
     33 class ContentSettingBubbleModel : public content::NotificationObserver {
     34  public:
     35   typedef ContentSettingBubbleModelDelegate Delegate;
     36 
     37   struct PopupItem {
     38     PopupItem(const gfx::Image& image, const std::string& title, int32 popup_id)
     39         : image(image), title(title), popup_id(popup_id) {}
     40 
     41     gfx::Image image;
     42     std::string title;
     43     int32 popup_id;
     44   };
     45   typedef std::vector<PopupItem> PopupItems;
     46 
     47   typedef std::vector<std::string> RadioItems;
     48   struct RadioGroup {
     49     RadioGroup();
     50     ~RadioGroup();
     51 
     52     GURL url;
     53     std::string title;
     54     RadioItems radio_items;
     55     int default_item;
     56   };
     57 
     58   struct DomainList {
     59     DomainList();
     60     ~DomainList();
     61 
     62     std::string title;
     63     std::set<std::string> hosts;
     64   };
     65 
     66   struct MediaMenu {
     67     MediaMenu();
     68     ~MediaMenu();
     69 
     70     std::string label;
     71     content::MediaStreamDevice default_device;
     72     content::MediaStreamDevice selected_device;
     73     bool disabled;
     74   };
     75   typedef std::map<content::MediaStreamType, MediaMenu> MediaMenuMap;
     76 
     77   struct BubbleContent {
     78     BubbleContent();
     79     ~BubbleContent();
     80 
     81     std::string title;
     82     PopupItems popup_items;
     83     RadioGroup radio_group;
     84     bool radio_group_enabled;
     85     std::vector<DomainList> domain_lists;
     86     std::string custom_link;
     87     bool custom_link_enabled;
     88     std::string manage_link;
     89     MediaMenuMap media_menus;
     90 
     91    private:
     92     DISALLOW_COPY_AND_ASSIGN(BubbleContent);
     93   };
     94 
     95   static ContentSettingBubbleModel* CreateContentSettingBubbleModel(
     96       Delegate* delegate,
     97       content::WebContents* web_contents,
     98       Profile* profile,
     99       ContentSettingsType content_type);
    100 
    101   virtual ~ContentSettingBubbleModel();
    102 
    103   ContentSettingsType content_type() const { return content_type_; }
    104 
    105   const BubbleContent& bubble_content() const { return bubble_content_; }
    106 
    107   // content::NotificationObserver:
    108   virtual void Observe(int type,
    109                        const content::NotificationSource& source,
    110                        const content::NotificationDetails& details) OVERRIDE;
    111 
    112   virtual void OnRadioClicked(int radio_index) {}
    113   virtual void OnPopupClicked(int index) {}
    114   virtual void OnCustomLinkClicked() {}
    115   virtual void OnManageLinkClicked() {}
    116   virtual void OnMediaMenuClicked(content::MediaStreamType type,
    117                                   const std::string& selected_device_id) {}
    118 
    119   // Called by the view code when the bubble is closed by the user using the
    120   // Done button.
    121   virtual void OnDoneClicked() {}
    122 
    123  protected:
    124   ContentSettingBubbleModel(
    125       content::WebContents* web_contents,
    126       Profile* profile,
    127       ContentSettingsType content_type);
    128 
    129   content::WebContents* web_contents() const { return web_contents_; }
    130   Profile* profile() const { return profile_; }
    131 
    132   void set_title(const std::string& title) { bubble_content_.title = title; }
    133   void add_popup(const PopupItem& popup) {
    134     bubble_content_.popup_items.push_back(popup);
    135   }
    136   void set_radio_group(const RadioGroup& radio_group) {
    137     bubble_content_.radio_group = radio_group;
    138   }
    139   void set_radio_group_enabled(bool enabled) {
    140     bubble_content_.radio_group_enabled = enabled;
    141   }
    142   void add_domain_list(const DomainList& domain_list) {
    143     bubble_content_.domain_lists.push_back(domain_list);
    144   }
    145   void set_custom_link(const std::string& link) {
    146     bubble_content_.custom_link = link;
    147   }
    148   void set_custom_link_enabled(bool enabled) {
    149     bubble_content_.custom_link_enabled = enabled;
    150   }
    151   void set_manage_link(const std::string& link) {
    152     bubble_content_.manage_link = link;
    153   }
    154   void add_media_menu(content::MediaStreamType type, const MediaMenu& menu) {
    155     bubble_content_.media_menus[type] = menu;
    156   }
    157   void set_selected_device(const content::MediaStreamDevice& device) {
    158     bubble_content_.media_menus[device.type].selected_device = device;
    159   }
    160 
    161  private:
    162   content::WebContents* web_contents_;
    163   Profile* profile_;
    164   ContentSettingsType content_type_;
    165   BubbleContent bubble_content_;
    166   // A registrar for listening for WEB_CONTENTS_DESTROYED notifications.
    167   content::NotificationRegistrar registrar_;
    168 
    169   DISALLOW_COPY_AND_ASSIGN(ContentSettingBubbleModel);
    170 };
    171 
    172 class ContentSettingTitleAndLinkModel : public ContentSettingBubbleModel {
    173  public:
    174   ContentSettingTitleAndLinkModel(Delegate* delegate,
    175                                   content::WebContents* web_contents,
    176                                   Profile* profile,
    177                                   ContentSettingsType content_type);
    178   virtual ~ContentSettingTitleAndLinkModel() {}
    179   Delegate* delegate() const { return delegate_; }
    180 
    181  private:
    182   void SetTitle();
    183   void SetManageLink();
    184   virtual void OnManageLinkClicked() OVERRIDE;
    185 
    186   Delegate* delegate_;
    187 };
    188 
    189 class ContentSettingRPHBubbleModel : public ContentSettingTitleAndLinkModel {
    190  public:
    191   ContentSettingRPHBubbleModel(Delegate* delegate,
    192                                content::WebContents* web_contents,
    193                                Profile* profile,
    194                                ProtocolHandlerRegistry* registry,
    195                                ContentSettingsType content_type);
    196 
    197   virtual void OnRadioClicked(int radio_index) OVERRIDE;
    198   virtual void OnDoneClicked() OVERRIDE;
    199 
    200  private:
    201   // These states must match the order of appearance of the radio buttons
    202   // in the XIB file for the Mac port.
    203   enum RPHState {
    204     RPH_ALLOW = 0,
    205     RPH_BLOCK,
    206     RPH_IGNORE,
    207   };
    208 
    209   void RegisterProtocolHandler();
    210   void UnregisterProtocolHandler();
    211   void IgnoreProtocolHandler();
    212   void ClearOrSetPreviousHandler();
    213 
    214   int selected_item_;
    215   ProtocolHandlerRegistry* registry_;
    216   ProtocolHandler pending_handler_;
    217   ProtocolHandler previous_handler_;
    218 };
    219 
    220 #endif  // CHROME_BROWSER_UI_CONTENT_SETTINGS_CONTENT_SETTING_BUBBLE_MODEL_H_
    221