summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_devtools_events.cc
blob: 07b691ef1156198b9a299be7e40caf42316647d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (c) 2010 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/extensions/extension_devtools_events.h"

#include <vector>

#include "base/string_number_conversions.h"
#include "base/string_split.h"
#include "base/string_util.h"
#include "base/stringprintf.h"

// These string constants and the formats used in this file must stay
// in sync with chrome/renderer/resources/extension_process_bindings.js
static const char kDevToolsEventPrefix[] = "devtools.";
static const char kOnPageEventName[] = "onPageEvent";
static const char kOnTabCloseEventName[] = "onTabClose";

// static
bool ExtensionDevToolsEvents::IsDevToolsEventName(
    const std::string& event_name, int* tab_id) {
  // We only care about events of the form "devtools.34.*", where 34 is
  // a tab id.
  if (IsStringASCII(event_name) &&
      StartsWithASCII(event_name,
                      kDevToolsEventPrefix,
                      true /* case_sensitive */)) {
    // At this point we want something like "4.onPageEvent"
    std::vector<std::string> parts;
    base::SplitString(
        event_name.substr(strlen(kDevToolsEventPrefix)), '.', &parts);
    if (parts.size() == 2 && base::StringToInt(parts[0], tab_id)) {
      return true;
    }
  }
  return false;
}

// static
std::string ExtensionDevToolsEvents::OnPageEventNameForTab(int tab_id) {
  return base::StringPrintf("%s%d.%s",
                            kDevToolsEventPrefix,
                            tab_id,
                            kOnPageEventName);
}

// static
std::string ExtensionDevToolsEvents::OnTabCloseEventNameForTab(int tab_id) {
  return base::StringPrintf("%s%d.%s",
                            kDevToolsEventPrefix,
                            tab_id,
                            kOnTabCloseEventName);
}