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