Chromium Code Reviews| Index: extensions/browser/api/guest_view/web_view/web_view_internal_api.cc |
| diff --git a/extensions/browser/api/guest_view/web_view/web_view_internal_api.cc b/extensions/browser/api/guest_view/web_view/web_view_internal_api.cc |
| index c55733559faf6d05473f668707230c2e577cd733..a29b010b40a5c3f2b7c62989dce9dc17601e4934 100644 |
| --- a/extensions/browser/api/guest_view/web_view/web_view_internal_api.cc |
| +++ b/extensions/browser/api/guest_view/web_view/web_view_internal_api.cc |
| @@ -6,12 +6,18 @@ |
| #include "base/strings/stringprintf.h" |
| #include "base/strings/utf_string_conversions.h" |
| +#include "content/public/browser/browser_context.h" |
| #include "content/public/browser/render_process_host.h" |
| #include "content/public/browser/render_view_host.h" |
| #include "content/public/browser/storage_partition.h" |
| #include "content/public/browser/web_contents.h" |
| #include "content/public/common/stop_find_action.h" |
| +#include "content/public/common/url_fetcher.h" |
| #include "extensions/common/api/web_view_internal.h" |
| +#include "extensions/common/error_utils.h" |
| +#include "net/base/load_flags.h" |
| +#include "net/url_request/url_fetcher.h" |
| +#include "net/url_request/url_fetcher_delegate.h" |
| #include "third_party/WebKit/public/web/WebFindOptions.h" |
| using content::WebContents; |
| @@ -28,6 +34,7 @@ const char kIndexedDBKey[] = "indexedDB"; |
| const char kLocalStorageKey[] = "localStorage"; |
| const char kWebSQLKey[] = "webSQL"; |
| const char kSinceKey[] = "since"; |
| +const char kLoadFileError[] = "Failed to load file: \"*\". "; |
| int MaskForKey(const char* key) { |
| if (strcmp(key, kAppCacheKey) == 0) |
| @@ -49,6 +56,52 @@ int MaskForKey(const char* key) { |
| namespace extensions { |
| +// WebUIURLFetcher downloads the content of a file by giving its |url| on WebUI. |
| +// Each WebUIURLFetcher is associated with a given |render_process_id, |
| +// render_view_id| pair. |
| +class WebViewInternalExecuteCodeFunction::WebUIURLFetcher |
| + : public net::URLFetcherDelegate { |
| + public: |
| + WebUIURLFetcher( |
| + content::BrowserContext* context, |
| + const GURL& url, |
| + const WebViewInternalExecuteCodeFunction::WebUILoadFileCallback& callback) |
| + : context_(context), url_(url), callback_(callback) {} |
| + ~WebUIURLFetcher() override {} |
| + |
| + void Start(int render_process_id, int render_view_id) { |
| + fetcher_.reset(net::URLFetcher::Create(url_, net::URLFetcher::GET, this)); |
| + fetcher_->SetRequestContext(context_->GetRequestContext()); |
| + fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); |
| + |
| + content::AssociateURLFetcherWithRenderFrame( |
| + fetcher_.get(), url_, render_process_id, render_view_id); |
| + fetcher_->Start(); |
| + } |
| + |
| + private: |
| + // net::URLFetcherDelegate: |
| + void OnURLFetchComplete(const net::URLFetcher* source) override { |
| + CHECK_EQ(fetcher_.get(), source); |
| + |
| + std::string data; |
| + bool result = false; |
| + if (fetcher_->GetStatus().status() == net::URLRequestStatus::SUCCESS) { |
| + result = fetcher_->GetResponseAsString(&data); |
| + DCHECK(result); |
| + } |
| + fetcher_.reset(); |
| + callback_.Run(result, data); |
| + } |
| + |
| + content::BrowserContext* context_; |
| + 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.
|
| + const WebViewInternalExecuteCodeFunction::WebUILoadFileCallback callback_; |
| + scoped_ptr<net::URLFetcher> fetcher_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(WebUIURLFetcher); |
| +}; |
| + |
| bool WebViewInternalExtensionFunction::RunAsync() { |
| int instance_id = 0; |
| EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); |
| @@ -145,6 +198,51 @@ const GURL& WebViewInternalExecuteCodeFunction::GetWebViewSrc() const { |
| return guest_src_; |
| } |
| +bool WebViewInternalExecuteCodeFunction::LoadFileForWebUI( |
| + const std::string& file_src, |
| + const WebUILoadFileCallback& callback) { |
| + if (!render_view_host() || !render_view_host()->GetProcess()) |
| + return false; |
| + WebViewGuest* guest = WebViewGuest::From( |
| + render_view_host()->GetProcess()->GetID(), guest_instance_id_); |
| + if (!guest || host_id().type() != HostID::WEBUI) |
| + return false; |
| + |
| + GURL owner_base_url(guest->GetOwnerSiteURL().GetWithEmptyPath()); |
| + GURL file_url(owner_base_url.Resolve(file_src)); |
| + |
| + url_fetcher_.reset( |
| + new WebUIURLFetcher(this->browser_context(), file_url, callback)); |
| + url_fetcher_->Start(render_view_host()->GetProcess()->GetID(), |
| + render_view_host()->GetRoutingID()); |
| + return true; |
| +} |
| + |
| +bool WebViewInternalExecuteCodeFunction::LoadFile(const std::string& file) { |
| + if (!extension()) { |
| + bool is_success = false; |
| + is_success = LoadFileForWebUI( |
| + *details_->file, |
| + 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.
|
| + this, file)); |
| + 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
|
| + return true; |
| + |
| + SendResponse(false); |
| + error_ = ErrorUtils::FormatErrorMessage(kLoadFileError, file); |
| + return false; |
| + } |
| + return ExecuteCodeFunction::LoadFile(file); |
| +} |
| + |
| +void WebViewInternalExecuteCodeFunction::DidLoadFileForWebUI( |
| + const std::string& file, |
| + bool success, |
| + const std::string& data) { |
| + DidLoadFileForHost(file, success, data); |
| + url_fetcher_.reset(); |
| +} |
| + |
| WebViewInternalExecuteScriptFunction::WebViewInternalExecuteScriptFunction() { |
| } |