OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/browser/api/guest_view/web_view/web_view_internal_api.h" | 5 #include "extensions/browser/api/guest_view/web_view/web_view_internal_api.h" |
6 | 6 |
7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "content/public/browser/browser_context.h" | |
9 #include "content/public/browser/render_process_host.h" | 10 #include "content/public/browser/render_process_host.h" |
10 #include "content/public/browser/render_view_host.h" | 11 #include "content/public/browser/render_view_host.h" |
11 #include "content/public/browser/storage_partition.h" | 12 #include "content/public/browser/storage_partition.h" |
12 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
13 #include "content/public/common/stop_find_action.h" | 14 #include "content/public/common/stop_find_action.h" |
15 #include "content/public/common/url_fetcher.h" | |
14 #include "extensions/common/api/web_view_internal.h" | 16 #include "extensions/common/api/web_view_internal.h" |
17 #include "net/base/load_flags.h" | |
18 #include "net/url_request/url_fetcher.h" | |
19 #include "net/url_request/url_fetcher_delegate.h" | |
15 #include "third_party/WebKit/public/web/WebFindOptions.h" | 20 #include "third_party/WebKit/public/web/WebFindOptions.h" |
16 | 21 |
17 using content::WebContents; | 22 using content::WebContents; |
18 using extensions::core_api::web_view_internal::SetPermission::Params; | 23 using extensions::core_api::web_view_internal::SetPermission::Params; |
19 using extensions::core_api::extension_types::InjectDetails; | 24 using extensions::core_api::extension_types::InjectDetails; |
25 using net::URLFetcher; | |
20 namespace webview = extensions::core_api::web_view_internal; | 26 namespace webview = extensions::core_api::web_view_internal; |
21 | 27 |
22 namespace { | 28 namespace { |
23 | 29 |
24 const char kAppCacheKey[] = "appcache"; | 30 const char kAppCacheKey[] = "appcache"; |
25 const char kCookiesKey[] = "cookies"; | 31 const char kCookiesKey[] = "cookies"; |
26 const char kFileSystemsKey[] = "fileSystems"; | 32 const char kFileSystemsKey[] = "fileSystems"; |
27 const char kIndexedDBKey[] = "indexedDB"; | 33 const char kIndexedDBKey[] = "indexedDB"; |
28 const char kLocalStorageKey[] = "localStorage"; | 34 const char kLocalStorageKey[] = "localStorage"; |
29 const char kWebSQLKey[] = "webSQL"; | 35 const char kWebSQLKey[] = "webSQL"; |
(...skipping 12 matching lines...) Expand all Loading... | |
42 return content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE; | 48 return content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE; |
43 if (strcmp(key, kWebSQLKey) == 0) | 49 if (strcmp(key, kWebSQLKey) == 0) |
44 return content::StoragePartition::REMOVE_DATA_MASK_WEBSQL; | 50 return content::StoragePartition::REMOVE_DATA_MASK_WEBSQL; |
45 return 0; | 51 return 0; |
46 } | 52 } |
47 | 53 |
48 } // namespace | 54 } // namespace |
49 | 55 |
50 namespace extensions { | 56 namespace extensions { |
51 | 57 |
58 // WebUIURLFetcher downloads the content of a file by giving its |url| on WebUI. | |
59 // Each WebUIURLFetcher is associated with a given |render_process_id, | |
60 // render_view_id| pair. | |
61 class WebViewInternalExecuteCodeFunction::WebUIURLFetcher | |
62 : public net::URLFetcherDelegate { | |
63 public: | |
64 WebUIURLFetcher(content::BrowserContext* context, | |
65 const GURL& url, | |
66 int render_process_id, | |
67 int render_view_id, | |
68 const ExecuteCodeFunction::WebUILoadFileCallback& callback) | |
69 : context_(context), | |
70 url_(url), | |
71 render_process_id_(render_process_id), | |
72 render_view_id_(render_view_id), | |
73 callback_(callback) {} | |
74 ~WebUIURLFetcher() override {} | |
75 | |
76 void Start() { | |
77 fetcher_.reset(URLFetcher::Create(url_, URLFetcher::GET, this)); | |
78 fetcher_->SetRequestContext(context_->GetRequestContext()); | |
79 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); | |
80 | |
81 content::AssociateURLFetcherWithRenderFrame( | |
82 fetcher_.get(), url_, render_process_id_, render_view_id_); | |
83 fetcher_->Start(); | |
84 } | |
85 | |
86 private: | |
87 // net::URLFetcherDelegate: | |
88 void OnURLFetchComplete(const URLFetcher* source) override { | |
89 CHECK_EQ(fetcher_.get(), source); | |
90 | |
91 std::string data; | |
92 if (fetcher_->GetStatus().status() != net::URLRequestStatus::SUCCESS || | |
93 fetcher_->GetResponseCode() != 200) { | |
94 callback_.Run(false, data); | |
Devlin
2015/03/19 15:45:35
I think it'd be nice to rewrite this as:
std::str
Devlin
2015/03/19 15:45:35
I think it'd be nice to rewrite this as:
std::str
Xi Han
2015/03/19 19:53:15
Updated.
asargent_no_longer_on_chrome
2015/03/19 20:24:38
Random drive-by: In the past we found bugs in exte
Xi Han
2015/03/19 21:05:14
That is a good point. Also I fount out URLRequestC
| |
95 return; | |
96 } | |
97 | |
98 bool result = fetcher_->GetResponseAsString(&data); | |
99 DCHECK(result) << "Invalid fetcher setting."; | |
100 callback_.Run(true, data); | |
101 } | |
102 | |
103 content::BrowserContext* context_; | |
104 GURL url_; | |
105 const int render_process_id_; | |
Devlin
2015/03/19 15:45:35
Is there a reason to store all these as member var
Xi Han
2015/03/19 19:53:15
There is no special reason, just makes the start()
| |
106 const int render_view_id_; | |
107 const ExecuteCodeFunction::WebUILoadFileCallback callback_; | |
108 scoped_ptr<URLFetcher> fetcher_; | |
109 | |
110 DISALLOW_COPY_AND_ASSIGN(WebUIURLFetcher); | |
111 }; | |
112 | |
52 bool WebViewInternalExtensionFunction::RunAsync() { | 113 bool WebViewInternalExtensionFunction::RunAsync() { |
53 int instance_id = 0; | 114 int instance_id = 0; |
54 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); | 115 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); |
55 WebViewGuest* guest = WebViewGuest::From( | 116 WebViewGuest* guest = WebViewGuest::From( |
56 render_view_host()->GetProcess()->GetID(), instance_id); | 117 render_view_host()->GetProcess()->GetID(), instance_id); |
57 if (!guest) | 118 if (!guest) |
58 return false; | 119 return false; |
59 | 120 |
60 return RunAsyncSafe(guest); | 121 return RunAsyncSafe(guest); |
61 } | 122 } |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
138 } | 199 } |
139 | 200 |
140 bool WebViewInternalExecuteCodeFunction::IsWebView() const { | 201 bool WebViewInternalExecuteCodeFunction::IsWebView() const { |
141 return true; | 202 return true; |
142 } | 203 } |
143 | 204 |
144 const GURL& WebViewInternalExecuteCodeFunction::GetWebViewSrc() const { | 205 const GURL& WebViewInternalExecuteCodeFunction::GetWebViewSrc() const { |
145 return guest_src_; | 206 return guest_src_; |
146 } | 207 } |
147 | 208 |
209 bool WebViewInternalExecuteCodeFunction::LoadFileForWebUI( | |
210 const std::string& file_src, | |
211 const WebUILoadFileCallback& callback) { | |
212 if (!render_view_host() || !render_view_host()->GetProcess()) | |
213 return false; | |
214 WebViewGuest* guest = WebViewGuest::From( | |
215 render_view_host()->GetProcess()->GetID(), guest_instance_id_); | |
216 if (!guest || host_id().type() != HostID::WEBUI) | |
217 return false; | |
218 | |
219 GURL owner_base_url(guest->GetOwnerSiteURL().GetWithEmptyPath()); | |
220 GURL file_url(owner_base_url.Resolve(file_src)); | |
221 | |
222 scoped_ptr<WebUIURLFetcher> fetcher( | |
223 new WebUIURLFetcher(this->browser_context(), file_url, | |
224 render_view_host()->GetProcess()->GetID(), | |
225 render_view_host()->GetRoutingID(), callback)); | |
226 fetcher->Start(); | |
227 fetcher_.reset(fetcher.release()); // Pass ownership to |fetcher_|. | |
Devlin
2015/03/19 15:45:35
... Why not just do fetcher_.reset(new WebUIUrlFet
Xi Han
2015/03/19 19:53:15
I also find that the old code causes memory leak.
| |
228 return true; | |
229 } | |
230 | |
231 bool WebViewInternalExecuteCodeFunction::LoadFile(const std::string& file) { | |
232 if (!extension()) { | |
233 bool is_success = false; | |
234 is_success = LoadFileForWebUI( | |
235 *details_->file, | |
236 base::Bind(&WebViewInternalExecuteCodeFunction::DidLoadFileForWebUI, | |
237 this)); | |
238 if (!is_success) { | |
Devlin
2015/03/19 15:45:35
inline is_success
Xi Han
2015/03/19 19:53:15
Done.
| |
239 SendResponse(false); | |
Devlin
2015/03/19 15:45:35
Set an error?
Xi Han
2015/03/19 19:53:14
Done.
| |
240 return false; | |
241 } | |
242 // Will finish asynchronously. | |
243 return true; | |
244 } | |
245 return ExecuteCodeFunction::LoadFile(file); | |
246 } | |
247 | |
248 void WebViewInternalExecuteCodeFunction::DidLoadFileForWebUI( | |
249 bool success, | |
250 const std::string& data) { | |
251 if (success) { | |
252 if (!base::IsStringUTF8(data)) | |
253 SendResponse(false); | |
Devlin
2015/03/19 15:45:36
Set errors.
Xi Han
2015/03/19 19:53:15
Done.
| |
254 else if (!Execute(data)) | |
255 SendResponse(false); | |
256 } else { | |
257 SendResponse(false); | |
258 } | |
259 } | |
260 | |
148 WebViewInternalExecuteScriptFunction::WebViewInternalExecuteScriptFunction() { | 261 WebViewInternalExecuteScriptFunction::WebViewInternalExecuteScriptFunction() { |
149 } | 262 } |
150 | 263 |
151 void WebViewInternalExecuteScriptFunction::OnExecuteCodeFinished( | 264 void WebViewInternalExecuteScriptFunction::OnExecuteCodeFinished( |
152 const std::string& error, | 265 const std::string& error, |
153 const GURL& on_url, | 266 const GURL& on_url, |
154 const base::ListValue& result) { | 267 const base::ListValue& result) { |
155 if (error.empty()) | 268 if (error.empty()) |
156 SetResult(result.DeepCopy()); | 269 SetResult(result.DeepCopy()); |
157 WebViewInternalExecuteCodeFunction::OnExecuteCodeFinished( | 270 WebViewInternalExecuteCodeFunction::OnExecuteCodeFinished( |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
522 // Will finish asynchronously. | 635 // Will finish asynchronously. |
523 return true; | 636 return true; |
524 } | 637 } |
525 | 638 |
526 void WebViewInternalClearDataFunction::ClearDataDone() { | 639 void WebViewInternalClearDataFunction::ClearDataDone() { |
527 Release(); // Balanced in RunAsync(). | 640 Release(); // Balanced in RunAsync(). |
528 SendResponse(true); | 641 SendResponse(true); |
529 } | 642 } |
530 | 643 |
531 } // namespace extensions | 644 } // namespace extensions |
OLD | NEW |