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/ui/webui/ntp/android/context_menu_handler.h" 6 7 #include "base/bind.h" 8 #include "base/prefs/pref_service.h" 9 #include "base/strings/string16.h" 10 #include "base/strings/utf_string_conversions.h" 11 #include "base/values.h" 12 #include "chrome/browser/android/tab_android.h" 13 #include "chrome/browser/prefs/incognito_mode_prefs.h" 14 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/common/pref_names.h" 16 #include "content/public/browser/user_metrics.h" 17 #include "content/public/browser/web_contents.h" 18 #include "content/public/common/context_menu_params.h" 19 #include "content/public/common/page_transition_types.h" 20 21 ContextMenuHandler::ContextMenuHandler() 22 : weak_ptr_factory_(this) { 23 } 24 25 ContextMenuHandler::~ContextMenuHandler() { 26 } 27 28 void ContextMenuHandler::RegisterMessages() { 29 web_ui()->RegisterMessageCallback("showContextMenu", 30 base::Bind(&ContextMenuHandler::HandleShowContextMenu, 31 base::Unretained(this))); 32 web_ui()->RegisterMessageCallback("openInNewTab", 33 base::Bind(&ContextMenuHandler::HandleOpenInNewTab, 34 base::Unretained(this))); 35 web_ui()->RegisterMessageCallback("openInIncognitoTab", 36 base::Bind(&ContextMenuHandler::HandleOpenInIncognitoTab, 37 base::Unretained(this))); 38 web_ui()->RegisterMessageCallback("getIncognitoDisabled", 39 base::Bind(&ContextMenuHandler::GetIncognitoDisabled, 40 base::Unretained(this))); 41 } 42 43 void ContextMenuHandler::OnItemSelected(int item_id) { 44 base::FundamentalValue value(item_id); 45 web_ui()->CallJavascriptFunction("ntp.onCustomMenuSelected", value); 46 } 47 48 void ContextMenuHandler::GetIncognitoDisabled( 49 const ListValue* args) { 50 DictionaryValue value; 51 52 const PrefService* pref = Profile::FromBrowserContext( 53 web_ui()->GetWebContents()->GetBrowserContext())->GetPrefs(); 54 55 // Tell to ntp_android.js whether "open in new incognito tab" is allowed or 56 // not in different context menus. This property can be disabled by 57 // preferences (e.g. via policies). 58 int pref_value = pref->GetInteger(prefs::kIncognitoModeAvailability); 59 bool incognito_enabled = pref_value != IncognitoModePrefs::DISABLED; 60 value.SetBoolean("incognitoEnabled", incognito_enabled); 61 web_ui()->CallJavascriptFunction("ntp.setIncognitoEnabled", value); 62 } 63 64 void ContextMenuHandler::HandleShowContextMenu( 65 const ListValue* menu_list_values) { 66 if (menu_list_values->empty()) { 67 LOG(WARNING) << "Ignoring request for empty context menu."; 68 return; 69 } 70 71 // We expect menu_list_values to be of the form: 72 // [ [ 1, "title1" ], [ 2, "title2" ], ...] 73 // Where the first value in the sub-array is the item id and the second its 74 // title. 75 content::ContextMenuParams menu; 76 for (size_t i = 0; i < menu_list_values->GetSize(); ++i) { 77 ListValue* item_list_value = NULL; 78 bool valid_value = menu_list_values->GetList( 79 i, const_cast<const ListValue**>(&item_list_value)); 80 if (!valid_value) { 81 LOG(ERROR) << "Invalid context menu request: menu item info " << i << 82 " is not a list."; 83 return; 84 } 85 86 int id; 87 if (!ExtractIntegerValue(item_list_value, &id)) { 88 Value* value = NULL; 89 item_list_value->Get(0, &value); 90 LOG(ERROR) << "Invalid context menu request: menu item " << i << 91 " expected int value for first parameter (got " << 92 value->GetType() << ")."; 93 return; 94 } 95 96 content::MenuItem menu_item; 97 menu_item.action = id; 98 if (!item_list_value->GetString(1, &(menu_item.label))) { 99 Value* value = NULL; 100 item_list_value->Get(1, &value); 101 LOG(ERROR) << "Invalid context menu request: menu item " << i << 102 " expected string value for second parameter (got " << 103 value->GetType() << ")."; 104 return; 105 } 106 menu.custom_items.push_back(menu_item); 107 } 108 109 TabAndroid* tab = TabAndroid::FromWebContents(web_ui()->GetWebContents()); 110 if (tab) { 111 tab->ShowCustomContextMenu( 112 menu, 113 base::Bind(&ContextMenuHandler::OnItemSelected, 114 weak_ptr_factory_.GetWeakPtr())); 115 } 116 } 117 118 void ContextMenuHandler::HandleOpenInNewTab(const ListValue* args) { 119 OpenUrl(args, NEW_FOREGROUND_TAB); 120 } 121 122 void ContextMenuHandler::HandleOpenInIncognitoTab(const ListValue* args) { 123 OpenUrl(args, OFF_THE_RECORD); 124 } 125 126 void ContextMenuHandler::OpenUrl(const ListValue* args, 127 WindowOpenDisposition disposition) { 128 string16 url = ExtractStringValue(args); 129 if (!url.empty()) { 130 web_ui()->GetWebContents()->OpenURL(content::OpenURLParams( 131 GURL(url), content::Referrer(), disposition, 132 content::PAGE_TRANSITION_AUTO_BOOKMARK, false)); 133 } 134 } 135