File: devtools_util.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (99 lines) | stat: -rw-r--r-- 3,859 bytes parent folder | download | duplicates (4)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/devtools_util.h"

#include "base/functional/bind.h"
#include "chrome/browser/devtools/devtools_window.h"
#include "chrome/browser/profiles/profile.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/lazy_context_id.h"
#include "extensions/browser/lazy_context_task_queue.h"
#include "extensions/browser/process_manager.h"
#include "extensions/browser/service_worker/service_worker_task_queue.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest_handlers/background_info.h"

namespace extensions {
namespace devtools_util {

namespace {

// Helper to inspect an ExtensionHost after it has been loaded.
void InspectExtensionHost(
    DevToolsOpenedByAction opened_by,
    std::unique_ptr<LazyContextTaskQueue::ContextInfo> context_info) {
  if (context_info != nullptr)
    DevToolsWindow::OpenDevToolsWindow(context_info->web_contents, opened_by);
}

void InspectServiceWorkerBackgroundHelper(
    DevToolsOpenedByAction opened_by,
    std::unique_ptr<LazyContextTaskQueue::ContextInfo> context_info) {
  if (!context_info)
    return;

  Profile* profile = Profile::FromBrowserContext(context_info->browser_context);
  const Extension* extension =
      ExtensionRegistry::Get(context_info->browser_context)
          ->enabled_extensions()
          .GetByID(context_info->extension_id);

  // A non-null context info does not guarantee that the extension is enabled,
  // due to thread/process asynchrony.
  if (extension)
    InspectServiceWorkerBackground(extension, profile, opened_by);
}

}  // namespace

// Helper to inspect a service worker after it has been started.
void InspectServiceWorkerBackground(const Extension* extension,
                                    Profile* profile,
                                    DevToolsOpenedByAction opened_by) {
  DCHECK(BackgroundInfo::IsServiceWorkerBased(extension));
  content::DevToolsAgentHost::List targets =
      content::DevToolsAgentHost::GetOrCreateAll();
  for (const scoped_refptr<content::DevToolsAgentHost>& host : targets) {
    if (host->GetType() == content::DevToolsAgentHost::kTypeServiceWorker &&
        host->GetURL() ==
            extension->GetResourceURL(
                BackgroundInfo::GetBackgroundServiceWorkerScript(extension)) &&
        host->GetBrowserContext() == profile) {
      DevToolsWindow::OpenDevToolsWindow(host, profile, opened_by);
      break;
    }
  }
}

void InspectInactiveServiceWorkerBackground(const Extension* extension,
                                            Profile* profile,
                                            DevToolsOpenedByAction opened_by) {
  DCHECK(extension);
  DCHECK(BackgroundInfo::IsServiceWorkerBased(extension));
  const auto context_id = LazyContextId::ForExtension(profile, extension);
  context_id.GetTaskQueue()->AddPendingTask(
      context_id,
      base::BindOnce(&InspectServiceWorkerBackgroundHelper, opened_by));
}

void InspectBackgroundPage(const Extension* extension,
                           Profile* profile,
                           DevToolsOpenedByAction opened_by) {
  DCHECK(extension);
  ExtensionHost* host = ProcessManager::Get(profile)
                            ->GetBackgroundHostForExtension(extension->id());
  if (host) {
    InspectExtensionHost(
        opened_by, std::make_unique<LazyContextTaskQueue::ContextInfo>(host));
  } else {
    const auto context_id = LazyContextId::ForExtension(profile, extension);
    context_id.GetTaskQueue()->AddPendingTask(
        context_id, base::BindOnce(&InspectExtensionHost, opened_by));
  }
}

}  // namespace devtools_util
}  // namespace extensions