summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/developer_private/inspectable_views_finder.cc
blob: f12df3aa273a42e39979488df79f8a6012332839 (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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2015 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/api/developer_private/inspectable_views_finder.h"

#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/developer_private.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/app_window/app_window.h"
#include "extensions/browser/app_window/app_window_registry.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/process_manager.h"
#include "extensions/browser/view_type_utils.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest_handlers/background_info.h"
#include "extensions/common/manifest_handlers/incognito_info.h"
#include "url/gurl.h"

namespace extensions {

InspectableViewsFinder::InspectableViewsFinder(Profile* profile)
    : profile_(profile) {
}

InspectableViewsFinder::~InspectableViewsFinder() {
}

// static
InspectableViewsFinder::View InspectableViewsFinder::ConstructView(
    const GURL& url,
    int render_process_id,
    int render_frame_id,
    bool incognito,
    bool is_iframe,
    ViewType type) {
  linked_ptr<api::developer_private::ExtensionView> view(
      new api::developer_private::ExtensionView());
  view->url = url.spec();
  view->render_process_id = render_process_id;
  // NOTE(devlin): This is called "render_view_id" in the api for legacy
  // reasons, but it's not a high priority to change.
  view->render_view_id = render_frame_id;
  view->incognito = incognito;
  view->is_iframe = is_iframe;
  switch (type) {
    case VIEW_TYPE_APP_WINDOW:
      view->type = api::developer_private::VIEW_TYPE_APP_WINDOW;
      break;
    case VIEW_TYPE_BACKGROUND_CONTENTS:
      view->type = api::developer_private::VIEW_TYPE_BACKGROUND_CONTENTS;
      break;
    case VIEW_TYPE_COMPONENT:
      view->type = api::developer_private::VIEW_TYPE_COMPONENT;
      break;
    case VIEW_TYPE_EXTENSION_BACKGROUND_PAGE:
      view->type = api::developer_private::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE;
      break;
    case VIEW_TYPE_EXTENSION_DIALOG:
      view->type = api::developer_private::VIEW_TYPE_EXTENSION_DIALOG;
      break;
    case VIEW_TYPE_EXTENSION_GUEST:
      view->type = api::developer_private::VIEW_TYPE_EXTENSION_GUEST;
      break;
    case VIEW_TYPE_EXTENSION_POPUP:
      view->type = api::developer_private::VIEW_TYPE_EXTENSION_POPUP;
      break;
    case VIEW_TYPE_LAUNCHER_PAGE:
      view->type = api::developer_private::VIEW_TYPE_LAUNCHER_PAGE;
      break;
    case VIEW_TYPE_PANEL:
      view->type = api::developer_private::VIEW_TYPE_PANEL;
      break;
    case VIEW_TYPE_TAB_CONTENTS:
      view->type = api::developer_private::VIEW_TYPE_TAB_CONTENTS;
      break;
    default:
      NOTREACHED();
  }
  return view;
}

InspectableViewsFinder::ViewList InspectableViewsFinder::GetViewsForExtension(
    const Extension& extension,
    bool is_enabled) {
  ViewList result;
  GetViewsForExtensionForProfile(
      extension, profile_, is_enabled, false, &result);
  if (profile_->HasOffTheRecordProfile()) {
    GetViewsForExtensionForProfile(
        extension,
        profile_->GetOffTheRecordProfile(),
        is_enabled,
        true,
        &result);
  }

  return result;
}

void InspectableViewsFinder::GetViewsForExtensionForProfile(
    const Extension& extension,
    Profile* profile,
    bool is_enabled,
    bool is_incognito,
    ViewList* result) {
  ProcessManager* process_manager = ProcessManager::Get(profile);
  // Get the extension process's active views.
  GetViewsForExtensionProcess(extension,
                              process_manager,
                              is_incognito,
                              result);
  // Get app window views, if not incognito.
  if (!is_incognito)
    GetAppWindowViewsForExtension(extension, result);
  // Include a link to start the lazy background page, if applicable.
  bool include_lazy_background = true;
  // Don't include the lazy background page for incognito if the extension isn't
  // enabled incognito or doesn't have a separate background page in incognito.
  if (is_incognito &&
      (!util::IsIncognitoEnabled(extension.id(), profile) ||
       !IncognitoInfo::IsSplitMode(&extension))) {
    include_lazy_background = false;
  }
  if (include_lazy_background &&
      BackgroundInfo::HasLazyBackgroundPage(&extension) &&
      is_enabled &&
      !process_manager->GetBackgroundHostForExtension(extension.id())) {
    result->push_back(ConstructView(
        BackgroundInfo::GetBackgroundURL(&extension),
        -1,
        -1,
        is_incognito,
        false,
        VIEW_TYPE_EXTENSION_BACKGROUND_PAGE));
  }
}

void InspectableViewsFinder::GetViewsForExtensionProcess(
    const Extension& extension,
    ProcessManager* process_manager,
    bool is_incognito,
    ViewList* result) {
  const std::set<content::RenderFrameHost*>& hosts =
      process_manager->GetRenderFrameHostsForExtension(extension.id());
  for (content::RenderFrameHost* host : hosts) {
    content::WebContents* web_contents =
        content::WebContents::FromRenderFrameHost(host);
    ViewType host_type = GetViewType(web_contents);
    if (host_type == VIEW_TYPE_INVALID ||
        host_type == VIEW_TYPE_EXTENSION_POPUP ||
        host_type == VIEW_TYPE_EXTENSION_DIALOG ||
        host_type == VIEW_TYPE_APP_WINDOW) {
      continue;
    }

    GURL url = web_contents->GetURL();
    // If this is a background page that just opened, there might not be a
    // committed (or visible) url yet. In this case, use the initial url.
    if (url.is_empty()) {
      ExtensionHost* extension_host =
          process_manager->GetExtensionHostForRenderFrameHost(host);
      if (extension_host)
        url = extension_host->initial_url();
    }

    bool is_iframe = web_contents->GetMainFrame() != host;
    content::RenderProcessHost* process = host->GetProcess();
    result->push_back(ConstructView(url, process->GetID(), host->GetRoutingID(),
                                    is_incognito, is_iframe, host_type));
  }
}

void InspectableViewsFinder::GetAppWindowViewsForExtension(
    const Extension& extension,
    ViewList* result) {
  AppWindowRegistry* registry = AppWindowRegistry::Get(profile_);
  if (!registry)
    return;

  AppWindowRegistry::AppWindowList windows =
      registry->GetAppWindowsForApp(extension.id());

  for (const AppWindow* window : windows) {
    content::WebContents* web_contents = window->web_contents();

    // If the window just opened, there might not be a committed (or visible)
    // url yet. In this case, use the initial url.
    GURL url = web_contents->GetLastCommittedURL();
    if (url.is_empty())
      url = window->initial_url();

    content::RenderProcessHost* process = web_contents->GetRenderProcessHost();
    result->push_back(ConstructView(
        url, process->GetID(), web_contents->GetMainFrame()->GetRoutingID(),
        false, false, GetViewType(web_contents)));
  }
}

}  // namespace extensions