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 GURL& url, | |
68 const WebViewInternalExecuteCodeFunction::WebUILoadFileCallback& callback) | |
69 : context_(context), url_(url), callback_(callback) {} | |
70 ~WebUIURLFetcher() override {} | |
71 | |
72 void Start(int render_process_id, int render_view_id) { | |
73 fetcher_.reset(net::URLFetcher::Create(url_, net::URLFetcher::GET, this)); | |
74 fetcher_->SetRequestContext(context_->GetRequestContext()); | |
75 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); | |
76 | |
77 content::AssociateURLFetcherWithRenderFrame( | |
78 fetcher_.get(), url_, render_process_id, render_view_id); | |
79 fetcher_->Start(); | |
80 } | |
81 | |
82 private: | |
83 // net::URLFetcherDelegate: | |
84 void OnURLFetchComplete(const net::URLFetcher* source) override { | |
85 CHECK_EQ(fetcher_.get(), source); | |
86 | |
87 std::string data; | |
88 bool result = false; | |
89 if (fetcher_->GetStatus().status() == net::URLRequestStatus::SUCCESS) { | |
90 result = fetcher_->GetResponseAsString(&data); | |
91 DCHECK(result); | |
92 } | |
93 fetcher_.reset(); | |
94 callback_.Run(result, data); | |
95 } | |
96 | |
97 content::BrowserContext* context_; | |
98 GURL url_; | |
Devlin
2015/03/25 17:55:16
There's still not really a point in persisting the
Xi Han
2015/03/25 20:49:21
Removed.
| |
99 const WebViewInternalExecuteCodeFunction::WebUILoadFileCallback callback_; | |
100 scoped_ptr<net::URLFetcher> fetcher_; | |
101 | |
102 DISALLOW_COPY_AND_ASSIGN(WebUIURLFetcher); | |
103 }; | |
104 | |
52 bool WebViewInternalExtensionFunction::RunAsync() { | 105 bool WebViewInternalExtensionFunction::RunAsync() { |
53 int instance_id = 0; | 106 int instance_id = 0; |
54 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); | 107 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); |
55 WebViewGuest* guest = WebViewGuest::From( | 108 WebViewGuest* guest = WebViewGuest::From( |
56 render_view_host()->GetProcess()->GetID(), instance_id); | 109 render_view_host()->GetProcess()->GetID(), instance_id); |
57 if (!guest) | 110 if (!guest) |
58 return false; | 111 return false; |
59 | 112 |
60 return RunAsyncSafe(guest); | 113 return RunAsyncSafe(guest); |
61 } | 114 } |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
138 } | 191 } |
139 | 192 |
140 bool WebViewInternalExecuteCodeFunction::IsWebView() const { | 193 bool WebViewInternalExecuteCodeFunction::IsWebView() const { |
141 return true; | 194 return true; |
142 } | 195 } |
143 | 196 |
144 const GURL& WebViewInternalExecuteCodeFunction::GetWebViewSrc() const { | 197 const GURL& WebViewInternalExecuteCodeFunction::GetWebViewSrc() const { |
145 return guest_src_; | 198 return guest_src_; |
146 } | 199 } |
147 | 200 |
201 bool WebViewInternalExecuteCodeFunction::LoadFileForWebUI( | |
202 const std::string& file_src, | |
203 const WebUILoadFileCallback& callback) { | |
204 if (!render_view_host() || !render_view_host()->GetProcess()) | |
205 return false; | |
206 WebViewGuest* guest = WebViewGuest::From( | |
207 render_view_host()->GetProcess()->GetID(), guest_instance_id_); | |
208 if (!guest || host_id().type() != HostID::WEBUI) | |
209 return false; | |
210 | |
211 GURL owner_base_url(guest->GetOwnerSiteURL().GetWithEmptyPath()); | |
212 GURL file_url(owner_base_url.Resolve(file_src)); | |
213 | |
214 url_fetcher_.reset( | |
215 new WebUIURLFetcher(this->browser_context(), file_url, callback)); | |
216 url_fetcher_->Start(render_view_host()->GetProcess()->GetID(), | |
217 render_view_host()->GetRoutingID()); | |
218 return true; | |
219 } | |
220 | |
221 bool WebViewInternalExecuteCodeFunction::LoadFile(const std::string& file) { | |
222 if (!extension()) { | |
223 bool is_success = false; | |
224 is_success = LoadFileForWebUI( | |
225 *details_->file, | |
226 base::Bind(&WebViewInternalExecuteCodeFunction::DidLoadFileForWebUI, | |
Devlin
2015/03/25 17:55:15
Just pass in DidLoadAndLocalizeFile(); there shoul
Devlin
2015/03/25 20:57:49
Missed this one?
Xi Han
2015/03/25 22:07:15
Done.
| |
227 this, file)); | |
228 if (is_success) | |
Devlin
2015/03/25 17:55:15
inline is_success
Xi Han
2015/03/25 20:49:21
Just confirmed what does "inline" mean here. Updat
| |
229 return true; | |
230 | |
231 SendResponse(false); | |
232 error_ = ErrorUtils::FormatErrorMessage(kLoadFileError, file); | |
233 return false; | |
234 } | |
235 return ExecuteCodeFunction::LoadFile(file); | |
236 } | |
237 | |
238 void WebViewInternalExecuteCodeFunction::DidLoadFileForWebUI( | |
239 const std::string& file, | |
240 bool success, | |
241 const std::string& data) { | |
242 DidLoadFileForHost(file, success, data); | |
243 url_fetcher_.reset(); | |
244 } | |
245 | |
148 WebViewInternalExecuteScriptFunction::WebViewInternalExecuteScriptFunction() { | 246 WebViewInternalExecuteScriptFunction::WebViewInternalExecuteScriptFunction() { |
149 } | 247 } |
150 | 248 |
151 void WebViewInternalExecuteScriptFunction::OnExecuteCodeFinished( | 249 void WebViewInternalExecuteScriptFunction::OnExecuteCodeFinished( |
152 const std::string& error, | 250 const std::string& error, |
153 const GURL& on_url, | 251 const GURL& on_url, |
154 const base::ListValue& result) { | 252 const base::ListValue& result) { |
155 if (error.empty()) | 253 if (error.empty()) |
156 SetResult(result.DeepCopy()); | 254 SetResult(result.DeepCopy()); |
157 WebViewInternalExecuteCodeFunction::OnExecuteCodeFinished( | 255 WebViewInternalExecuteCodeFunction::OnExecuteCodeFinished( |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
522 // Will finish asynchronously. | 620 // Will finish asynchronously. |
523 return true; | 621 return true; |
524 } | 622 } |
525 | 623 |
526 void WebViewInternalClearDataFunction::ClearDataDone() { | 624 void WebViewInternalClearDataFunction::ClearDataDone() { |
527 Release(); // Balanced in RunAsync(). | 625 Release(); // Balanced in RunAsync(). |
528 SendResponse(true); | 626 SendResponse(true); |
529 } | 627 } |
530 | 628 |
531 } // namespace extensions | 629 } // namespace extensions |
OLD | NEW |