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 "extensions/common/error_utils.h" | |
18 #include "net/base/load_flags.h" | |
19 #include "net/url_request/url_fetcher.h" | |
20 #include "net/url_request/url_fetcher_delegate.h" | |
15 #include "third_party/WebKit/public/web/WebFindOptions.h" | 21 #include "third_party/WebKit/public/web/WebFindOptions.h" |
16 | 22 |
17 using content::WebContents; | 23 using content::WebContents; |
18 using extensions::core_api::web_view_internal::SetPermission::Params; | 24 using extensions::core_api::web_view_internal::SetPermission::Params; |
19 using extensions::core_api::extension_types::InjectDetails; | 25 using extensions::core_api::extension_types::InjectDetails; |
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"; |
30 const char kSinceKey[] = "since"; | 36 const char kSinceKey[] = "since"; |
37 const char kLoadFileError[] = "Failed to load file: \"*\". "; | |
31 | 38 |
32 int MaskForKey(const char* key) { | 39 int MaskForKey(const char* key) { |
33 if (strcmp(key, kAppCacheKey) == 0) | 40 if (strcmp(key, kAppCacheKey) == 0) |
34 return content::StoragePartition::REMOVE_DATA_MASK_APPCACHE; | 41 return content::StoragePartition::REMOVE_DATA_MASK_APPCACHE; |
35 if (strcmp(key, kCookiesKey) == 0) | 42 if (strcmp(key, kCookiesKey) == 0) |
36 return content::StoragePartition::REMOVE_DATA_MASK_COOKIES; | 43 return content::StoragePartition::REMOVE_DATA_MASK_COOKIES; |
37 if (strcmp(key, kFileSystemsKey) == 0) | 44 if (strcmp(key, kFileSystemsKey) == 0) |
38 return content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS; | 45 return content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS; |
39 if (strcmp(key, kIndexedDBKey) == 0) | 46 if (strcmp(key, kIndexedDBKey) == 0) |
40 return content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB; | 47 return content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB; |
41 if (strcmp(key, kLocalStorageKey) == 0) | 48 if (strcmp(key, kLocalStorageKey) == 0) |
42 return content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE; | 49 return content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE; |
43 if (strcmp(key, kWebSQLKey) == 0) | 50 if (strcmp(key, kWebSQLKey) == 0) |
44 return content::StoragePartition::REMOVE_DATA_MASK_WEBSQL; | 51 return content::StoragePartition::REMOVE_DATA_MASK_WEBSQL; |
45 return 0; | 52 return 0; |
46 } | 53 } |
47 | 54 |
48 } // namespace | 55 } // namespace |
49 | 56 |
50 namespace extensions { | 57 namespace extensions { |
51 | 58 |
59 // WebUIURLFetcher downloads the content of a file by giving its |url| on WebUI. | |
60 // Each WebUIURLFetcher is associated with a given |render_process_id, | |
61 // render_view_id| pair. | |
62 class WebViewInternalExecuteCodeFunction::WebUIURLFetcher | |
63 : public net::URLFetcherDelegate { | |
64 public: | |
65 WebUIURLFetcher( | |
66 content::BrowserContext* context, | |
67 const WebViewInternalExecuteCodeFunction::WebUILoadFileCallback& callback) | |
68 : context_(context), callback_(callback) {} | |
69 ~WebUIURLFetcher() override {} | |
70 | |
71 void Start(int render_process_id, int render_view_id, const GURL& url) { | |
72 fetcher_.reset(net::URLFetcher::Create(url, net::URLFetcher::GET, this)); | |
73 fetcher_->SetRequestContext(context_->GetRequestContext()); | |
74 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); | |
75 | |
76 content::AssociateURLFetcherWithRenderFrame( | |
77 fetcher_.get(), url, render_process_id, render_view_id); | |
78 fetcher_->Start(); | |
79 } | |
80 | |
81 private: | |
82 // net::URLFetcherDelegate: | |
83 void OnURLFetchComplete(const net::URLFetcher* source) override { | |
84 CHECK_EQ(fetcher_.get(), source); | |
85 | |
86 std::string data; | |
87 bool result = false; | |
88 if (fetcher_->GetStatus().status() == net::URLRequestStatus::SUCCESS) { | |
89 result = fetcher_->GetResponseAsString(&data); | |
90 DCHECK(result); | |
91 } | |
92 fetcher_.reset(); | |
93 callback_.Run(result, data); | |
Devlin
2015/03/25 21:14:11
You'll also need to call callback_.reset() so that
Xi Han
2015/03/25 22:07:15
Thanks for your help. Updated.
| |
94 } | |
95 | |
96 content::BrowserContext* context_; | |
97 const WebViewInternalExecuteCodeFunction::WebUILoadFileCallback callback_; | |
98 scoped_ptr<net::URLFetcher> fetcher_; | |
99 | |
100 DISALLOW_COPY_AND_ASSIGN(WebUIURLFetcher); | |
101 }; | |
102 | |
52 bool WebViewInternalExtensionFunction::RunAsync() { | 103 bool WebViewInternalExtensionFunction::RunAsync() { |
53 int instance_id = 0; | 104 int instance_id = 0; |
54 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); | 105 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); |
55 WebViewGuest* guest = WebViewGuest::From( | 106 WebViewGuest* guest = WebViewGuest::From( |
56 render_view_host()->GetProcess()->GetID(), instance_id); | 107 render_view_host()->GetProcess()->GetID(), instance_id); |
57 if (!guest) | 108 if (!guest) |
58 return false; | 109 return false; |
59 | 110 |
60 return RunAsyncSafe(guest); | 111 return RunAsyncSafe(guest); |
61 } | 112 } |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
138 } | 189 } |
139 | 190 |
140 bool WebViewInternalExecuteCodeFunction::IsWebView() const { | 191 bool WebViewInternalExecuteCodeFunction::IsWebView() const { |
141 return true; | 192 return true; |
142 } | 193 } |
143 | 194 |
144 const GURL& WebViewInternalExecuteCodeFunction::GetWebViewSrc() const { | 195 const GURL& WebViewInternalExecuteCodeFunction::GetWebViewSrc() const { |
145 return guest_src_; | 196 return guest_src_; |
146 } | 197 } |
147 | 198 |
199 bool WebViewInternalExecuteCodeFunction::LoadFileForWebUI( | |
200 const std::string& file_src, | |
201 const WebUILoadFileCallback& callback) { | |
202 if (!render_view_host() || !render_view_host()->GetProcess()) | |
203 return false; | |
204 WebViewGuest* guest = WebViewGuest::From( | |
205 render_view_host()->GetProcess()->GetID(), guest_instance_id_); | |
206 if (!guest || host_id().type() != HostID::WEBUI) | |
207 return false; | |
208 | |
209 GURL owner_base_url(guest->GetOwnerSiteURL().GetWithEmptyPath()); | |
210 GURL file_url(owner_base_url.Resolve(file_src)); | |
211 | |
212 url_fetcher_.reset(new WebUIURLFetcher(this->browser_context(), callback)); | |
213 url_fetcher_->Start(render_view_host()->GetProcess()->GetID(), | |
214 render_view_host()->GetRoutingID(), file_url); | |
215 return true; | |
216 } | |
217 | |
218 bool WebViewInternalExecuteCodeFunction::LoadFile(const std::string& file) { | |
219 if (!extension()) { | |
220 if (LoadFileForWebUI( | |
221 *details_->file, | |
222 base::Bind(&WebViewInternalExecuteCodeFunction::DidLoadFileForWebUI, | |
223 this, file))) | |
224 return true; | |
225 | |
226 SendResponse(false); | |
227 error_ = ErrorUtils::FormatErrorMessage(kLoadFileError, file); | |
228 return false; | |
229 } | |
230 return ExecuteCodeFunction::LoadFile(file); | |
231 } | |
232 | |
233 void WebViewInternalExecuteCodeFunction::DidLoadFileForWebUI( | |
234 const std::string& file, | |
235 bool success, | |
236 const std::string& data) { | |
237 DidLoadAndLocalizeFile(file, success, data); | |
238 url_fetcher_.reset(); | |
239 } | |
240 | |
148 WebViewInternalExecuteScriptFunction::WebViewInternalExecuteScriptFunction() { | 241 WebViewInternalExecuteScriptFunction::WebViewInternalExecuteScriptFunction() { |
149 } | 242 } |
150 | 243 |
151 void WebViewInternalExecuteScriptFunction::OnExecuteCodeFinished( | 244 void WebViewInternalExecuteScriptFunction::OnExecuteCodeFinished( |
152 const std::string& error, | 245 const std::string& error, |
153 const GURL& on_url, | 246 const GURL& on_url, |
154 const base::ListValue& result) { | 247 const base::ListValue& result) { |
155 if (error.empty()) | 248 if (error.empty()) |
156 SetResult(result.DeepCopy()); | 249 SetResult(result.DeepCopy()); |
157 WebViewInternalExecuteCodeFunction::OnExecuteCodeFinished( | 250 WebViewInternalExecuteCodeFunction::OnExecuteCodeFinished( |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
522 // Will finish asynchronously. | 615 // Will finish asynchronously. |
523 return true; | 616 return true; |
524 } | 617 } |
525 | 618 |
526 void WebViewInternalClearDataFunction::ClearDataDone() { | 619 void WebViewInternalClearDataFunction::ClearDataDone() { |
527 Release(); // Balanced in RunAsync(). | 620 Release(); // Balanced in RunAsync(). |
528 SendResponse(true); | 621 SendResponse(true); |
529 } | 622 } |
530 | 623 |
531 } // namespace extensions | 624 } // namespace extensions |
OLD | NEW |