1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef EXTENSIONS_RENDERER_USER_SCRIPT_SLAVE_H_
6#define EXTENSIONS_RENDERER_USER_SCRIPT_SLAVE_H_
7
8#include <map>
9#include <set>
10#include <string>
11#include <vector>
12
13#include "base/memory/linked_ptr.h"
14#include "base/memory/scoped_ptr.h"
15#include "base/memory/scoped_vector.h"
16#include "base/memory/shared_memory.h"
17#include "base/strings/string_piece.h"
18#include "extensions/common/user_script.h"
19#include "extensions/renderer/script_injection.h"
20#include "third_party/WebKit/public/platform/WebString.h"
21
22class GURL;
23
24namespace blink {
25class WebFrame;
26}
27
28namespace content {
29class RenderView;
30}
31
32namespace extensions {
33class Extension;
34class ExtensionSet;
35
36// Manages installed UserScripts for a render process.
37class UserScriptSlave {
38 public:
39  explicit UserScriptSlave(const ExtensionSet* extensions);
40  ~UserScriptSlave();
41
42  // Returns the unique set of extension IDs this UserScriptSlave knows about.
43  void GetActiveExtensions(std::set<std::string>* extension_ids);
44
45  // Gets the extension with the given |id|, if one exists.
46  const Extension* GetExtension(const std::string& extension_id);
47
48  // Update the parsed scripts from shared memory.
49  // If |changed_extensions| is not empty, only those extensions will be
50  // updated.
51  // Otherwise, all extensions will be updated.
52  bool UpdateScripts(base::SharedMemoryHandle shared_memory,
53                     const std::set<std::string>& changed_extensions);
54
55  // Gets the isolated world ID to use for the given |extension| in the given
56  // |frame|. If no isolated world has been created for that extension,
57  // one will be created and initialized.
58  int GetIsolatedWorldIdForExtension(const Extension* extension,
59                                     blink::WebFrame* frame);
60
61  // Gets the id of the extension running in a given isolated world. If no such
62  // isolated world exists, or no extension is running in it, returns empty
63  // string.
64  std::string GetExtensionIdForIsolatedWorld(int isolated_world_id);
65
66  void RemoveIsolatedWorld(const std::string& extension_id);
67
68  // Inject the appropriate scripts into a frame based on its URL.
69  // TODO(aa): Extract a UserScriptFrame interface out of this to improve
70  // testability.
71  void InjectScripts(blink::WebFrame* frame, UserScript::RunLocation location);
72
73  // Allow an extension to inject scripts that were previously delayed for user
74  // approval.
75  void OnContentScriptGrantedPermission(
76      content::RenderView* render_view, int request_id);
77
78  // Notify the UserScriptSlave that the |frame| is detached, and about to die.
79  void FrameDetached(blink::WebFrame* frame);
80
81 private:
82  // Log the data from scripts being run, including doing UMA and notifying the
83  // browser.
84  void LogScriptsRun(blink::WebFrame* frame,
85                     UserScript::RunLocation location,
86                     const ScriptInjection::ScriptsRunInfo& info);
87
88  // Shared memory containing raw script data.
89  scoped_ptr<base::SharedMemory> shared_memory_;
90
91  // Parsed script data, ready to inject.
92  ScopedVector<ScriptInjection> script_injections_;
93
94  // Extension metadata.
95  const ExtensionSet* extensions_;
96
97  typedef std::map<std::string, int> IsolatedWorldMap;
98  IsolatedWorldMap isolated_world_ids_;
99
100  DISALLOW_COPY_AND_ASSIGN(UserScriptSlave);
101};
102
103}  // namespace extensions
104
105#endif  // EXTENSIONS_RENDERER_USER_SCRIPT_SLAVE_H_
106