| 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 "chrome/browser/ui/webui/ntp/android/on_context_menu_item_selected_call
back.h" |
| 13 #include "content/public/browser/user_metrics.h" |
| 14 #include "content/public/browser/web_contents.h" |
| 15 #include "content/public/common/context_menu_params.h" |
| 16 #include "content/public/common/page_transition_types.h" |
| 17 |
| 18 ContextMenuHandler::ContextMenuHandler() { |
| 19 } |
| 20 |
| 21 ContextMenuHandler::~ContextMenuHandler() { |
| 22 ReleaseCallback(); |
| 23 } |
| 24 |
| 25 void ContextMenuHandler::RegisterMessages() { |
| 26 web_ui()->RegisterMessageCallback("showContextMenu", |
| 27 base::Bind(&ContextMenuHandler::HandleShowContextMenu, |
| 28 base::Unretained(this))); |
| 29 web_ui()->RegisterMessageCallback("openInNewTab", |
| 30 base::Bind(&ContextMenuHandler::HandleOpenInNewTab, |
| 31 base::Unretained(this))); |
| 32 web_ui()->RegisterMessageCallback("openInIncognitoTab", |
| 33 base::Bind(&ContextMenuHandler::HandleOpenInIncognitoTab, |
| 34 base::Unretained(this))); |
| 35 } |
| 36 |
| 37 void ContextMenuHandler::OnItemSelected(int item_id) { |
| 38 base::FundamentalValue value(item_id); |
| 39 web_ui()->CallJavascriptFunction("ntp.onCustomMenuSelected", value); |
| 40 // Callback has called us, we can release our reference so it gets deleted |
| 41 // right away. |
| 42 ReleaseCallback(); |
| 43 } |
| 44 |
| 45 void ContextMenuHandler::HandleShowContextMenu( |
| 46 const ListValue* menu_list_values) { |
| 47 // If we have a pending callback, it is now obsolete. |
| 48 ReleaseCallback(); |
| 49 |
| 50 if (menu_list_values->empty()) { |
| 51 LOG(WARNING) << "Ignoring request for empty context menu."; |
| 52 return; |
| 53 } |
| 54 |
| 55 // We expect menu_list_values to be of the form: |
| 56 // [ [ 1, "title1" ], [ 2, "title2" ], ...] |
| 57 // Where the first value in the sub-array is the item id and the second its |
| 58 // title. |
| 59 content::ContextMenuParams menu; |
| 60 for (size_t i = 0; i < menu_list_values->GetSize(); ++i) { |
| 61 ListValue* item_list_value = NULL; |
| 62 bool valid_value = menu_list_values->GetList( |
| 63 i, const_cast<const ListValue**>(&item_list_value)); |
| 64 if (!valid_value) { |
| 65 LOG(ERROR) << "Invalid context menu request: menu item info " << i << |
| 66 " is not a list."; |
| 67 return; |
| 68 } |
| 69 |
| 70 int id; |
| 71 if (!ExtractIntegerValue(item_list_value, &id)) { |
| 72 Value* value = NULL; |
| 73 item_list_value->Get(0, &value); |
| 74 LOG(ERROR) << "Invalid context menu request: menu item " << i << |
| 75 " expected int value for first parameter (got " << |
| 76 value->GetType() << ")."; |
| 77 return; |
| 78 } |
| 79 |
| 80 WebMenuItem menu_item; |
| 81 menu_item.action = id; |
| 82 if (!item_list_value->GetString(1, &(menu_item.label))) { |
| 83 Value* value = NULL; |
| 84 item_list_value->Get(1, &value); |
| 85 LOG(ERROR) << "Invalid context menu request: menu item " << i << |
| 86 " expected string value for second parameter (got " << |
| 87 value->GetType() << ")."; |
| 88 return; |
| 89 } |
| 90 menu.custom_items.push_back(menu_item); |
| 91 } |
| 92 |
| 93 on_item_selected_callback_ = new OnContextMenuItemSelectedCallBack(this); |
| 94 |
| 95 TabAndroid* tab = TabAndroid::FromWebContents(web_ui()->GetWebContents()); |
| 96 if (tab) |
| 97 tab->ShowCustomContextMenu(menu, on_item_selected_callback_.get()); |
| 98 } |
| 99 |
| 100 void ContextMenuHandler::HandleOpenInNewTab(const ListValue* args) { |
| 101 string16 url = ExtractStringValue(args); |
| 102 if (!url.empty()) { |
| 103 web_ui()->GetWebContents()->OpenURL(content::OpenURLParams( |
| 104 GURL(url), content::Referrer(), NEW_FOREGROUND_TAB, |
| 105 content::PAGE_TRANSITION_AUTO_BOOKMARK, false)); |
| 106 } |
| 107 } |
| 108 |
| 109 void ContextMenuHandler::HandleOpenInIncognitoTab(const ListValue* args) { |
| 110 string16 url = ExtractStringValue(args); |
| 111 if (!url.empty()) { |
| 112 web_ui()->GetWebContents()->OpenURL(content::OpenURLParams( |
| 113 GURL(url), content::Referrer(), OFF_THE_RECORD, |
| 114 content::PAGE_TRANSITION_AUTO_BOOKMARK, false)); |
| 115 } |
| 116 } |
| 117 |
| 118 void ContextMenuHandler::SetOnItemSelectedCallback( |
| 119 OnContextMenuItemSelectedCallBack* callback) { |
| 120 DCHECK(!on_item_selected_callback_.get()); |
| 121 on_item_selected_callback_ = callback; |
| 122 } |
| 123 |
| 124 void ContextMenuHandler::ReleaseCallback() { |
| 125 if (on_item_selected_callback_.get()) { |
| 126 on_item_selected_callback_->ClearContextMenuHandler(); |
| 127 on_item_selected_callback_ = NULL; |
| 128 } |
| 129 } |
| OLD | NEW |