OLD | NEW |
(Empty) | |
| 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 #include "chrome/browser/garbled_text_helper.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "chrome/browser/garbled_text_infobar_delegate.h" |
| 9 #include "chrome/browser/garbled_text_service.h" |
| 10 #include "chrome/browser/infobars/infobar_tab_helper.h" |
| 11 #include "chrome/browser/prefs/pref_service.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 14 #include "chrome/common/pref_names.h" |
| 15 #include "chrome/common/render_messages.h" |
| 16 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "content/browser/renderer_host/render_view_host.h" |
| 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/web_contents.h" |
| 20 #include "ipc/ipc_message_macros.h" |
| 21 #include "content/public/browser/notification_details.h" |
| 22 #include "content/public/browser/notification_service.h" |
| 23 |
| 24 GarbledTextHelper::GarbledTextHelper(content::WebContents* web_contents) |
| 25 : WebContentsObserver(web_contents) { |
| 26 pref_change_registrar_.Init(GetProfile()->GetPrefs()); |
| 27 pref_change_registrar_.Add(prefs::kEnableAutoGarbledTextFix, this); |
| 28 } |
| 29 |
| 30 GarbledTextHelper::~GarbledTextHelper() { |
| 31 } |
| 32 |
| 33 Profile* GarbledTextHelper::GetProfile() { |
| 34 return Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 35 } |
| 36 |
| 37 bool GarbledTextHelper::OnMessageReceived(const IPC::Message& message) { |
| 38 bool handled = true; |
| 39 IPC_BEGIN_MESSAGE_MAP(GarbledTextHelper, message) |
| 40 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_GarbledTextDetected, |
| 41 OnGarbledTextDetected) |
| 42 IPC_MESSAGE_UNHANDLED(handled = false) |
| 43 IPC_END_MESSAGE_MAP() |
| 44 |
| 45 return handled; |
| 46 } |
| 47 |
| 48 void GarbledTextHelper::DidNavigateAnyFrame( |
| 49 const content::LoadCommittedDetails& details, |
| 50 const content::FrameNavigateParams& params) { |
| 51 OnEnableStateChanged( |
| 52 GetProfile()->GetPrefs()->GetBoolean( |
| 53 prefs::kEnableAutoGarbledTextFix)); |
| 54 } |
| 55 |
| 56 void GarbledTextHelper::RenderViewCreated(RenderViewHost* render_view_host) { |
| 57 OnEnableStateChanged( |
| 58 GetProfile()->GetPrefs()->GetBoolean( |
| 59 prefs::kEnableAutoGarbledTextFix)); |
| 60 } |
| 61 |
| 62 void GarbledTextHelper::Observe(int type, |
| 63 const content::NotificationSource& source, |
| 64 const content::NotificationDetails& details) { |
| 65 switch (type) { |
| 66 case chrome::NOTIFICATION_PREF_CHANGED: { |
| 67 std::string* pref_name = content::Details<std::string>(details).ptr(); |
| 68 PrefService* user_prefs = content::Source<PrefService>(source).ptr(); |
| 69 DCHECK(user_prefs == GetProfile()->GetPrefs()); |
| 70 if (*pref_name == prefs::kEnableAutoGarbledTextFix) { |
| 71 OnEnableStateChanged(user_prefs->GetBoolean( |
| 72 prefs::kEnableAutoGarbledTextFix)); |
| 73 } else { |
| 74 NOTREACHED(); |
| 75 } |
| 76 break; |
| 77 } |
| 78 default: |
| 79 NOTREACHED(); |
| 80 } |
| 81 } |
| 82 |
| 83 void GarbledTextHelper::OnGarbledTextDetected( |
| 84 const GarbledURLs& garbled_urls) { |
| 85 if (!GetProfile()->GetPrefs()->GetBoolean(prefs::kEnableAutoGarbledTextFix)) |
| 86 return; |
| 87 |
| 88 TabContentsWrapper* wrapper = |
| 89 TabContentsWrapper::GetCurrentWrapperForContents(web_contents()); |
| 90 DCHECK(wrapper); |
| 91 |
| 92 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); |
| 93 DCHECK(infobar_helper); |
| 94 |
| 95 infobar_helper->AddInfoBar( |
| 96 new GarbledTextInfoBarDelegate( |
| 97 infobar_helper, |
| 98 base::Bind(&GarbledTextHelper::EnableEncodingDetection, |
| 99 AsWeakPtr(), |
| 100 garbled_urls))); |
| 101 } |
| 102 |
| 103 void GarbledTextHelper::EnableEncodingDetection( |
| 104 const GarbledURLs& garbled_urls) { |
| 105 Profile* profile = GetProfile(); |
| 106 DCHECK(profile); |
| 107 |
| 108 GarbledTextService* garbled_text_service = |
| 109 GarbledTextServiceFactory::GetForProfile(profile); |
| 110 DCHECK(garbled_text_service); |
| 111 |
| 112 garbled_text_service->UpdateBlacklist( |
| 113 garbled_urls, |
| 114 base::Bind(&GarbledTextHelper::WillFinishUpdatingBlacklist, AsWeakPtr())); |
| 115 } |
| 116 |
| 117 void GarbledTextHelper::WillFinishUpdatingBlacklist() { |
| 118 web_contents()->GetRenderViewHost()->Send( |
| 119 new ChromeViewMsg_ReloadFromCache( |
| 120 web_contents()->GetRenderViewHost()->routing_id())); |
| 121 } |
| 122 |
| 123 void GarbledTextHelper::OnEnableStateChanged(bool enabled) { |
| 124 web_contents()->GetRenderViewHost()->Send( |
| 125 new ChromeViewMsg_SetGarbledTextDetectionEnableState( |
| 126 web_contents()->GetRenderViewHost()->routing_id(), |
| 127 enabled)); |
| 128 } |
OLD | NEW |