extensions: tabs: only filter out tab id when reporting values to javascript

https://codereview.chromium.org/1239643004/ modified the
ExtensionTabUtil::GetTabId function to return TAB_ID_NONE for all
devtools tabs. This is not correct as we need to be able to run
extensions on the devtools.

This CL introduces a new ExtensionTabUtil::GetTabIdForExtension
specifically to strip tab ids of devtools windows in data structure
exposed to extensions. This enables the chromium code to still be able
to get the id without exposing it to extensions.

BUG=512823
TEST=browser_test --gtest_filter=ExtensionTabsTest.ExecuteScriptOnDevTools

Review URL: https://codereview.chromium.org/1243873004

Cr-Commit-Position: refs/heads/master@{#340070}
diff --git a/chrome/browser/extensions/extension_tab_util.cc b/chrome/browser/extensions/extension_tab_util.cc
index 08064d3e..6ac0c78e 100644
--- a/chrome/browser/extensions/extension_tab_util.cc
+++ b/chrome/browser/extensions/extension_tab_util.cc
@@ -106,6 +106,16 @@
   return browser;
 }
 
+// Use this function for reporting a tab id to an extension. It will
+// take care of setting the id to TAB_ID_NONE if necessary (for
+// example with devtools).
+int GetTabIdForExtensions(const WebContents* web_contents) {
+  Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
+  if (browser && !ExtensionTabUtil::BrowserSupportsTabs(browser))
+    return -1;
+  return SessionTabHelper::IdForTab(web_contents);
+}
+
 }  // namespace
 
 ExtensionTabUtil::OpenTabParams::OpenTabParams()
@@ -315,9 +325,6 @@
 }
 
 int ExtensionTabUtil::GetTabId(const WebContents* web_contents) {
-  Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
-  if (browser && !ExtensionTabUtil::BrowserSupportsTabs(browser))
-    return -1;
   return SessionTabHelper::IdForTab(web_contents);
 }
 
@@ -377,7 +384,7 @@
 
   base::DictionaryValue* result = new base::DictionaryValue();
   bool is_loading = contents->IsLoading();
-  result->SetInteger(keys::kIdKey, GetTabId(contents));
+  result->SetInteger(keys::kIdKey, GetTabIdForExtensions(contents));
   result->SetInteger(keys::kIndexKey, tab_index);
   result->SetInteger(keys::kWindowIdKey, GetWindowIdOfTab(contents));
   result->SetString(keys::kStatusKey, GetTabStatusText(is_loading));
@@ -415,7 +422,7 @@
   if (tab_strip) {
     WebContents* opener = tab_strip->GetOpenerOfWebContentsAt(tab_index);
     if (opener)
-      result->SetInteger(keys::kOpenerTabIdKey, GetTabId(opener));
+      result->SetInteger(keys::kOpenerTabIdKey, GetTabIdForExtensions(opener));
   }
 
   return result;