| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/page_capture/page_capture_api.h" | 5 #include "chrome/browser/extensions/api/page_capture/page_capture_api.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" |
| 11 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/memory/ptr_util.h" |
| 12 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/extensions/extension_tab_util.h" | 15 #include "chrome/browser/extensions/extension_tab_util.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/profiles/profiles_state.h" |
| 15 #include "content/public/browser/child_process_security_policy.h" | 18 #include "content/public/browser/child_process_security_policy.h" |
| 16 #include "content/public/browser/notification_details.h" | 19 #include "content/public/browser/notification_details.h" |
| 17 #include "content/public/browser/notification_source.h" | 20 #include "content/public/browser/notification_source.h" |
| 18 #include "content/public/browser/notification_types.h" | 21 #include "content/public/browser/notification_types.h" |
| 19 #include "content/public/browser/render_frame_host.h" | 22 #include "content/public/browser/render_frame_host.h" |
| 20 #include "content/public/browser/render_process_host.h" | 23 #include "content/public/browser/render_process_host.h" |
| 21 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
| 22 #include "content/public/common/mhtml_generation_params.h" | 25 #include "content/public/common/mhtml_generation_params.h" |
| 23 #include "extensions/common/extension_messages.h" | 26 #include "extensions/common/extension_messages.h" |
| 24 | 27 |
| 28 #if defined(OS_CHROMEOS) |
| 29 #include "chrome/browser/chromeos/extensions/public_session_permission_helper.h" |
| 30 #endif |
| 31 |
| 25 using content::BrowserThread; | 32 using content::BrowserThread; |
| 26 using content::ChildProcessSecurityPolicy; | 33 using content::ChildProcessSecurityPolicy; |
| 27 using content::WebContents; | 34 using content::WebContents; |
| 28 using extensions::PageCaptureSaveAsMHTMLFunction; | 35 using extensions::PageCaptureSaveAsMHTMLFunction; |
| 29 using storage::ShareableFileReference; | 36 using storage::ShareableFileReference; |
| 30 | 37 |
| 31 namespace SaveAsMHTML = extensions::api::page_capture::SaveAsMHTML; | 38 namespace SaveAsMHTML = extensions::api::page_capture::SaveAsMHTML; |
| 32 | 39 |
| 33 namespace { | 40 namespace { |
| 34 | 41 |
| 35 const char kFileTooBigError[] = "The MHTML file generated is too big."; | 42 const char kFileTooBigError[] = "The MHTML file generated is too big."; |
| 36 const char kMHTMLGenerationFailedError[] = "Failed to generate MHTML."; | 43 const char kMHTMLGenerationFailedError[] = "Failed to generate MHTML."; |
| 37 const char kTemporaryFileError[] = "Failed to create a temporary file."; | 44 const char kTemporaryFileError[] = "Failed to create a temporary file."; |
| 38 const char kTabClosedError[] = "Cannot find the tab for this request."; | 45 const char kTabClosedError[] = "Cannot find the tab for this request."; |
| 46 #if defined(OS_CHROMEOS) |
| 47 const char kUserDenied[] = "User denied request."; |
| 48 #endif |
| 39 | 49 |
| 40 } // namespace | 50 } // namespace |
| 41 | 51 |
| 42 static PageCaptureSaveAsMHTMLFunction::TestDelegate* test_delegate_ = NULL; | 52 static PageCaptureSaveAsMHTMLFunction::TestDelegate* test_delegate_ = NULL; |
| 43 | 53 |
| 44 PageCaptureSaveAsMHTMLFunction::PageCaptureSaveAsMHTMLFunction() { | 54 PageCaptureSaveAsMHTMLFunction::PageCaptureSaveAsMHTMLFunction() { |
| 45 } | 55 } |
| 46 | 56 |
| 47 PageCaptureSaveAsMHTMLFunction::~PageCaptureSaveAsMHTMLFunction() { | 57 PageCaptureSaveAsMHTMLFunction::~PageCaptureSaveAsMHTMLFunction() { |
| 48 if (mhtml_file_.get()) { | 58 if (mhtml_file_.get()) { |
| 49 storage::ShareableFileReference* to_release = mhtml_file_.get(); | 59 storage::ShareableFileReference* to_release = mhtml_file_.get(); |
| 50 to_release->AddRef(); | 60 to_release->AddRef(); |
| 51 mhtml_file_ = NULL; | 61 mhtml_file_ = NULL; |
| 52 BrowserThread::ReleaseSoon(BrowserThread::IO, FROM_HERE, to_release); | 62 BrowserThread::ReleaseSoon(BrowserThread::IO, FROM_HERE, to_release); |
| 53 } | 63 } |
| 54 } | 64 } |
| 55 | 65 |
| 56 void PageCaptureSaveAsMHTMLFunction::SetTestDelegate(TestDelegate* delegate) { | 66 void PageCaptureSaveAsMHTMLFunction::SetTestDelegate(TestDelegate* delegate) { |
| 57 test_delegate_ = delegate; | 67 test_delegate_ = delegate; |
| 58 } | 68 } |
| 59 | 69 |
| 60 bool PageCaptureSaveAsMHTMLFunction::RunAsync() { | 70 bool PageCaptureSaveAsMHTMLFunction::RunAsync() { |
| 61 params_ = SaveAsMHTML::Params::Create(*args_); | 71 params_ = SaveAsMHTML::Params::Create(*args_); |
| 62 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 72 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 63 | 73 |
| 64 AddRef(); // Balanced in ReturnFailure/ReturnSuccess() | 74 AddRef(); // Balanced in ReturnFailure/ReturnSuccess() |
| 65 | 75 |
| 76 // In Public Sessions, extensions (and apps) are force-installed by admin |
| 77 // policy so the user does not get a chance to review the permissions for |
| 78 // these extensions. This is not acceptable from a security/privacy |
| 79 // standpoint, so when an extension uses the PageCapture API for the first |
| 80 // time, we show the user a dialog where they can choose whether to allow the |
| 81 // extension access to the API. |
| 82 #if defined(OS_CHROMEOS) |
| 83 if (profiles::IsPublicSession()) { |
| 84 WebContents* web_contents = GetWebContents(); |
| 85 if (!web_contents) { |
| 86 ReturnFailure(kTabClosedError); |
| 87 return true; |
| 88 } |
| 89 // This Unretained is safe because this object is Released() in |
| 90 // OnMessageReceived which gets called at some point after callback is run. |
| 91 auto callback = |
| 92 base::Bind(&PageCaptureSaveAsMHTMLFunction::ResolvePermissionRequest, |
| 93 base::Unretained(this)); |
| 94 permission_helper::HandlePermissionRequest( |
| 95 *extension(), {APIPermission::kPageCapture}, web_contents, callback); |
| 96 return true; |
| 97 } |
| 98 #endif |
| 99 |
| 66 BrowserThread::PostTask( | 100 BrowserThread::PostTask( |
| 67 BrowserThread::FILE, FROM_HERE, | 101 BrowserThread::FILE, FROM_HERE, |
| 68 base::Bind(&PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile, this)); | 102 base::Bind(&PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile, this)); |
| 69 return true; | 103 return true; |
| 70 } | 104 } |
| 71 | 105 |
| 72 bool PageCaptureSaveAsMHTMLFunction::OnMessageReceived( | 106 bool PageCaptureSaveAsMHTMLFunction::OnMessageReceived( |
| 73 const IPC::Message& message) { | 107 const IPC::Message& message) { |
| 74 if (message.type() != ExtensionHostMsg_ResponseAck::ID) | 108 if (message.type() != ExtensionHostMsg_ResponseAck::ID) |
| 75 return false; | 109 return false; |
| 76 | 110 |
| 77 int message_request_id; | 111 int message_request_id; |
| 78 base::PickleIterator iter(message); | 112 base::PickleIterator iter(message); |
| 79 if (!iter.ReadInt(&message_request_id)) { | 113 if (!iter.ReadInt(&message_request_id)) { |
| 80 NOTREACHED() << "malformed extension message"; | 114 NOTREACHED() << "malformed extension message"; |
| 81 return true; | 115 return true; |
| 82 } | 116 } |
| 83 | 117 |
| 84 if (message_request_id != request_id()) | 118 if (message_request_id != request_id()) |
| 85 return false; | 119 return false; |
| 86 | 120 |
| 87 // The extension process has processed the response and has created a | 121 // The extension process has processed the response and has created a |
| 88 // reference to the blob, it is safe for us to go away. | 122 // reference to the blob, it is safe for us to go away. |
| 89 Release(); // Balanced in Run() | 123 Release(); // Balanced in Run() |
| 90 | 124 |
| 91 return true; | 125 return true; |
| 92 } | 126 } |
| 93 | 127 |
| 128 #if defined (OS_CHROMEOS) |
| 129 void PageCaptureSaveAsMHTMLFunction::ResolvePermissionRequest( |
| 130 PermissionIDSet allowed_permissions) { |
| 131 if (allowed_permissions.ContainsID(APIPermission::kPageCapture)) { |
| 132 BrowserThread::PostTask( |
| 133 BrowserThread::FILE, FROM_HERE, |
| 134 base::Bind(&PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile, this)); |
| 135 } else { |
| 136 ReturnFailure(kUserDenied); |
| 137 } |
| 138 } |
| 139 #endif |
| 140 |
| 94 void PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile() { | 141 void PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile() { |
| 95 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 142 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 96 bool success = base::CreateTemporaryFile(&mhtml_path_); | 143 bool success = base::CreateTemporaryFile(&mhtml_path_); |
| 97 BrowserThread::PostTask( | 144 BrowserThread::PostTask( |
| 98 BrowserThread::IO, FROM_HERE, | 145 BrowserThread::IO, FROM_HERE, |
| 99 base::Bind(&PageCaptureSaveAsMHTMLFunction::TemporaryFileCreated, this, | 146 base::Bind(&PageCaptureSaveAsMHTMLFunction::TemporaryFileCreated, this, |
| 100 success)); | 147 success)); |
| 101 } | 148 } |
| 102 | 149 |
| 103 void PageCaptureSaveAsMHTMLFunction::TemporaryFileCreated(bool success) { | 150 void PageCaptureSaveAsMHTMLFunction::TemporaryFileCreated(bool success) { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 GetProfile(), | 240 GetProfile(), |
| 194 include_incognito(), | 241 include_incognito(), |
| 195 &browser, | 242 &browser, |
| 196 NULL, | 243 NULL, |
| 197 &web_contents, | 244 &web_contents, |
| 198 NULL)) { | 245 NULL)) { |
| 199 return NULL; | 246 return NULL; |
| 200 } | 247 } |
| 201 return web_contents; | 248 return web_contents; |
| 202 } | 249 } |
| OLD | NEW |