| 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/ui/webui/ntp/android/context_menu_handler.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/string16.h" |
| 9 #include "base/utf_string_conversions.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/browser/android/tab_android.h" |
| 12 #include "content/public/browser/user_metrics.h" |
| 13 #include "content/public/browser/web_contents.h" |
| 14 #include "content/public/common/context_menu_params.h" |
| 15 #include "content/public/common/page_transition_types.h" |
| 16 |
| 17 ContextMenuHandler::ContextMenuHandler() |
| 18 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 19 } |
| 20 |
| 21 ContextMenuHandler::~ContextMenuHandler() { |
| 22 } |
| 23 |
| 24 void ContextMenuHandler::RegisterMessages() { |
| 25 web_ui()->RegisterMessageCallback("showContextMenu", |
| 26 base::Bind(&ContextMenuHandler::HandleShowContextMenu, |
| 27 base::Unretained(this))); |
| 28 web_ui()->RegisterMessageCallback("openInNewTab", |
| 29 base::Bind(&ContextMenuHandler::HandleOpenInNewTab, |
| 30 base::Unretained(this))); |
| 31 web_ui()->RegisterMessageCallback("openInIncognitoTab", |
| 32 base::Bind(&ContextMenuHandler::HandleOpenInIncognitoTab, |
| 33 base::Unretained(this))); |
| 34 } |
| 35 |
| 36 void ContextMenuHandler::OnItemSelected(int item_id) { |
| 37 base::FundamentalValue value(item_id); |
| 38 web_ui()->CallJavascriptFunction("ntp.onCustomMenuSelected", value); |
| 39 } |
| 40 |
| 41 void ContextMenuHandler::HandleShowContextMenu( |
| 42 const ListValue* menu_list_values) { |
| 43 if (menu_list_values->empty()) { |
| 44 LOG(WARNING) << "Ignoring request for empty context menu."; |
| 45 return; |
| 46 } |
| 47 |
| 48 // We expect menu_list_values to be of the form: |
| 49 // [ [ 1, "title1" ], [ 2, "title2" ], ...] |
| 50 // Where the first value in the sub-array is the item id and the second its |
| 51 // title. |
| 52 content::ContextMenuParams menu; |
| 53 for (size_t i = 0; i < menu_list_values->GetSize(); ++i) { |
| 54 ListValue* item_list_value = NULL; |
| 55 bool valid_value = menu_list_values->GetList( |
| 56 i, const_cast<const ListValue**>(&item_list_value)); |
| 57 if (!valid_value) { |
| 58 LOG(ERROR) << "Invalid context menu request: menu item info " << i << |
| 59 " is not a list."; |
| 60 return; |
| 61 } |
| 62 |
| 63 int id; |
| 64 if (!ExtractIntegerValue(item_list_value, &id)) { |
| 65 Value* value = NULL; |
| 66 item_list_value->Get(0, &value); |
| 67 LOG(ERROR) << "Invalid context menu request: menu item " << i << |
| 68 " expected int value for first parameter (got " << |
| 69 value->GetType() << ")."; |
| 70 return; |
| 71 } |
| 72 |
| 73 WebMenuItem menu_item; |
| 74 menu_item.action = id; |
| 75 if (!item_list_value->GetString(1, &(menu_item.label))) { |
| 76 Value* value = NULL; |
| 77 item_list_value->Get(1, &value); |
| 78 LOG(ERROR) << "Invalid context menu request: menu item " << i << |
| 79 " expected string value for second parameter (got " << |
| 80 value->GetType() << ")."; |
| 81 return; |
| 82 } |
| 83 menu.custom_items.push_back(menu_item); |
| 84 } |
| 85 |
| 86 TabAndroid* tab = TabAndroid::FromWebContents(web_ui()->GetWebContents()); |
| 87 if (tab) { |
| 88 tab->ShowCustomContextMenu( |
| 89 menu, |
| 90 base::Bind(&ContextMenuHandler::OnItemSelected, |
| 91 weak_ptr_factory_.GetWeakPtr())); |
| 92 } |
| 93 } |
| 94 |
| 95 void ContextMenuHandler::HandleOpenInNewTab(const ListValue* args) { |
| 96 OpenUrl(args, NEW_FOREGROUND_TAB); |
| 97 } |
| 98 |
| 99 void ContextMenuHandler::HandleOpenInIncognitoTab(const ListValue* args) { |
| 100 OpenUrl(args, OFF_THE_RECORD); |
| 101 } |
| 102 |
| 103 void ContextMenuHandler::OpenUrl(const ListValue* args, |
| 104 WindowOpenDisposition disposition) { |
| 105 string16 url = ExtractStringValue(args); |
| 106 if (!url.empty()) { |
| 107 web_ui()->GetWebContents()->OpenURL(content::OpenURLParams( |
| 108 GURL(url), content::Referrer(), disposition, |
| 109 content::PAGE_TRANSITION_AUTO_BOOKMARK, false)); |
| 110 } |
| 111 } |
| OLD | NEW |