diff options
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/app/chrome_dll_resource.h | 4 | ||||
-rw-r--r-- | chrome/app/nibs/en.lproj/MainMenu.xib | 34 | ||||
-rw-r--r-- | chrome/browser/app_controller_mac.mm | 8 | ||||
-rw-r--r-- | chrome/browser/browser.vcproj | 8 | ||||
-rw-r--r-- | chrome/browser/cocoa/browser_window_controller.mm | 22 | ||||
-rw-r--r-- | chrome/browser/cocoa/encoding_menu_controller_delegate_mac.h | 22 | ||||
-rw-r--r-- | chrome/browser/cocoa/encoding_menu_controller_delegate_mac.mm | 63 | ||||
-rw-r--r-- | chrome/browser/encoding_menu_controller.cc | 144 | ||||
-rw-r--r-- | chrome/browser/encoding_menu_controller.h | 53 | ||||
-rw-r--r-- | chrome/browser/encoding_menu_controller_delegate.cc | 109 | ||||
-rw-r--r-- | chrome/browser/encoding_menu_controller_delegate.h | 2 | ||||
-rw-r--r-- | chrome/browser/encoding_menu_controller_unittest.cc | 92 | ||||
-rw-r--r-- | chrome/chrome.gyp | 5 | ||||
-rw-r--r-- | chrome/test/unit/unittests.vcproj | 4 |
14 files changed, 467 insertions, 103 deletions
diff --git a/chrome/app/chrome_dll_resource.h b/chrome/app/chrome_dll_resource.h index f8275e7..0854823 100644 --- a/chrome/app/chrome_dll_resource.h +++ b/chrome/app/chrome_dll_resource.h @@ -90,6 +90,10 @@ #define IDC_PRINT 35003 #define IDC_SAVE_PAGE 35004 #define IDC_ENCODING_MENU 35005 + +// When adding a new encoding to this list, be sure to append it to the +// EncodingMenuController::kValidEncodingIds array in +// encoding_menu_controller.cc. #define IDC_ENCODING_AUTO_DETECT 35006 #define IDC_ENCODING_UTF8 35007 #define IDC_ENCODING_UTF16LE 35008 diff --git a/chrome/app/nibs/en.lproj/MainMenu.xib b/chrome/app/nibs/en.lproj/MainMenu.xib index 0b5b278..fdeec8bb 100644 --- a/chrome/app/nibs/en.lproj/MainMenu.xib +++ b/chrome/app/nibs/en.lproj/MainMenu.xib @@ -8,7 +8,7 @@ <string key="IBDocument.HIToolboxVersion">353.00</string> <object class="NSMutableArray" key="IBDocument.EditedObjectIDs"> <bool key="EncodedWithXMLCoder">YES</bool> - <integer value="502"/> + <integer value="465"/> </object> <object class="NSArray" key="IBDocument.PluginDependencies"> <bool key="EncodedWithXMLCoder">YES</bool> @@ -795,14 +795,6 @@ <string key="NSTitle">Text Encoding</string> <object class="NSMutableArray" key="NSMenuItems"> <bool key="EncodedWithXMLCoder">YES</bool> - <object class="NSMenuItem" id="433074134"> - <reference key="NSMenu" ref="466817936"/> - <string type="base64-UTF8" key="NSTitle">VW5pY29kZeKEoiAoVVRGLTgpA</string> - <string key="NSKeyEquiv"/> - <int key="NSMnemonicLoc">2147483647</int> - <reference key="NSOnImage" ref="353210768"/> - <reference key="NSMixedImage" ref="549394948"/> - </object> </object> </object> </object> @@ -1580,6 +1572,14 @@ </object> <int key="connectionID">558</int> </object> + <object class="IBConnectionRecord"> + <object class="IBActionConnection" key="connection"> + <string key="label">commandDispatch:</string> + <reference key="source" ref="1014"/> + <reference key="destination" ref="602502221"/> + </object> + <int key="connectionID">615</int> + </object> </object> <object class="IBMutableOrderedSet" key="objectRecords"> <object class="NSArray" key="orderedObjects"> @@ -2234,16 +2234,10 @@ <reference key="object" ref="466817936"/> <object class="NSMutableArray" key="children"> <bool key="EncodedWithXMLCoder">YES</bool> - <reference ref="433074134"/> </object> <reference key="parent" ref="602502221"/> </object> <object class="IBObjectRecord"> - <int key="objectID">467</int> - <reference key="object" ref="433074134"/> - <reference key="parent" ref="466817936"/> - </object> - <object class="IBObjectRecord"> <int key="objectID">473</int> <reference key="object" ref="299901009"/> <object class="NSMutableArray" key="children"> @@ -2598,7 +2592,6 @@ <string>465.IBPluginDependency</string> <string>466.IBEditorWindowLastContentRect</string> <string>466.IBPluginDependency</string> - <string>467.IBPluginDependency</string> <string>473.IBPluginDependency</string> <string>483.IBPluginDependency</string> <string>491.IBPluginDependency</string> @@ -2786,13 +2779,13 @@ <string>com.apple.InterfaceBuilder.CocoaPlugin</string> <reference ref="9"/> <string>{{525, 802}, {197, 73}}</string> - <string>{{369, 836}, {535, 20}}</string> + <string>{{455, 721}, {535, 20}}</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string> <reference ref="9"/> <string>{74, 862}</string> <string>{{11, 977}, {478, 20}}</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string> - <string>{{561, 623}, {273, 213}}</string> + <string>{{647, 508}, {273, 213}}</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string> <string>{{475, 832}, {234, 43}}</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string> @@ -2825,8 +2818,7 @@ <string>com.apple.InterfaceBuilder.CocoaPlugin</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string> - <string>{{834, 643}, {178, 23}}</string> - <string>com.apple.InterfaceBuilder.CocoaPlugin</string> + <string>{{920, 545}, {64, 6}}</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string> @@ -2919,7 +2911,7 @@ </object> </object> <nil key="sourceID"/> - <int key="maxID">558</int> + <int key="maxID">615</int> </object> <object class="IBClassDescriber" key="IBDocument.Classes"> <object class="NSMutableArray" key="referencedPartialClassDescriptions"> diff --git a/chrome/browser/app_controller_mac.mm b/chrome/browser/app_controller_mac.mm index 2261342..7632024 100644 --- a/chrome/browser/app_controller_mac.mm +++ b/chrome/browser/app_controller_mac.mm @@ -13,6 +13,7 @@ #include "chrome/browser/browser_list.h" #include "chrome/browser/browser_shutdown.h" #import "chrome/browser/cocoa/bookmark_menu_bridge.h" +#import "chrome/browser/cocoa/encoding_menu_controller_delegate_mac.h" #import "chrome/browser/cocoa/preferences_window_controller.h" #include "chrome/browser/command_updater.h" #include "chrome/common/pref_names.h" @@ -66,6 +67,13 @@ // Register any Mac-specific preferences. PrefService* prefs = [self defaultProfile]->GetPrefs(); prefs->RegisterBooleanPref(prefs::kShowPageOptionsButtons, false); + + // Build up the encoding menu, the order of the items differs based on the + // current locale (see http://crbug.com/7647 for details). + // We need a valid g_browser_process to get the profile which is why we can't + // call this from awakeFromNib. + EncodingMenuControllerDelegate::BuildEncodingMenu([self defaultProfile]); + } - (void)dealloc { diff --git a/chrome/browser/browser.vcproj b/chrome/browser/browser.vcproj index 6653ca2..87cf969 100644 --- a/chrome/browser/browser.vcproj +++ b/chrome/browser/browser.vcproj @@ -2631,6 +2631,14 @@ > </File> <File + RelativePath=".\encoding_menu_controller.cc" + > + </File> + <File + RelativePath=".\encoding_menu_controller.h" + > + </File> + <File RelativePath=".\encoding_menu_controller_delegate.cc" > </File> diff --git a/chrome/browser/cocoa/browser_window_controller.mm b/chrome/browser/cocoa/browser_window_controller.mm index 283eb1e..92f3527 100644 --- a/chrome/browser/cocoa/browser_window_controller.mm +++ b/chrome/browser/cocoa/browser_window_controller.mm @@ -8,6 +8,7 @@ #include "chrome/browser/browser.h" #include "chrome/browser/browser_list.h" #include "chrome/browser/browser_process.h" +#include "chrome/browser/encoding_menu_controller.h" #include "chrome/browser/tab_contents/tab_contents.h" #include "chrome/browser/tab_contents/tab_contents_view.h" #include "chrome/browser/tabs/tab_strip_model.h" @@ -263,6 +264,27 @@ willPositionSheet:(NSWindow *)sheet if (oldState != newState) [item setState:newState]; } + + // Update the checked/Unchecked state of items in the encoding menu. + // On Windows this logic is part of encoding_menu_controller_delegate.cc + EncodingMenuController encoding_controller; + if (encoding_controller.DoesCommandBelongToEncodingMenu(tag)) { + DCHECK(browser_.get()); + Profile *profile = browser_->profile(); + DCHECK(profile); + TabContents* current_tab = browser_->GetSelectedTabContents(); + if (!current_tab) { + return; + } + const std::wstring encoding = current_tab->encoding(); + + bool toggled = encoding_controller.IsItemChecked(profile, encoding, tag); + NSInteger oldState = [item state]; + NSInteger newState = toggled ? NSOnState : NSOffState; + if (oldState != newState) + [item setState:newState]; + } + } // Called to validate menu and toolbar items when this window is key. All the diff --git a/chrome/browser/cocoa/encoding_menu_controller_delegate_mac.h b/chrome/browser/cocoa/encoding_menu_controller_delegate_mac.h new file mode 100644 index 0000000..a23b1aa --- /dev/null +++ b/chrome/browser/cocoa/encoding_menu_controller_delegate_mac.h @@ -0,0 +1,22 @@ +// Copyright (c) 2009 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. + +#ifndef CHROME_BROWSER_COCOA_ENCODING_MENU_CONTROLLER_DELEGATE_MAC_H_ +#define CHROME_BROWSER_COCOA_ENCODING_MENU_CONTROLLER_DELEGATE_MAC_H_ + +#include "base/basictypes.h" // For DISALLOW_IMPLICIT_CONSTRUCTORS + +class Profile; + +// The Windows version of this class manages the Encoding Menu, but since Cocoa +// does that for us automagically, the only thing left to do is construct +// the encoding menu. +class EncodingMenuControllerDelegate { + public: + static void BuildEncodingMenu(Profile *profile); + private: + DISALLOW_IMPLICIT_CONSTRUCTORS(EncodingMenuControllerDelegate); +}; + +#endif // CHROME_BROWSER_COCOA_ENCODING_MENU_CONTROLLER_DELEGATE_MAC_H_ diff --git a/chrome/browser/cocoa/encoding_menu_controller_delegate_mac.mm b/chrome/browser/cocoa/encoding_menu_controller_delegate_mac.mm new file mode 100644 index 0000000..4f9468f --- /dev/null +++ b/chrome/browser/cocoa/encoding_menu_controller_delegate_mac.mm @@ -0,0 +1,63 @@ +// Copyright (c) 2009 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/cocoa/encoding_menu_controller_delegate_mac.h" + +#import <Cocoa/Cocoa.h> + +#include "base/sys_string_conversions.h" +#include "chrome/app/chrome_dll_resource.h" +#include "chrome/browser/browser.h" +#include "chrome/browser/encoding_menu_controller.h" +#include "chrome/browser/profile.h" + +namespace { + +void AddSeparatorToMenu(NSMenu *parent_menu) { + NSMenuItem* separator = [NSMenuItem separatorItem]; + [parent_menu addItem:separator]; +} + +void AppendMenuItem(NSMenu *parent_menu, int tag, NSString *title) { + + NSMenuItem* item = [[[NSMenuItem alloc] initWithTitle:title + action:nil + keyEquivalent:@""] autorelease]; + [parent_menu addItem:item]; + [item setAction:@selector(commandDispatch:)]; + [item setTag:tag]; +} + +} // namespace + +// static +void EncodingMenuControllerDelegate::BuildEncodingMenu(Profile *profile) { + DCHECK(profile); + + // Get hold of the Cocoa encoding menu. + NSMenu* view_menu = [[[NSApp mainMenu] itemWithTag:IDC_VIEW_MENU] submenu]; + NSMenuItem* encoding_menu_item = [view_menu itemWithTag:IDC_ENCODING_MENU]; + NSMenu *encoding_menu = [encoding_menu_item submenu]; + + typedef EncodingMenuController::EncodingMenuItemList EncodingMenuItemList; + EncodingMenuItemList menuItems; + EncodingMenuController controller; + controller.GetEncodingMenuItems(profile, &menuItems); + + for (EncodingMenuItemList::iterator it = menuItems.begin(); + it != menuItems.end(); + ++it) { + int item_id = it->first; + std::wstring &localized_title_wstring = it->second; + + if (item_id == 0) { + AddSeparatorToMenu(encoding_menu); + } else { + using base::SysWideToNSString; + NSString *localized_title = SysWideToNSString(localized_title_wstring); + AppendMenuItem(encoding_menu, item_id, localized_title); + } + } + +} diff --git a/chrome/browser/encoding_menu_controller.cc b/chrome/browser/encoding_menu_controller.cc new file mode 100644 index 0000000..ca8c964 --- /dev/null +++ b/chrome/browser/encoding_menu_controller.cc @@ -0,0 +1,144 @@ +// Copyright (c) 2009 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/encoding_menu_controller.h" + +#include "app/l10n_util.h" +#include "chrome/app/chrome_dll_resource.h" +#include "chrome/browser/browser.h" +#include "chrome/browser/character_encoding.h" +#include "chrome/browser/profile.h" +#include "chrome/common/pref_names.h" +#include "chrome/common/pref_service.h" +#include "grit/generated_resources.h" + +const int EncodingMenuController::kValidEncodingIds[] = { + IDC_ENCODING_UTF8, + IDC_ENCODING_UTF16LE, + IDC_ENCODING_ISO88591, + IDC_ENCODING_WINDOWS1252, + IDC_ENCODING_GBK, + IDC_ENCODING_GB18030, + IDC_ENCODING_BIG5, + IDC_ENCODING_BIG5HKSCS, + IDC_ENCODING_KOREAN, + IDC_ENCODING_SHIFTJIS, + IDC_ENCODING_ISO2022JP, + IDC_ENCODING_EUCJP, + IDC_ENCODING_THAI, + IDC_ENCODING_ISO885915, + IDC_ENCODING_MACINTOSH, + IDC_ENCODING_ISO88592, + IDC_ENCODING_WINDOWS1250, + IDC_ENCODING_ISO88595, + IDC_ENCODING_WINDOWS1251, + IDC_ENCODING_KOI8R, + IDC_ENCODING_KOI8U, + IDC_ENCODING_ISO88597, + IDC_ENCODING_WINDOWS1253, + IDC_ENCODING_ISO88594, + IDC_ENCODING_ISO885913, + IDC_ENCODING_WINDOWS1257, + IDC_ENCODING_ISO88593, + IDC_ENCODING_ISO885910, + IDC_ENCODING_ISO885914, + IDC_ENCODING_ISO885916, + IDC_ENCODING_WINDOWS1254, + IDC_ENCODING_ISO88596, + IDC_ENCODING_WINDOWS1256, + IDC_ENCODING_ISO88598, + IDC_ENCODING_WINDOWS1255, + IDC_ENCODING_WINDOWS1258, + IDC_ENCODING_ISO88598I, +}; + +bool EncodingMenuController::DoesCommandBelongToEncodingMenu(int id) { + if (id == IDC_ENCODING_AUTO_DETECT) { + return true; + } + + for (size_t i = 0; i < arraysize(kValidEncodingIds); ++i) { + if (id == kValidEncodingIds[i]) { + return true; + } + } + + return false; +} + +const int* EncodingMenuController::ValidGUIEncodingIDs() { + return kValidEncodingIds; +} + +int EncodingMenuController::NumValidGUIEncodingIDs() { + return arraysize(kValidEncodingIds); +} + +bool EncodingMenuController::IsItemChecked( + Profile* browser_profile, + const std::wstring& current_tab_encoding, + int item_id) { + if (!DoesCommandBelongToEncodingMenu(item_id)) { + return false; + } + + std::wstring encoding = current_tab_encoding; + if (encoding.empty()) { + encoding = browser_profile->GetPrefs()->GetString(prefs::kDefaultCharset); + } + + if (item_id == IDC_ENCODING_AUTO_DETECT) { + return browser_profile->GetPrefs()->GetBoolean( + prefs::kWebKitUsesUniversalDetector); + } + + if (!encoding.empty()) { + return encoding == + CharacterEncoding::GetCanonicalEncodingNameByCommandId(item_id); + } + + return false; +} + +void EncodingMenuController::GetEncodingMenuItems(Profile* profile, + EncodingMenuItemList* menuItems) { + + DCHECK(menuItems); + EncodingMenuItem separator(0, L""); + + menuItems->clear(); + menuItems->push_back( + EncodingMenuItem(IDC_ENCODING_AUTO_DETECT, + l10n_util::GetString(IDS_ENCODING_AUTO_DETECT))); + menuItems->push_back(separator); + + // Create current display encoding list. + const std::vector<CharacterEncoding::EncodingInfo>* encodings; + + // Build the list of encoding ids : It is made of the + // locale-dependent short list, the cache of recently selected + // encodings and other encodings. + encodings = CharacterEncoding::GetCurrentDisplayEncodings( + g_browser_process->GetApplicationLocale(), + profile->GetPrefs()->GetString(prefs::kStaticEncodings), + profile->GetPrefs()->GetString(prefs::kRecentlySelectedEncoding)); + DCHECK(encodings); + DCHECK(!encodings->empty()); + + // Build up output list for menu. + std::vector<CharacterEncoding::EncodingInfo>::const_iterator it; + for (it = encodings->begin(); it != encodings->end(); ++it) { + if (it->encoding_id) { + std::wstring encoding = it->encoding_display_name; + std::wstring bidi_safe_encoding; + if (l10n_util::AdjustStringForLocaleDirection(encoding, + &bidi_safe_encoding)) + encoding.swap(bidi_safe_encoding); + menuItems->push_back(EncodingMenuItem(it->encoding_id, encoding)); + } else { + menuItems->push_back(separator); + } + } + +} diff --git a/chrome/browser/encoding_menu_controller.h b/chrome/browser/encoding_menu_controller.h new file mode 100644 index 0000000..9134b54 --- /dev/null +++ b/chrome/browser/encoding_menu_controller.h @@ -0,0 +1,53 @@ +// Copyright (c) 2009 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. + +#ifndef CHORME_BROWSER_ENCODING_MENU_CONTROLLER_H_ +#define CHORME_BROWSER_ENCODING_MENU_CONTROLLER_H_ + +#include <vector> + +#include "base/basictypes.h" // For DISALLOW_IMPLICIT_CONSTRUCTORS +#include "testing/gtest/include/gtest/gtest_prod.h" // For FRIEND_TEST + +class Browser; +class Profile; + +// Cross-platform logic needed for the encoding menu. +// For now, we don't need to track state so all methods are static. +class EncodingMenuController { + FRIEND_TEST(EncodingMenuControllerTest, EncodingIDsBelongTest); + FRIEND_TEST(EncodingMenuControllerTest, IsItemChecked); + + public: + typedef std::pair<int, std::wstring> EncodingMenuItem; + typedef std::vector<EncodingMenuItem> EncodingMenuItemList; + + public: + EncodingMenuController() {} + + // Given a command ID, does this command belong to the encoding menu? + bool DoesCommandBelongToEncodingMenu(int id); + + // Returns true if the given encoding menu item (specified by item_id) + // is checked. Note that this header is included from objc, where the name + // "id" is reserved. + bool IsItemChecked(Profile* browser_profile, + const std::wstring& current_tab_encoding, + int item_id); + + // Fills in a list of menu items in the order they should appear in the menu. + // Items whose ids are 0 are separators. + void GetEncodingMenuItems(Profile* profile, + EncodingMenuItemList* menuItems); + + private: + // List of all valid encoding GUI IDs. + static const int kValidEncodingIds[]; + const int* ValidGUIEncodingIDs(); + int NumValidGUIEncodingIDs(); + private: + DISALLOW_COPY_AND_ASSIGN(EncodingMenuController); +}; + +#endif // CHORME_BROWSER_ENCODING_MENU_CONTROLLER_H_ diff --git a/chrome/browser/encoding_menu_controller_delegate.cc b/chrome/browser/encoding_menu_controller_delegate.cc index a7a925a..6faac2d 100644 --- a/chrome/browser/encoding_menu_controller_delegate.cc +++ b/chrome/browser/encoding_menu_controller_delegate.cc @@ -4,77 +4,34 @@ #include "chrome/browser/encoding_menu_controller_delegate.h" -#include "app/l10n_util.h" #include "chrome/app/chrome_dll_resource.h" #include "chrome/browser/browser.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/character_encoding.h" +#include "chrome/browser/encoding_menu_controller.h" #include "chrome/browser/profile.h" #include "chrome/browser/tab_contents/tab_contents.h" #include "chrome/common/pref_names.h" #include "chrome/common/pref_service.h" -#include "grit/generated_resources.h" EncodingMenuControllerDelegate::EncodingMenuControllerDelegate(Browser* browser) : browser_(browser) { } bool EncodingMenuControllerDelegate::IsItemChecked(int id) const { - Profile* profile = browser_->profile(); + if (!browser_) + return false; + Profile *profile = browser_->profile(); if (!profile) return false; TabContents* current_tab = browser_->GetSelectedTabContents(); - if (!current_tab) + if (!current_tab) { return false; - std::wstring encoding = current_tab->encoding(); - if (encoding.empty()) - encoding = profile->GetPrefs()->GetString(prefs::kDefaultCharset); - switch (id) { - case IDC_ENCODING_AUTO_DETECT: - return profile->GetPrefs()->GetBoolean( - prefs::kWebKitUsesUniversalDetector); - case IDC_ENCODING_UTF8: - case IDC_ENCODING_UTF16LE: - case IDC_ENCODING_ISO88591: - case IDC_ENCODING_WINDOWS1252: - case IDC_ENCODING_GBK: - case IDC_ENCODING_GB18030: - case IDC_ENCODING_BIG5: - case IDC_ENCODING_BIG5HKSCS: - case IDC_ENCODING_KOREAN: - case IDC_ENCODING_SHIFTJIS: - case IDC_ENCODING_ISO2022JP: - case IDC_ENCODING_EUCJP: - case IDC_ENCODING_THAI: - case IDC_ENCODING_ISO885915: - case IDC_ENCODING_MACINTOSH: - case IDC_ENCODING_ISO88592: - case IDC_ENCODING_WINDOWS1250: - case IDC_ENCODING_ISO88595: - case IDC_ENCODING_WINDOWS1251: - case IDC_ENCODING_KOI8R: - case IDC_ENCODING_KOI8U: - case IDC_ENCODING_ISO88597: - case IDC_ENCODING_WINDOWS1253: - case IDC_ENCODING_ISO88594: - case IDC_ENCODING_ISO885913: - case IDC_ENCODING_WINDOWS1257: - case IDC_ENCODING_ISO88593: - case IDC_ENCODING_ISO885910: - case IDC_ENCODING_ISO885914: - case IDC_ENCODING_ISO885916: - case IDC_ENCODING_WINDOWS1254: - case IDC_ENCODING_ISO88596: - case IDC_ENCODING_WINDOWS1256: - case IDC_ENCODING_ISO88598: - case IDC_ENCODING_ISO88598I: - case IDC_ENCODING_WINDOWS1255: - case IDC_ENCODING_WINDOWS1258: - return (!encoding.empty() && encoding == - CharacterEncoding::GetCanonicalEncodingNameByCommandId(id)); - default: - return false; } + const std::wstring encoding = current_tab->encoding(); + + EncodingMenuController controller; + return controller.IsItemChecked(profile, encoding, id); } bool EncodingMenuControllerDelegate::SupportsCommand(int id) const { @@ -97,38 +54,26 @@ void EncodingMenuControllerDelegate::ExecuteCommand(int id) { void EncodingMenuControllerDelegate::BuildEncodingMenu( Profile* profile, Menu* encoding_menu) { - // Append auto-detection item. - encoding_menu->AppendMenuItem(IDC_ENCODING_AUTO_DETECT, - l10n_util::GetString(IDS_ENCODING_AUTO_DETECT), - Menu::CHECKBOX); + typedef EncodingMenuController::EncodingMenuItemList EncodingMenuItemList; + EncodingMenuItemList menuItems; + EncodingMenuController controller; + controller.GetEncodingMenuItems(profile, &menuItems); - // Append encoding item. - encoding_menu->AppendSeparator(); - // Create current display encoding list. - std::wstring cur_locale = g_browser_process->GetApplicationLocale(); - const std::vector<CharacterEncoding::EncodingInfo>* encodings; - // Build the list of encoding ids : It is made of the - // locale-dependent short list, the cache of recently selected - // encodings and other encodings. - encodings = CharacterEncoding::GetCurrentDisplayEncodings( - cur_locale, - profile->GetPrefs()->GetString(prefs::kStaticEncodings), - profile->GetPrefs()->GetString(prefs::kRecentlySelectedEncoding)); - DCHECK(encodings); - DCHECK(!encodings->empty()); - unsigned len = static_cast<unsigned>(encodings->size()); - // Add encoding menus. - std::vector<CharacterEncoding::EncodingInfo>::const_iterator it; - for (it = encodings->begin(); it != encodings->end(); ++it) { - if (it->encoding_id) { - std::wstring encoding = it->encoding_display_name; - std::wstring bidi_safe_encoding; - if (l10n_util::AdjustStringForLocaleDirection(encoding, - &bidi_safe_encoding)) - encoding.swap(bidi_safe_encoding); - encoding_menu->AppendMenuItem(it->encoding_id, encoding, Menu::RADIO); - } else { + for (EncodingMenuItemList::iterator it = menuItems.begin(); + it != menuItems.end(); + ++it) { + Menu::MenuItemType type = Menu::RADIO; + int id = it->first; + std::wstring &localized_title = it->second; + + if (id == 0) { encoding_menu->AppendSeparator(); + } else { + if (id == IDC_ENCODING_AUTO_DETECT) { + type = Menu::CHECKBOX; + } + + encoding_menu->AppendMenuItem(id, localized_title, type); } } } diff --git a/chrome/browser/encoding_menu_controller_delegate.h b/chrome/browser/encoding_menu_controller_delegate.h index 83d42ec..f35a5e4 100644 --- a/chrome/browser/encoding_menu_controller_delegate.h +++ b/chrome/browser/encoding_menu_controller_delegate.h @@ -5,6 +5,7 @@ #ifndef CHORME_BROWSER_ENCODING_MENU_CONTROLLER_DELEGATE_H__ #define CHORME_BROWSER_ENCODING_MENU_CONTROLLER_DELEGATE_H__ +#include "base/basictypes.h" // For DISALLOW_IMPLICIT_CONSTRUCTORS #include "views/controls/menu/menu.h" class Browser; @@ -32,6 +33,7 @@ class EncodingMenuControllerDelegate : public Menu::Delegate { private: Browser* browser_; + DISALLOW_IMPLICIT_CONSTRUCTORS(EncodingMenuControllerDelegate); }; #endif // CHORME_BROWSER_ENCODING_MENU_CONTROLLER_DELEGATE_H__ diff --git a/chrome/browser/encoding_menu_controller_unittest.cc b/chrome/browser/encoding_menu_controller_unittest.cc new file mode 100644 index 0000000..a8258bd6 --- /dev/null +++ b/chrome/browser/encoding_menu_controller_unittest.cc @@ -0,0 +1,92 @@ +// Copyright (c) 2009 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/encoding_menu_controller.h" + +#include <string> + +#include "base/basictypes.h" +#include "chrome/app/chrome_dll_resource.h" +#include "chrome/browser/profile.h" +#include "chrome/common/pref_names.h" +#include "chrome/test/testing_profile.h" +#include "testing/gtest/include/gtest/gtest.h" + + +class EncodingMenuControllerTest : public testing::Test { +}; + +TEST_F(EncodingMenuControllerTest, EncodingIDsBelongTest) { + EncodingMenuController controller; + + // Check some bogus ids to make sure they're never valid. + ASSERT_FALSE(controller.DoesCommandBelongToEncodingMenu(0)); + ASSERT_FALSE(controller.DoesCommandBelongToEncodingMenu(-1)); + + int num_valid_encoding_ids = controller.NumValidGUIEncodingIDs(); + const int* valid_encodings = controller.ValidGUIEncodingIDs(); + ASSERT_TRUE(controller.DoesCommandBelongToEncodingMenu( + IDC_ENCODING_AUTO_DETECT)); + // Check that all valid encodings are accepted. + for (int i = 0; i < num_valid_encoding_ids; ++i) { + ASSERT_TRUE(controller.DoesCommandBelongToEncodingMenu(valid_encodings[i])); + } + + // This test asserts that we haven't added a new valid ID without including it + // in the kValidEncodingIds test list above. + // The premise is that new encodings will be added directly after the current + // ones so we'll catch such cases. + int one_past_largest_id = valid_encodings[num_valid_encoding_ids - 1] + 1; + ASSERT_FALSE(controller.DoesCommandBelongToEncodingMenu(one_past_largest_id)); +} + +TEST_F(EncodingMenuControllerTest, ListEncodingMenuItems) { + typedef EncodingMenuController::EncodingMenuItemList EncodingMenuItemList; + EncodingMenuController controller; + + EncodingMenuItemList english_items; + TestingProfile profile_en; + + controller.GetEncodingMenuItems(&profile_en, &english_items); + + // Make sure there are items in the lists. + ASSERT_TRUE(english_items.size() > 0); + // Make sure that autodetect is the first item on both menus + ASSERT_EQ(english_items[0].first, IDC_ENCODING_AUTO_DETECT); +} + +TEST_F(EncodingMenuControllerTest, IsItemChecked) { + TestingProfile profile_en; + std::wstring encoding(L"UTF-8"); + + // Check that enabling and disabling autodetect works. + bool autodetect_enabed[] = {true, false}; + PrefService* prefs = profile_en.GetPrefs(); + EncodingMenuController controller; + for (size_t i = 0; i < arraysize(autodetect_enabed); ++i) { + bool enabled = autodetect_enabed[i]; + prefs->SetBoolean(prefs::kWebKitUsesUniversalDetector, enabled); + ASSERT_TRUE(controller.IsItemChecked(&profile_en, + encoding, + IDC_ENCODING_AUTO_DETECT) == enabled); + } + + // Check all valid encodings, make sure only one is enabled when autodetection + // is turned off. + prefs->SetBoolean(prefs::kWebKitUsesUniversalDetector, false); + bool encoding_is_enabled = false; + size_t num_valid_encoding_ids = controller.NumValidGUIEncodingIDs(); + const int* valid_encodings = controller.ValidGUIEncodingIDs(); + for (size_t i = 0; i < num_valid_encoding_ids; ++i) { + bool on = controller.IsItemChecked(&profile_en, + encoding, + valid_encodings[i]); + // Only one item in the encoding menu can be selected at a time. + ASSERT_FALSE(on && encoding_is_enabled); + encoding_is_enabled |= on; + } + + // Make sure at least one encoding is enabled. + ASSERT_TRUE(encoding_is_enabled); +} diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index 5e4e07b..8b47380 100644 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -728,6 +728,8 @@ 'browser/cocoa/cocoa_test_helper.h', 'browser/cocoa/command_observer_bridge.h', 'browser/cocoa/command_observer_bridge.mm', + 'browser/cocoa/encoding_menu_controller_delegate_mac.h', + 'browser/cocoa/encoding_menu_controller_delegate_mac.mm', 'browser/cocoa/find_bar_bridge.h', 'browser/cocoa/find_bar_bridge.mm', 'browser/cocoa/find_bar_cocoa_controller.h', @@ -864,6 +866,8 @@ 'browser/download/save_package.cc', 'browser/download/save_package.h', 'browser/download/save_types.h', + 'browser/encoding_menu_controller.cc', + 'browser/encoding_menu_controller.h', 'browser/encoding_menu_controller_delegate.cc', 'browser/encoding_menu_controller_delegate.h', 'browser/extensions/extension.cc', @@ -2694,6 +2698,7 @@ 'browser/cocoa/browser_window_cocoa_unittest.mm', 'browser/cocoa/browser_window_controller_unittest.mm', 'browser/cocoa/command_observer_bridge_unittest.mm', + 'browser/encoding_menu_controller_unittest.cc', 'browser/cocoa/find_bar_bridge_unittest.mm', 'browser/cocoa/find_bar_cocoa_controller_unittest.mm', 'browser/cocoa/find_bar_view_unittest.mm', diff --git a/chrome/test/unit/unittests.vcproj b/chrome/test/unit/unittests.vcproj index f18b051..6c8ddc5 100644 --- a/chrome/test/unit/unittests.vcproj +++ b/chrome/test/unit/unittests.vcproj @@ -516,6 +516,10 @@ > </File> <File + RelativePath="..\..\browser\encoding_menu_controller_unittest.cc" + > + </File> + <File RelativePath="..\..\browser\extensions\extension_messages_unittest.cc" > </File> |