Index: chrome/browser/garbled_text_helper.cc |
diff --git a/chrome/browser/garbled_text_helper.cc b/chrome/browser/garbled_text_helper.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..75976a68f2e3c0a5206e999ae38734d3237ca0d7 |
--- /dev/null |
+++ b/chrome/browser/garbled_text_helper.cc |
@@ -0,0 +1,128 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/garbled_text_helper.h" |
+ |
+#include "base/bind.h" |
+#include "chrome/browser/garbled_text_infobar_delegate.h" |
+#include "chrome/browser/garbled_text_service.h" |
+#include "chrome/browser/infobars/infobar_tab_helper.h" |
+#include "chrome/browser/prefs/pref_service.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
+#include "chrome/common/pref_names.h" |
+#include "chrome/common/render_messages.h" |
+#include "chrome/common/chrome_notification_types.h" |
+#include "content/browser/renderer_host/render_view_host.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/web_contents.h" |
+#include "ipc/ipc_message_macros.h" |
+#include "content/public/browser/notification_details.h" |
+#include "content/public/browser/notification_service.h" |
+ |
+GarbledTextHelper::GarbledTextHelper(content::WebContents* web_contents) |
+ : WebContentsObserver(web_contents) { |
+ pref_change_registrar_.Init(GetProfile()->GetPrefs()); |
+ pref_change_registrar_.Add(prefs::kEnableAutoGarbledTextFix, this); |
+} |
+ |
+GarbledTextHelper::~GarbledTextHelper() { |
+} |
+ |
+Profile* GarbledTextHelper::GetProfile() { |
+ return Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
+} |
+ |
+bool GarbledTextHelper::OnMessageReceived(const IPC::Message& message) { |
+ bool handled = true; |
+ IPC_BEGIN_MESSAGE_MAP(GarbledTextHelper, message) |
+ IPC_MESSAGE_HANDLER(ChromeViewHostMsg_GarbledTextDetected, |
+ OnGarbledTextDetected) |
+ IPC_MESSAGE_UNHANDLED(handled = false) |
+ IPC_END_MESSAGE_MAP() |
+ |
+ return handled; |
+} |
+ |
+void GarbledTextHelper::DidNavigateAnyFrame( |
+ const content::LoadCommittedDetails& details, |
+ const content::FrameNavigateParams& params) { |
+ OnEnableStateChanged( |
+ GetProfile()->GetPrefs()->GetBoolean( |
+ prefs::kEnableAutoGarbledTextFix)); |
+} |
+ |
+void GarbledTextHelper::RenderViewCreated(RenderViewHost* render_view_host) { |
+ OnEnableStateChanged( |
+ GetProfile()->GetPrefs()->GetBoolean( |
+ prefs::kEnableAutoGarbledTextFix)); |
+} |
+ |
+void GarbledTextHelper::Observe(int type, |
+ const content::NotificationSource& source, |
+ const content::NotificationDetails& details) { |
+ switch (type) { |
+ case chrome::NOTIFICATION_PREF_CHANGED: { |
+ std::string* pref_name = content::Details<std::string>(details).ptr(); |
+ PrefService* user_prefs = content::Source<PrefService>(source).ptr(); |
+ DCHECK(user_prefs == GetProfile()->GetPrefs()); |
+ if (*pref_name == prefs::kEnableAutoGarbledTextFix) { |
+ OnEnableStateChanged(user_prefs->GetBoolean( |
+ prefs::kEnableAutoGarbledTextFix)); |
+ } else { |
+ NOTREACHED(); |
+ } |
+ break; |
+ } |
+ default: |
+ NOTREACHED(); |
+ } |
+} |
+ |
+void GarbledTextHelper::OnGarbledTextDetected( |
+ const GarbledURLs& garbled_urls) { |
+ if (!GetProfile()->GetPrefs()->GetBoolean(prefs::kEnableAutoGarbledTextFix)) |
+ return; |
+ |
+ TabContentsWrapper* wrapper = |
+ TabContentsWrapper::GetCurrentWrapperForContents(web_contents()); |
+ DCHECK(wrapper); |
+ |
+ InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); |
+ DCHECK(infobar_helper); |
+ |
+ infobar_helper->AddInfoBar( |
+ new GarbledTextInfoBarDelegate( |
+ infobar_helper, |
+ base::Bind(&GarbledTextHelper::EnableEncodingDetection, |
+ AsWeakPtr(), |
+ garbled_urls))); |
+} |
+ |
+void GarbledTextHelper::EnableEncodingDetection( |
+ const GarbledURLs& garbled_urls) { |
+ Profile* profile = GetProfile(); |
+ DCHECK(profile); |
+ |
+ GarbledTextService* garbled_text_service = |
+ GarbledTextServiceFactory::GetForProfile(profile); |
+ DCHECK(garbled_text_service); |
+ |
+ garbled_text_service->UpdateBlacklist( |
+ garbled_urls, |
+ base::Bind(&GarbledTextHelper::WillFinishUpdatingBlacklist, AsWeakPtr())); |
+} |
+ |
+void GarbledTextHelper::WillFinishUpdatingBlacklist() { |
+ web_contents()->GetRenderViewHost()->Send( |
+ new ChromeViewMsg_ReloadFromCache( |
+ web_contents()->GetRenderViewHost()->routing_id())); |
+} |
+ |
+void GarbledTextHelper::OnEnableStateChanged(bool enabled) { |
+ web_contents()->GetRenderViewHost()->Send( |
+ new ChromeViewMsg_SetGarbledTextDetectionEnableState( |
+ web_contents()->GetRenderViewHost()->routing_id(), |
+ enabled)); |
+} |