#include "base/bind.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
+#include "base/files/file_util.h"
#include "base/json/json_string_value_serializer.h"
#include "base/logging.h"
#include "base/pickle.h"
#include "base/strings/utf_string_conversions.h"
+#include "base/task/task_runner_util.h"
#include "browser/javascript_dialog_manager_efl.h"
#include "browser/javascript_interface/gin_native_bridge_dispatcher_host.h"
#include "browser/navigation_policy_handler_efl.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
+#include "content/public/common/mhtml_generation_params.h"
#include "content/public/common/user_agent.h"
+#include "net/base/filename_util.h"
#include "permission_popup_manager.cc"
#include "private/ewk_back_forward_list_private.h"
#include "private/ewk_context_private.h"
namespace {
+const int kTitleLengthMax = 80;
+const base::FilePath::CharType kMHTMLFileNameExtension[] =
+ FILE_PATH_LITERAL(".mhtml");
+const base::FilePath::CharType kMHTMLExtension[] = FILE_PATH_LITERAL("mhtml");
+const base::FilePath::CharType kDefaultFileName[] =
+ FILE_PATH_LITERAL("saved_page");
+const char kReplaceChars[] = " ";
+const char kReplaceWith[] = "_";
+
static const char* kRendererCrashedHTMLMessage =
"<html><body><h1>Renderer process has crashed!</h1></body></html>";
return NULL;
}
+base::FilePath GenerateMHTMLFilePath(const GURL& url,
+ const std::string& title,
+ const std::string& base_path) {
+ base::FilePath file_path(base_path);
+
+ if (base::DirectoryExists(file_path)) {
+ std::string title_part(title.substr(0, kTitleLengthMax));
+ base::ReplaceChars(title_part, kReplaceChars, kReplaceWith, &title_part);
+ base::FilePath file_name =
+ net::GenerateFileName(url, std::string(), std::string(), title_part,
+ std::string(), kDefaultFileName);
+ DCHECK(!file_name.empty());
+ file_path = file_path.Append(file_name);
+ }
+
+ if (file_path.Extension().empty())
+ file_path = file_path.AddExtension(kMHTMLExtension);
+ else if (!file_path.MatchesExtension(kMHTMLFileNameExtension))
+ file_path = file_path.ReplaceExtension(kMHTMLExtension);
+
+ if (!base::PathExists(file_path))
+ return file_path;
+
+ int uniquifier = base::GetUniquePathNumber(file_path);
+ if (uniquifier > 0) {
+ return file_path.InsertBeforeExtensionASCII(
+ base::StringPrintf(" (%d)", uniquifier));
+ }
+
+ return base::FilePath();
+}
+
} // namespace
class WebViewAsyncRequestHitTestDataCallback {
mhtml_callback_map_.Remove(callback_id);
}
+bool EWebView::SavePageAsMHTML(const std::string& path,
+ Ewk_View_Save_Page_Callback callback,
+ void* user_data) {
+ if (!web_contents_)
+ return false;
+
+ GURL url(web_contents_->GetLastCommittedURL());
+ std::u16string title(web_contents_->GetTitle());
+
+ // Post function that has file access to blocking task runner.
+ return base::PostTaskAndReplyWithResult(
+ base::ThreadPool::CreateSequencedTaskRunner(
+ {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
+ base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN})
+ .get(),
+ FROM_HERE,
+ base::BindOnce(&GenerateMHTMLFilePath, url, base::UTF16ToUTF8(title),
+ path),
+ base::BindOnce(&EWebView::GenerateMHTML, base::Unretained(this), callback,
+ user_data));
+}
+
+void EWebView::GenerateMHTML(Ewk_View_Save_Page_Callback callback,
+ void* user_data,
+ const base::FilePath& file_path) {
+ if (file_path.empty()) {
+ LOG(ERROR) << "Generating file path was failed";
+ callback(evas_object_, nullptr, user_data);
+ return;
+ }
+
+ MHTMLGenerationParams params(file_path);
+ web_contents_->GenerateMHTML(
+ params, base::BindOnce(&EWebView::MHTMLGenerated, base::Unretained(this),
+ callback, user_data, file_path));
+}
+
+void EWebView::MHTMLGenerated(Ewk_View_Save_Page_Callback callback,
+ void* user_data,
+ const base::FilePath& file_path,
+ int64_t file_size) {
+ callback(evas_object_, file_size > 0 ? file_path.value().c_str() : nullptr,
+ user_data);
+}
+
bool EWebView::IsFullscreen() {
return web_contents_delegate_->IsFullscreenForTabOrPending(
web_contents_.get());