Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: extensions/browser/web_ui_user_script_loader.cc

Issue 1056533002: Implement <webview>.addContentScript/removeContentScript API [2] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@webview_addremove_contentscripts_2
Patch Set: Forward declaration and remove "<" overload. Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 #include "extensions/browser/web_ui_user_script_loader.h"
6
7 #include "base/bind.h"
8 #include "base/pickle.h"
Devlin 2015/04/23 16:34:38 Please double check your includes.
Xi Han 2015/04/23 17:50:40 Removed two unnecessary ones.
9 #include "base/strings/string_util.h"
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "extensions/browser/content_verifier.h"
13
14 namespace {
Devlin 2015/04/23 16:34:38 newline after namespace.
Xi Han 2015/04/23 17:50:40 Done.
15 void SerializeOnFileThread(
16 scoped_ptr<extensions::UserScriptList> user_scripts,
17 extensions::UserScriptLoader::LoadScriptsCallback callback) {
18 scoped_ptr<base::SharedMemory> memory =
19 extensions::UserScriptLoader::Serialize(*user_scripts);
20 content::BrowserThread::PostTask(
21 content::BrowserThread::UI, FROM_HERE,
22 base::Bind(callback, base::Passed(&user_scripts), base::Passed(&memory)));
23 }
24
25 } // namespace
26
27 struct WebUIUserScriptLoader::UserScriptRenderInfo {
28 int render_process_id;
29 int render_view_id;
30
31 UserScriptRenderInfo() : render_process_id(-1), render_view_id(-1) {}
32
33 UserScriptRenderInfo(int render_process_id, int render_view_id)
34 : render_process_id(render_process_id), render_view_id(render_view_id) {}
35 };
36
37 WebUIUserScriptLoader::WebUIUserScriptLoader(
38 content::BrowserContext* browser_context,
39 const HostID& host_id)
40 : UserScriptLoader(browser_context, host_id),
41 complete_fetchers_(0),
42 user_scripts_cache_(nullptr) {
Devlin 2015/04/23 16:34:38 scoped ptr has a default constructor to initialize
Xi Han 2015/04/23 17:50:40 Got it, thanks.
43 SetReady(true);
44 }
45
46 WebUIUserScriptLoader::~WebUIUserScriptLoader() {
47 }
48
49 void WebUIUserScriptLoader::AddScripts(
50 const std::set<extensions::UserScript>& scripts,
51 int render_process_id,
52 int render_view_id) {
53 UserScriptRenderInfo info(render_process_id, render_view_id);
54 for (const extensions::UserScript& script : scripts) {
55 script_render_info_map_.insert(
56 std::pair<int, UserScriptRenderInfo>(script.id(), info));
57 }
58
59 extensions::UserScriptLoader::AddScripts(scripts);
60 }
61
62 void WebUIUserScriptLoader::LoadScripts(
63 scoped_ptr<extensions::UserScriptList> user_scripts,
64 const std::set<HostID>& changed_hosts,
65 const std::set<int>& added_script_ids,
66 LoadScriptsCallback callback) {
67 user_scripts_cache_.swap(user_scripts);
68 callback_ = callback;
69
70 // The total number of the tasks is used to trace whether all the fetches
71 // are complete. Therefore, we store all the fetcher pointers in |fetchers_|
72 // before we get theis number. Once we get the total number, start each
73 // fetch tasks.
74 complete_fetchers_ = 0;
75
76 for (extensions::UserScript& script : *user_scripts_cache_) {
77 if (added_script_ids.count(script.id()) == 0)
78 continue;
79
80 auto iter = script_render_info_map_.find(script.id());
81 DCHECK(iter != script_render_info_map_.end());
82 int render_process_id = iter->second.render_process_id;
83 int render_view_id = iter->second.render_view_id;
84
85 CreateWebUIURLFetchers(&script.js_scripts(), render_process_id,
86 render_view_id);
87 CreateWebUIURLFetchers(&script.css_scripts(), render_process_id,
88 render_view_id);
89
90 script_render_info_map_.erase(script.id());
91 }
92
93 // If no fetch is needed, call OnWebUIURLFetchComplete directly.
94 if (fetchers_.empty()) {
95 OnWebUIURLFetchComplete();
96 return;
97 }
98 for (auto fetcher : fetchers_)
99 fetcher->Start();
100 }
101
102 void WebUIUserScriptLoader::CreateWebUIURLFetchers(
103 extensions::UserScript::FileList* script_files,
104 int render_process_id,
105 int render_view_id) {
106 for (extensions::UserScript::File& file : *script_files) {
107 if (file.GetContent().empty()) {
108 // The WebUIUserScriptLoader owns these WebUIURLFetchers. Once the
109 // loader is destroyed, all the fetchers will be destroyed. Therefore,
110 // we are sure it is safe to use base::Unretained(this) here.
111 scoped_ptr<WebUIURLFetcher> fetcher(new WebUIURLFetcher(
112 browser_context(), render_process_id, render_view_id, file.url(),
113 base::Bind(&WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete,
114 base::Unretained(this), &file)));
115 fetchers_.push_back(fetcher.release());
116 }
117 }
118 }
119
120 void WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete(
121 extensions::UserScript::File* script_file,
122 bool success,
123 const std::string& data) {
124 if (success) {
125 // Remove BOM from the content.
126 std::string::size_type index = data.find(base::kUtf8ByteOrderMark);
127 if (index == 0)
128 script_file->set_content(data.substr(strlen(base::kUtf8ByteOrderMark)));
129 else
130 script_file->set_content(data);
131 }
132
133 ++complete_fetchers_;
134 if (complete_fetchers_ == fetchers_.size()) {
135 complete_fetchers_ = 0;
136 OnWebUIURLFetchComplete();
137 fetchers_.clear();
138 }
139 }
140
141 void WebUIUserScriptLoader::OnWebUIURLFetchComplete() {
142 content::BrowserThread::PostTask(
143 content::BrowserThread::FILE, FROM_HERE,
144 base::Bind(&SerializeOnFileThread, base::Passed(&user_scripts_cache_),
145 callback_));
Devlin 2015/04/23 16:34:38 reset |callback_| here after this.
Xi Han 2015/04/23 17:50:40 Done.
146 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698