Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1505)

Unified Diff: chrome/browser/ui/webui/ntp/android/context_menu_handler.cc

Issue 10861042: Add Android's context menu handler used on the NTP. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/webui/ntp/android/context_menu_handler.cc
diff --git a/chrome/browser/ui/webui/ntp/android/context_menu_handler.cc b/chrome/browser/ui/webui/ntp/android/context_menu_handler.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f6f342d4658934f87d4e399e318b5c7041cb9bb2
--- /dev/null
+++ b/chrome/browser/ui/webui/ntp/android/context_menu_handler.cc
@@ -0,0 +1,129 @@
+// 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/ui/webui/ntp/android/context_menu_handler.h"
+
+#include "base/bind.h"
+#include "base/string16.h"
+#include "base/utf_string_conversions.h"
+#include "base/values.h"
+#include "chrome/browser/android/tab_android.h"
+#include "chrome/browser/ui/webui/ntp/android/on_context_menu_item_selected_callback.h"
+#include "content/public/browser/user_metrics.h"
+#include "content/public/browser/web_contents.h"
+#include "content/public/common/context_menu_params.h"
+#include "content/public/common/page_transition_types.h"
+
+ContextMenuHandler::ContextMenuHandler() {
+}
+
+ContextMenuHandler::~ContextMenuHandler() {
+ ReleaseCallback();
+}
+
+void ContextMenuHandler::RegisterMessages() {
+ web_ui()->RegisterMessageCallback("showContextMenu",
+ base::Bind(&ContextMenuHandler::HandleShowContextMenu,
+ base::Unretained(this)));
+ web_ui()->RegisterMessageCallback("openInNewTab",
+ base::Bind(&ContextMenuHandler::HandleOpenInNewTab,
+ base::Unretained(this)));
+ web_ui()->RegisterMessageCallback("openInIncognitoTab",
+ base::Bind(&ContextMenuHandler::HandleOpenInIncognitoTab,
+ base::Unretained(this)));
+}
+
+void ContextMenuHandler::OnItemSelected(int item_id) {
+ base::FundamentalValue value(item_id);
+ web_ui()->CallJavascriptFunction("ntp.onCustomMenuSelected", value);
+ // Callback has called us, we can release our reference so it gets deleted
+ // right away.
+ ReleaseCallback();
+}
+
+void ContextMenuHandler::HandleShowContextMenu(
+ const ListValue* menu_list_values) {
+ // If we have a pending callback, it is now obsolete.
+ ReleaseCallback();
+
+ if (menu_list_values->empty()) {
+ LOG(WARNING) << "Ignoring request for empty context menu.";
+ return;
+ }
+
+ // We expect menu_list_values to be of the form:
+ // [ [ 1, "title1" ], [ 2, "title2" ], ...]
+ // Where the first value in the sub-array is the item id and the second its
+ // title.
+ content::ContextMenuParams menu;
+ for (size_t i = 0; i < menu_list_values->GetSize(); ++i) {
+ ListValue* item_list_value = NULL;
+ bool valid_value = menu_list_values->GetList(
+ i, const_cast<const ListValue**>(&item_list_value));
+ if (!valid_value) {
+ LOG(ERROR) << "Invalid context menu request: menu item info " << i <<
+ " is not a list.";
+ return;
+ }
+
+ int id;
+ if (!ExtractIntegerValue(item_list_value, &id)) {
+ Value* value = NULL;
+ item_list_value->Get(0, &value);
+ LOG(ERROR) << "Invalid context menu request: menu item " << i <<
+ " expected int value for first parameter (got " <<
+ value->GetType() << ").";
+ return;
+ }
+
+ WebMenuItem menu_item;
+ menu_item.action = id;
+ if (!item_list_value->GetString(1, &(menu_item.label))) {
+ Value* value = NULL;
+ item_list_value->Get(1, &value);
+ LOG(ERROR) << "Invalid context menu request: menu item " << i <<
+ " expected string value for second parameter (got " <<
+ value->GetType() << ").";
+ return;
+ }
+ menu.custom_items.push_back(menu_item);
+ }
+
+ on_item_selected_callback_ = new OnContextMenuItemSelectedCallBack(this);
+
+ TabAndroid* tab = TabAndroid::FromWebContents(web_ui()->GetWebContents());
+ if (tab)
+ tab->ShowCustomContextMenu(menu, on_item_selected_callback_.get());
+}
+
+void ContextMenuHandler::HandleOpenInNewTab(const ListValue* args) {
+ string16 url = ExtractStringValue(args);
+ if (!url.empty()) {
+ web_ui()->GetWebContents()->OpenURL(content::OpenURLParams(
+ GURL(url), content::Referrer(), NEW_FOREGROUND_TAB,
+ content::PAGE_TRANSITION_AUTO_BOOKMARK, false));
+ }
+}
+
+void ContextMenuHandler::HandleOpenInIncognitoTab(const ListValue* args) {
+ string16 url = ExtractStringValue(args);
+ if (!url.empty()) {
+ web_ui()->GetWebContents()->OpenURL(content::OpenURLParams(
+ GURL(url), content::Referrer(), OFF_THE_RECORD,
+ content::PAGE_TRANSITION_AUTO_BOOKMARK, false));
+ }
+}
+
+void ContextMenuHandler::SetOnItemSelectedCallback(
+ OnContextMenuItemSelectedCallBack* callback) {
+ DCHECK(!on_item_selected_callback_.get());
+ on_item_selected_callback_ = callback;
+}
+
+void ContextMenuHandler::ReleaseCallback() {
+ if (on_item_selected_callback_.get()) {
+ on_item_selected_callback_->ClearContextMenuHandler();
+ on_item_selected_callback_ = NULL;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698