text:
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "IECommandExecutor.h"

#include <algorithm>
#include <ctime>
#include <vector>
#include <mutex>

#include <iepmapi.h>

#include "command_types.h"
#include "errorcodes.h"
#include "logging.h"
#include "response.h"

#include "Alert.h"
#include "Browser.h"
#include "BrowserFactory.h"
#include "CommandExecutor.h"
#include "CommandHandlerRepository.h"
#include "CookieManager.h"
#include "Element.h"
#include "ElementFinder.h"
#include "ElementRepository.h"
#include "IECommandHandler.h"
#include "InputManager.h"
#include "HtmlDialog.h"
#include "ProxyManager.h"
#include "StringUtilities.h"
#include "Script.h"
#include "WebDriverConstants.h"
#include "WindowUtilities.h"

#define MAX_HTML_DIALOG_RETRIES 5
#define WAIT_TIME_IN_MILLISECONDS 50
#define DEFAULT_SCRIPT_TIMEOUT_IN_MILLISECONDS 30000
#define DEFAULT_PAGE_LOAD_TIMEOUT_IN_MILLISECONDS 300000
#define DEFAULT_FILE_UPLOAD_DIALOG_TIMEOUT_IN_MILLISECONDS 3000
#define DEFAULT_BROWSER_REATTACH_TIMEOUT_IN_MILLISECONDS 10000

namespace webdriver {

struct WaitThreadContext {
  HWND window_handle;
  bool is_deferred_command;
  LPSTR deferred_response;
};

struct DelayPostMessageThreadContext {
  HWND window_handle;
  DWORD delay;
  UINT msg;
};

LRESULT IECommandExecutor::OnCreate(UINT uMsg,
                                    WPARAM wParam,
                                    LPARAM lParam,
                                    BOOL& bHandled) {
  LOG(TRACE) << "Entering IECommandExecutor::OnCreate";

  CREATESTRUCT* create = reinterpret_cast<CREATESTRUCT*>(lParam);
  IECommandExecutorThreadContext* context = reinterpret_cast<IECommandExecutorThreadContext*>(create->lpCreateParams);
  this->port_ = context->port;

  // NOTE: COM should be initialized on this thread, so we
  // could use CoCreateGuid() and StringFromGUID2() instead.
  UUID guid;
  RPC_WSTR guid_string = NULL;
  RPC_STATUS status = ::UuidCreate(&guid);
  status = ::UuidToString(&guid, &guid_string);

  // RPC_WSTR is currently typedef'd in RpcDce.h (pulled in by rpc.h)
  // as unsigned short*. It needs to be typedef'd as wchar_t*
  wchar_t* cast_guid_string = reinterpret_cast<wchar_t*>(guid_string);
  this->SetWindowText(cast_guid_string);

  std::string session_id = StringUtilities::ToString(cast_guid_string);
  this->session_id_ = session_id;
  this->is_valid_ = true;

  ::RpcStringFree(&guid_string);

  this->PopulateElementFinderMethods();
  this->current_browser_id_ = "";
  this->serialized_response_ = "";
  this->unexpected_alert_behavior_ = "";
  this->implicit_wait_timeout_ = 0;
  this->async_script_timeout_ = DEFAULT_SCRIPT_TIMEOUT_IN_MILLISECONDS;
  this->page_load_timeout_ = DEFAULT_PAGE_LOAD_TIMEOUT_IN_MILLISECONDS;
  this->reattach_browser_timeout_ = DEFAULT_BROWSER_REATTACH_TIMEOUT_IN_MILLISECONDS;
  this->is_waiting_ = false;
  this->is_quitting_ = false;
  this->is_awaiting_new_window_ = false;
  this->use_strict_file_interactability_ = false;
  this->page_load_strategy_ = "normal";
  this->file_upload_dialog_timeout_ = DEFAULT_FILE_UPLOAD_DIALOG_TIMEOUT_IN_MILLISECONDS;

  this->managed_elements_ = new ElementRepository();
  this->input_manager_ = new InputManager();
  this->proxy_manager_ = new ProxyManager();
  this->factory_ = new BrowserFactory();
  this->element_finder_ = new ElementFinder();
  this->command_handlers_ = new CommandHandlerRepository();

  return 0;
}

LRESULT IECommandExecutor::OnDestroy(UINT uMsg,
                                     WPARAM wParam,
                                     LPARAM lParam,
                                     BOOL& bHandled) {
  LOG(DEBUG) << "Entering IECommandExecutor::OnDestroy";

  LOG(DEBUG) << "Clearing managed element cache";
  this->managed_elements_->Clear();
  delete this->managed_elements_;
  LOG(DEBUG) << "Closing command handler repository";
  delete this->command_handlers_;
  LOG(DEBUG) << "Closing element finder";
  delete this->element_finder_;
  LOG(DEBUG) << "Closing input manager";
  delete this->input_manager_;
  LOG(DEBUG) << "Closing proxy manager";
  delete this->proxy_manager_;
  LOG(DEBUG) << "Closing browser factory";
  delete this->factory_;
  LOG(DEBUG) << "Posting quit message";
  ::PostQuitMessage(0);
  LOG(DEBUG) << "Leaving IECommandExecutor::OnDestroy";
  return 0;
}

LRESULT IECommandExecutor::OnSetCommand(UINT uMsg,
                                        WPARAM wParam,
                                        LPARAM lParam,
                                        BOOL& bHandled) {
  LOG(TRACE) << "Entering IECommandExecutor::OnSetCommand";
  LRESULT set_command_result = 0;

  LPCSTR json_command = reinterpret_cast<LPCSTR>(lParam);
  Command requested_command;
  requested_command.Deserialize(json_command);

  this->set_command_mutex_.lock();
  if (this->current_command_.command_type() == CommandType::NoCommand ||
      requested_command.command_type() == CommandType::Quit) {
    this->current_command_.Deserialize(json_command);
    set_command_result = 1;
  }
  this->set_command_mutex_.unlock();

  return set_command_result;
}

LRESULT IECommandExecutor::OnExecCommand(UINT uMsg,
                                         WPARAM wParam,
                                         LPARAM lParam,
                                         BOOL& bHandled) {
  LOG(TRACE) << "Entering IECommandExecutor::OnExecCommand";

  this->DispatchCommand();
  return 0;
}

LRESULT IECommandExecutor::OnGetResponseLength(UINT uMsg,
                                               WPARAM wParam,
                                               LPARAM lParam,
                                               BOOL& bHandled) {
  // Not logging trace entering IECommandExecutor::OnGetResponseLength,
  // because it is polled repeatedly for a non-zero return value.
  size_t response_length = 0;
  if (!this->is_waiting_) {
    response_length = this->serialized_response_.size();
  }
  return response_length;
}

LRESULT IECommandExecutor::OnGetResponse(UINT uMsg,
                                         WPARAM wParam,
                                         LPARAM lParam,
                                         BOOL& bHandled) {
  LOG(TRACE) << "Entering IECommandExecutor::OnGetResponse";

  LPSTR str = reinterpret_cast<LPSTR>(lParam);
  strcpy_s(str,
           this->serialized_response_.size() + 1,
           this->serialized_response_.c_str());

  // Reset the serialized response for the next command.
  this->serialized_response_ = "";
  this->current_command_.Reset();
  return 0;
}

LRESULT IECommandExecutor::OnWait(UINT uMsg,
                                  WPARAM wParam,
                                  LPARAM lParam,
                                  BOOL& bHandled) {
  LOG(TRACE) << "Entering IECommandExecutor::OnWait";

  LPCSTR str = reinterpret_cast<LPCSTR>(lParam);
  std::string deferred_response(str);
  delete[] str;

  LOG(DEBUG) << "Starting wait cycle.";
  if (this->is_awaiting_new_window_) {
    LOG(DEBUG) << "Awaiting new window. Aborting current wait cycle and "
               << "scheduling another.";
    this->CreateWaitThread(deferred_response);
    return 0;
  }

  bool is_single_wait = (wParam == 0);

  BrowserHandle browser;
  int status_code = this->GetCurrentBrowser(&browser);
  if (status_code == WD_SUCCESS) {
    if (!browser->is_closing()) {
      if (this->page_load_timeout_ >= 0 && this->wait_timeout_ < clock()) {
        LOG(DEBUG) << "Page load timeout reached. Ending wait cycle.";
        Response timeout_response;
        timeout_response.SetErrorResponse(ERROR_WEBDRIVER_TIMEOUT,
                                          "Timed out waiting for page to load.");
        browser->set_wait_required(false);
        this->serialized_response_ = timeout_response.Serialize();
        this->is_waiting_ = false;
        return 0;
      } else {
        LOG(DEBUG) << "Beginning wait.";
        this->is_waiting_ = !(browser->Wait(this->page_load_strategy_));
        if (is_single_wait) {
          LOG(DEBUG) << "Single requested wait with no deferred "
                     << "response complete. Ending wait cycle.";
          this->is_waiting_ = false;
          return 0;
        } else {
          if (this->is_waiting_) {
            LOG(DEBUG) << "Wait not complete. Scheduling another wait cycle.";
            this->CreateWaitThread(deferred_response);
            return 0;
          }
        }
      }
    }
  }
  LOG(DEBUG) << "Wait complete. Setting serialized response to deferred value "
             << deferred_response;
  this->serialized_response_ = deferred_response;
  this->is_waiting_ = false;
  return 0;
}

LRESULT IECommandExecutor::OnBeforeNewWindow(UINT uMsg,
                                             WPARAM wParam,
                                             LPARAM lParam,
                                             BOOL& bHandled) {
  LOG(TRACE) << "Entering IECommandExecutor::OnBeforeNewWindow";
  LOG(DEBUG) << "Setting await new window flag";
  this->is_awaiting_new_window_ = true;
  return 0;
}

LRESULT IECommandExecutor::OnAfterNewWindow(UINT uMsg,
                                            WPARAM wParam,
                                            LPARAM lParam,
                                            BOOL& bHandled) {
  LOG(TRACE) << "Entering IECommandExecutor::OnAfterNewWindow";
  if (wParam > 0) {
    LOG(DEBUG) << "Creating thread and reposting message.";
    this->CreateDelayPostMessageThread(static_cast<DWORD>(wParam),
                                       this->m_hWnd,
                                       WD_AFTER_NEW_WINDOW);
  } else {
    LOG(DEBUG) << "Clearing await new window flag";
    this->is_awaiting_new_window_ = false;
  }
  return 0;
}

LRESULT IECommandExecutor::OnBrowserNewWindow(UINT uMsg,
                                              WPARAM wParam,
                                              LPARAM lParam,
                                              BOOL& bHandled) {
  LOG(TRACE) << "Entering IECommandExecutor::OnBrowserNewWindow";
  NewWindowInfo* info = reinterpret_cast<NewWindowInfo*>(lParam);
  std::string target_url = info->target_url;
  std::string new_browser_id = this->OpenNewBrowsingContext(WINDOW_WINDOW_TYPE,
                                                            target_url);
  BrowserHandle new_window_wrapper;
  this->GetManagedBrowser(new_browser_id, &new_window_wrapper);
  if (new_window_wrapper->IsCrossZoneUrl(target_url)) {
    new_window_wrapper->InitiateBrowserReattach();
  }

  LOG(DEBUG) << "Attempting to marshal interface pointer to requesting thread.";
  IWebBrowser2* browser = new_window_wrapper->browser();
  HRESULT hr = ::CoMarshalInterThreadInterfaceInStream(IID_IWebBrowser2,
                                                       browser,
                                                       &(info->browser_stream));
  if (FAILED(hr)) {
    LOGHR(WARN, hr) << "Marshalling of interface pointer b/w threads is failed.";
  }

  return 0;
}

LRESULT IECommandExecutor::OnBrowserCloseWait(UINT uMsg,
                                              WPARAM wParam,
                                              LPARAM lParam,
                                              BOOL& bHandled) {
  LOG(TRACE) << "Entering IECommandExecutor::OnBrowserCloseWait";

  LPCSTR str = reinterpret_cast<LPCSTR>(lParam);
  std::string browser_id(str);
  delete[] str;
  BrowserMap::iterator found_iterator = this->managed_browsers_.find(browser_id);
  if (found_iterator != this->managed_browsers_.end()) {
    HWND alert_handle;
    bool is_alert_active = this->IsAlertActive(found_iterator->second,
                                               &alert_handle);
    if (is_alert_active) {
      // If there's an alert window active, the browser's Quit event does
      // not fire until any alerts are handled. Note that OnBeforeUnload
      // alerts must be handled here; the driver contains the ability to
      // handle other standard alerts on the next received command. We rely
      // on the browser's Quit command to remove the driver from the list of
      // managed browsers.
      Alert dialog(found_iterator->second, alert_handle);
      if (!dialog.is_standard_alert()) {
        dialog.Accept();
        is_alert_active = false;
      }
    }
    if (!is_alert_active) {
      ::Sleep(100);
      // If no alert is present, repost the message to the message pump, so
      // that we can wait until the browser is fully closed to return the
      // proper still-open list of window handles.
      LPSTR message_payload = new CHAR[browser_id.size() + 1];
      strcpy_s(message_payload, browser_id.size() + 1, browser_id.c_str());
      ::PostMessage(this->m_hWnd,
                    WD_BROWSER_CLOSE_WAIT,
                    NULL,
                    reinterpret_cast<LPARAM>(message_payload));
      return 0;
    }
  } else {
    LOG(WARN) << "Unable to find browser to quit with ID " << browser_id;
  }
  Json::Value handles(Json::arrayValue);
  std::vector<std::string> handle_list;
  this->GetManagedBrowserHandles(&handle_list);
  std::vector<std::string>::const_iterator handle_iterator = handle_list.begin();
  for (; handle_iterator != handle_list.end(); ++handle_iterator) {
    handles.append(*handle_iterator);
  }

  Response close_window_response;
  close_window_response.SetSuccessResponse(handles);
  this->serialized_response_ = close_window_response.Serialize();
  this->is_waiting_ = false;
  return 0;
}

LRESULT IECommandExecutor::OnBrowserQuit(UINT uMsg,
                                         WPARAM wParam,
                                         LPARAM lParam,
                                         BOOL& bHandled) {
  LOG(TRACE) << "Entering IECommandExecutor::OnBrowserQuit";

  LPCSTR str = reinterpret_cast<LPCSTR>(lParam);
  std::string browser_id(str);
  delete[] str;
  BrowserMap::iterator found_iterator = this->managed_browsers_.find(browser_id);
  if (found_iterator != this->managed_browsers_.end()) {
    this->managed_browsers_.erase(browser_id);
    if (this->managed_browsers_.size() == 0) {
      this->current_browser_id_ = "";
    }
  } else {
    LOG(WARN) << "Unable to find browser to quit with ID " << browser_id;
  }
  return 0;
}

LRESULT IECommandExecutor::OnBeforeBrowserReattach(UINT uMsg,
                                                   WPARAM wParam,
                                                   LPARAM lParam,
                                                   BOOL& bHandled) {
  LOG(TRACE) << "Entering IECommandExecutor::OnBeforeBrowserReattach";
  if (this->factory_->ignore_protected_mode_settings()) {
    this->reattach_wait_timeout_ = clock() + (static_cast<int>(this->reattach_browser_timeout_) / 1000 * CLOCKS_PER_SEC);
  }
  return 0;
}

LRESULT IECommandExecutor::OnBrowserReattach(UINT uMsg,
                                             WPARAM wParam,
                                             LPARAM lParam,
                                             BOOL& bHandled) {
  LOG(TRACE) << "Entering IECommandExecutor::OnBrowserReattach";
  BrowserReattachInfo* info = reinterpret_cast<BrowserReattachInfo*>(lParam);
  DWORD current_process_id = info->current_process_id;
  std::string browser_id = info->browser_id;
  std::vector<DWORD> known_process_ids = info->known_process_ids;
  delete info;

  if (!this->factory_->ignore_protected_mode_settings()) {
    return 0;
  }

  if (this->reattach_wait_timeout_ < clock()) {
    LOG(WARN) << "Reattach attempt has timed out";
    return 0;
  }

  LOG(DEBUG) << "Starting browser reattach process";

  std::vector<DWORD> new_process_ids;
  this->GetNewBrowserProcessIds(&known_process_ids, &new_process_ids);
  if (new_process_ids.size() == 0) {
    LOG(DEBUG) << "No new process found, rescheduling reattach";
    // If no new process IDs were found yet, repost the message
    this->PostBrowserReattachMessage(current_process_id,
                                     browser_id,
                                     known_process_ids);
  }
  if (new_process_ids.size() > 1) {
    LOG(WARN) << "Found more than one new iexplore.exe process. It is "
              << "impossible to know which is the proper one. Choosing one "
              << "at random.";
  }

  DWORD new_process_id = new_process_ids[0];
  if (!this->factory_->IsBrowserProcessInitialized(new_process_id)) {
    // If the browser for the new process ID is not yet ready,
    // repost the message
    LOG(DEBUG) << "Browser process " << new_process_id
               << " not initialized, rescheduling reattach";
    this->PostBrowserReattachMessage(current_process_id,
                                     browser_id,
                                     known_process_ids);
    return 0;
  }

  std::string error_message = "";
  ProcessWindowInfo process_window_info;
  process_window_info.dwProcessId = new_process_id;
  process_window_info.hwndBrowser = NULL;
  process_window_info.pBrowser = NULL;
  bool attached = this->factory_->AttachToBrowser(&process_window_info, &error_message);

  BrowserMap::iterator found_iterator = this->managed_browsers_.find(browser_id);
  if (found_iterator != this->managed_browsers_.end()) {
    this->proxy_manager_->SetProxySettings(process_window_info.hwndBrowser);
    found_iterator->second->cookie_manager()->Initialize(process_window_info.hwndBrowser);
    found_iterator->second->ReattachBrowser(process_window_info.pBrowser);
  } else {
    LOG(WARN) << "The managed browser was not found to reattach to.";
  }
  return 0;
}

LRESULT IECommandExecutor::OnIsSessionValid(UINT uMsg,
                                            WPARAM wParam,
                                            LPARAM lParam,
                                            BOOL& bHandled) {
  LOG(TRACE) << "Entering IECommandExecutor::OnIsSessionValid";

  return this->is_valid_ ? 1 : 0;
}

LRESULT IECommandExecutor::OnNewHtmlDialog(UINT uMsg,
                                           WPARAM wParam,
                                           LPARAM lParam,
                                           BOOL& bHandles) {
  LOG(TRACE) << "Entering IECommandExecutor::OnNewHtmlDialog";

  HWND dialog_handle = reinterpret_cast<HWND>(lParam);
  BrowserMap::const_iterator it = this->managed_browsers_.begin();
  for (; it != this->managed_browsers_.end(); ++it) {
    if (dialog_handle == it->second->window_handle()) {
      LOG(DEBUG) << "Dialog is equal to one managed browser";
      return 0;
    }
  }

  int retry_count = 0;
  CComPtr<IHTMLDocument2> document;
  bool found_document = this->factory_->GetDocumentFromWindowHandle(dialog_handle, &document);
  while (found_document && retry_count < MAX_HTML_DIALOG_RETRIES) {
    CComPtr<IHTMLWindow2> window;
    HRESULT hr = document->get_parentWindow(&window);
    if (FAILED(hr)) {
      // Getting the parent window of the dialog's document failed. This
      // usually means that the document changed out from under us before we
      // could get the window reference. The canonical case for this is a
      // redirect using JavaScript. Sleep for a short time, then retry to
      // obtain the reference.
      LOGHR(DEBUG, hr) << "IHTMLDocument2::get_parentWindow failed. Retrying.";
      ::Sleep(100);
      document.Release();
      found_document = this->factory_->GetDocumentFromWindowHandle(dialog_handle, &document);
      ++retry_count;
    } else {
      this->AddManagedBrowser(BrowserHandle(new HtmlDialog(window,
                                                           dialog_handle,
                                                           this->m_hWnd)));
      return 0;
    }
  }
  if (found_document) {
    LOG(WARN) << "Got document from dialog, but could not get window";
  } else {
    LOG(WARN) << "Unable to get document from dialog";
  }
  return 0;
}

LRESULT IECommandExecutor::OnQuit(UINT uMsg,
                                  WPARAM wParam,
                                  LPARAM lParam,
                                  BOOL& bHandled) {
  return 0;
}

LRESULT IECommandExecutor::OnGetQuitStatus(UINT uMsg,
                                           WPARAM wParam,
                                           LPARAM lParam,
                                           BOOL& bHandled) {
  return this->is_quitting_ && this->managed_browsers_.size() > 0 ? 1 : 0;
}

LRESULT IECommandExecutor::OnScriptWait(UINT uMsg,
                                        WPARAM wParam,
                                        LPARAM lParam,
                                        BOOL& bHandled) {
  LOG(TRACE) << "Entering IECommandExecutor::OnScriptWait";

  BrowserHandle browser;
  int status_code = this->GetCurrentBrowser(&browser);
  if (status_code == WD_SUCCESS && !browser->is_closing()) {
    if (this->async_script_timeout_ >= 0 && this->wait_timeout_ < clock()) {
      ::SendMessage(browser->script_executor_handle(),
                    WD_ASYNC_SCRIPT_DETACH_LISTENTER,
                    NULL,
                    NULL);
      Response timeout_response;
      timeout_response.SetErrorResponse(ERROR_SCRIPT_TIMEOUT,
                                        "Timed out waiting for script to complete.");
      this->serialized_response_ = timeout_response.Serialize();
      this->is_waiting_ = false;
      browser->set_script_executor_handle(NULL);
    } else {
      HWND alert_handle;
      bool is_execution_finished = ::SendMessage(browser->script_executor_handle(),
                                                 WD_ASYNC_SCRIPT_IS_EXECUTION_COMPLETE,
                                                 NULL,
                                                 NULL) != 0;
      bool is_alert_active = this->IsAlertActive(browser, &alert_handle);
      this->is_waiting_ = !is_execution_finished && !is_alert_active;
      if (this->is_waiting_) {
        // If we are still waiting, we need to wait a bit then post a message to
        // ourselves to run the wait again. However, we can't wait using Sleep()
        // on this thread. This call happens in a message loop, and we would be
        // unable to process the COM events in the browser if we put this thread
        // to sleep.
        unsigned int thread_id = 0;
        HANDLE thread_handle = reinterpret_cast<HANDLE>(_beginthreadex(NULL,
                                                        0,
                                                        &IECommandExecutor::ScriptWaitThreadProc,
                                                        (void *)this->m_hWnd,
                                                        0,
                                                        &thread_id));
        if (thread_handle != NULL) {
          ::CloseHandle(thread_handle);
        } else {
          LOGERR(DEBUG) << "Unable to create waiter thread";
        }
      } else {
        Response response;
        Json::Value script_result;
        ::SendMessage(browser->script_executor_handle(),
                      WD_ASYNC_SCRIPT_DETACH_LISTENTER,
                      NULL,
                      NULL);
        int status_code = static_cast<int>(::SendMessage(browser->script_executor_handle(),
                                                         WD_ASYNC_SCRIPT_GET_RESULT,
                                                         NULL,
                                                         reinterpret_cast<LPARAM>(&script_result)));
        if (status_code != WD_SUCCESS) {
          std::string error_message = "Error executing JavaScript";
          if (script_result.isString()) {
            error_message = script_result.asString();
          }
          response.SetErrorResponse(status_code, error_message);
        } else {
          response.SetSuccessResponse(script_result);
        }
        ::SendMessage(browser->script_executor_handle(), WM_CLOSE, NULL, NULL);
        browser->set_script_executor_handle(NULL);
        this->serialized_response_ = response.Serialize();
      }
    }
  } else {
    this->is_waiting_ = false;
  }
  return 0;
}

LRESULT IECommandExecutor::OnRefreshManagedElements(UINT uMsg,
                                                    WPARAM wParam,
                                                    LPARAM lParam,
                                                    BOOL& bHandled) {
  this->managed_elements_->ClearCache();
  return 0;
}

LRESULT IECommandExecutor::OnHandleUnexpectedAlerts(UINT uMsg,
                                                    WPARAM wParam,
                                                    LPARAM lParam,
                                                    BOOL& bHandled) {
  LOG(TRACE) << "Entering IECommandExecutor::OnHandleUnexpectedAlerts";
  BrowserMap::const_iterator it = this->managed_browsers_.begin();
  for (; it != this->managed_browsers_.end(); ++it) {
    HWND alert_handle = it->second->GetActiveDialogWindowHandle();
    if (alert_handle != NULL) {
      std::string alert_text;
      this->HandleUnexpectedAlert(it->second, alert_handle, true, &alert_text);
    }
  }
  return 0;
}

LRESULT IECommandExecutor::OnTransferManagedElement(UINT uMsg,
                                                    WPARAM wParam,
                                                    LPARAM lParam,
                                                    BOOL& bHandled) {
  LOG(TRACE) << "Entering IECommandExecutor::OnTransferManagedElement";
  ElementInfo* info = reinterpret_cast<ElementInfo*>(lParam);
  std::string element_id = info->element_id;
  LPSTREAM element_stream = info->element_stream;
  BrowserHandle browser_handle;
  this->GetCurrentBrowser(&browser_handle);
  CComPtr<IHTMLElement> element;
  ::CoGetInterfaceAndReleaseStream(element_stream,
                                   IID_IHTMLElement,
                                   reinterpret_cast<void**>(&element));
  delete info;
  ElementHandle element_handle;
  this->managed_elements_->AddManagedElement(browser_handle,
                                             element,
                                             &element_handle);
  RemappedElementInfo* return_info = new RemappedElementInfo;
  return_info->original_element_id = element_id;
  return_info->element_id = element_handle->element_id();
  ::PostMessage(browser_handle->script_executor_handle(),
                WD_ASYNC_SCRIPT_NOTIFY_ELEMENT_TRANSFERRED,
                NULL,
                reinterpret_cast<LPARAM>(return_info));
  return WD_SUCCESS;
}

LRESULT IECommandExecutor::OnScheduleRemoveManagedElement(UINT uMsg,
                                                          WPARAM wParam,
                                                          LPARAM lParam,
                                                          BOOL& bHandled) {
  LOG(TRACE) << "Entering IECommandExecutor::OnScheduleRemoveManagedElement";
  ElementInfo* info = reinterpret_cast<ElementInfo*>(lParam);
  std::string element_id = info->element_id;
  delete info;
  this->RemoveManagedElement(element_id);
  return WD_SUCCESS;
}

unsigned int WINAPI IECommandExecutor::WaitThreadProc(LPVOID lpParameter) {
  LOG(TRACE) << "Entering IECommandExecutor::WaitThreadProc";
  WaitThreadContext* thread_context = reinterpret_cast<WaitThreadContext*>(lpParameter);
  HWND window_handle = thread_context->window_handle;
  bool is_deferred_command = thread_context->is_deferred_command;
  std::string deferred_response(thread_context->deferred_response);
  delete thread_context->deferred_response;
  delete thread_context;

  LPSTR message_payload = new CHAR[deferred_response.size() + 1];
  strcpy_s(message_payload,
           deferred_response.size() + 1,
           deferred_response.c_str());

  ::Sleep(WAIT_TIME_IN_MILLISECONDS);
  ::PostMessage(window_handle,
                WD_WAIT,
                static_cast<WPARAM>(deferred_response.size()),
                reinterpret_cast<LPARAM>(message_payload));
  if (is_deferred_command) {
    // This wait is requested by the automatic handling of a user prompt
    // before a command was executed, so re-queue the command execution
    // for after the wait.
    ::PostMessage(window_handle, WD_EXEC_COMMAND, NULL, NULL);
  }
  return 0;
}

unsigned int WINAPI IECommandExecutor::ScriptWaitThreadProc(LPVOID lpParameter) {
  LOG(TRACE) << "Entering IECommandExecutor::ScriptWaitThreadProc";
  HWND window_handle = reinterpret_cast<HWND>(lpParameter);
  ::Sleep(SCRIPT_WAIT_TIME_IN_MILLISECONDS);
  ::PostMessage(window_handle, WD_SCRIPT_WAIT, NULL, NULL);
  return 0;
}

unsigned int WINAPI IECommandExecutor::DelayPostMessageThreadProc(LPVOID lpParameter) {
  LOG(TRACE) << "Entering IECommandExecutor::DelayPostMessageThreadProc";
  DelayPostMessageThreadContext* context = reinterpret_cast<DelayPostMessageThreadContext*>(lpParameter);
  HWND window_handle = context->window_handle;
  DWORD sleep_time = context->delay;
  UINT message_to_post = context->msg;
  delete context;

  ::Sleep(sleep_time);
  ::PostMessage(window_handle, message_to_post, NULL, NULL);
  return 0;
}

unsigned int WINAPI IECommandExecutor::ThreadProc(LPVOID lpParameter) {
  LOG(TRACE) << "Entering IECommandExecutor::ThreadProc";

  IECommandExecutorThreadContext* thread_context = reinterpret_cast<IECommandExecutorThreadContext*>(lpParameter);
  HWND window_handle = thread_context->hwnd;

  // it is better to use IECommandExecutorSessionContext instead
  // but use ThreadContext for code minimization
  IECommandExecutorThreadContext* session_context = new IECommandExecutorThreadContext();
  session_context->port = thread_context->port;

  DWORD error = 0;
  HRESULT hr = ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  if (FAILED(hr)) {
    LOGHR(DEBUG, hr) << "COM library initialization encountered an error";
  }

  IECommandExecutor new_session;
  HWND session_window_handle = new_session.Create(/*HWND*/ HWND_MESSAGE,
                                                  /*_U_RECT rect*/ CWindow::rcDefault,
                                                  /*LPCTSTR szWindowName*/ NULL,
                                                  /*DWORD dwStyle*/ NULL,
                                                  /*DWORD dwExStyle*/ NULL,
                                                  /*_U_MENUorID MenuOrID*/ 0U,
                                                  /*LPVOID lpCreateParam*/ reinterpret_cast<LPVOID*>(session_context));
  if (session_window_handle == NULL) {
    LOGERR(WARN) << "Unable to create new IECommandExecutor session";
  }

  MSG msg;
  ::PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);

  // Return the HWND back through lpParameter, and signal that the
  // window is ready for messages.
  thread_context->hwnd = session_window_handle;
  HANDLE event_handle = ::OpenEvent(EVENT_ALL_ACCESS, FALSE, WEBDRIVER_START_EVENT_NAME);
  if (event_handle != NULL) {
    ::SetEvent(event_handle);
    ::CloseHandle(event_handle);
  } else {
    LOGERR(DEBUG) << "Unable to signal that window is ready";
  }

  // Run the message loop
  BOOL get_message_return_value;
  while ((get_message_return_value = ::GetMessage(&msg, NULL, 0, 0)) != 0) {
    if (get_message_return_value == -1) {
      LOGERR(WARN) << "Windows GetMessage() API returned error";
      break;
    } else {
      if (msg.message == WD_SHUTDOWN) {
        LOG(DEBUG) << "Shutdown message received";
        new_session.DestroyWindow();
        LOG(DEBUG) << "Returned from DestroyWindow()";
        break;
      } else {
        // We need to lock this mutex here to make sure only one thread is processing
        // win32 messages at a time.
        static std::mutex messageLock;
        std::lock_guard<std::mutex> lock(messageLock);
        ::TranslateMessage(&msg);
        ::DispatchMessage(&msg);
      }
    }
  }

  LOG(DEBUG) << "Exited IECommandExecutor thread message loop";
  ::CoUninitialize();
  delete session_context;
  return 0;
}

void IECommandExecutor::DispatchCommand() {
  LOG(TRACE) << "Entering IECommandExecutor::DispatchCommand";

  Response response;

  if (!this->command_handlers_->IsValidCommand(this->current_command_.command_type())) {
    LOG(WARN) << "Unable to find command handler for " << this->current_command_.command_type();
    response.SetErrorResponse(ERROR_UNKNOWN_COMMAND, "Command not implemented");
  } else if (!this->current_command_.is_valid_parameters()) {
    response.SetErrorResponse(ERROR_INVALID_ARGUMENT, "parameters property of command is not a valid JSON object");
  } else {
    BrowserHandle browser;
    int status_code = WD_SUCCESS;
    std::string command_type = this->current_command_.command_type();
    if (command_type != webdriver::CommandType::NewSession) {
      // There should never be a modal dialog or alert to check for if the command
      // is the "newSession" command.
      status_code = this->GetCurrentBrowser(&browser);
      if (status_code == WD_SUCCESS) {
        LOG(DEBUG) << "Checking for alert before executing " << command_type << " command";
        HWND alert_handle = NULL;
        bool alert_is_active = this->IsAlertActive(browser, &alert_handle);
        if (alert_is_active) {
          if (this->IsCommandValidWithAlertPresent()) {
            LOG(DEBUG) << "Alert is detected, and the sent command is valid";
          } else {
            LOG(DEBUG) << "Unexpected alert is detected, and the sent command "
                       << "is invalid when an alert is present";
            bool is_quit_command = command_type == webdriver::CommandType::Quit;

            std::string alert_text;
            bool is_notify_unexpected_alert = this->HandleUnexpectedAlert(browser,
                                                                          alert_handle,
                                                                          is_quit_command,
                                                                          &alert_text);
            if (!is_quit_command) {
              if (is_notify_unexpected_alert) {
                // To keep pace with what the specification suggests, we'll
                // return the text of the alert in the error response.
                response.SetErrorResponse(EUNEXPECTEDALERTOPEN,
                                          "Modal dialog present with text: " + alert_text);
                response.AddAdditionalData("text", alert_text);
                this->serialized_response_ = response.Serialize();
                return;
              } else {
                LOG(DEBUG) << "Command other than quit was issued, and option "
                           << "to not notify was specified. Continuing with "
                           << "command after automatically closing alert.";
                // Push a wait cycle, then re-execute the current command (which
                // hasn't actually been executed yet). Note that an empty string
                // for the deferred response parameter of CreateWaitThread will
                // re-queue the execution of the command.
                this->CreateWaitThread("", true);
                return;
              }
            } else {
                LOG(DEBUG) << "Quit command was issued. Continuing with "
                           << "command after automatically closing alert.";
            }
          }
        }
      } else {
        LOG(WARN) << "Unable to find current browser";
      }
    }

    LOG(DEBUG) << "Executing command: " << command_type;
    CommandHandlerHandle command_handler = this->command_handlers_->GetCommandHandler(command_type);
    command_handler->Execute(*this, this->current_command_, &response);
    LOG(DEBUG) << "Command execution for " << command_type << " complete";

    status_code = this->GetCurrentBrowser(&browser);
    if (status_code == WD_SUCCESS) {
      if (browser->is_closing() && !this->is_quitting_) {
        // Case 1: The browser window is closing, but not via the Quit command,
        // so the executor must wait for the browser window to be closed and
        // removed from the list of managed browser windows.
        LOG(DEBUG) << "Browser is closing; awaiting close.";
        LPSTR message_payload = new CHAR[browser->browser_id().size() + 1];
        strcpy_s(message_payload,
                 browser->browser_id().size() + 1,
                 browser->browser_id().c_str());

        this->is_waiting_ = true;
        ::Sleep(WAIT_TIME_IN_MILLISECONDS);
        ::PostMessage(this->m_hWnd,
                      WD_BROWSER_CLOSE_WAIT,
                      NULL,
                      reinterpret_cast<LPARAM>(message_payload));
        return;
      } else if (browser->script_executor_handle() != NULL) {
        // Case 2: There is a pending asynchronous JavaScript execution in
        // progress, so the executor must wait for the script to complete
        // or a timeout.
        this->is_waiting_ = true;
        if (this->async_script_timeout_ >= 0) {
          this->wait_timeout_ = clock() + (static_cast<int>(this->async_script_timeout_) / 1000 * CLOCKS_PER_SEC);
        }
        LOG(DEBUG) << "Awaiting completion of in-progress asynchronous JavaScript execution.";
        ::PostMessage(this->m_hWnd, WD_SCRIPT_WAIT, NULL, NULL);
        return;
      } else if (browser->wait_required()) {
        // Case 3: The command handler has explicitly asked to wait for page
        // load, so the executor must wait for page load or timeout.
        this->is_waiting_ = true;
        if (this->page_load_timeout_ >= 0) {
          this->wait_timeout_ = clock() + (static_cast<int>(this->page_load_timeout_) / 1000 * CLOCKS_PER_SEC);
        }
        std::string deferred_response = response.Serialize();
        LOG(DEBUG) << "Command handler requested wait. This will cause a minimal wait of at least 50 milliseconds.";
        this->CreateWaitThread(deferred_response);
        return;
      }
    } else {
      if (this->current_command_.command_type() != webdriver::CommandType::Quit) {
        LOG(WARN) << "Unable to get current browser";
      }
    }
  }

  this->serialized_response_ = response.Serialize();
  LOG(DEBUG) << "Setting serialized response to " << this->serialized_response_;
  LOG(DEBUG) << "Is waiting flag: " << this->is_waiting_ ? "true" : "false";
}

bool IECommandExecutor::IsCommandValidWithAlertPresent() {
  std::string command_type = this->current_command_.command_type();
  if (command_type == webdriver::CommandType::GetAlertText ||
      command_type == webdriver::CommandType::SendKeysToAlert ||
      command_type == webdriver::CommandType::AcceptAlert ||
      command_type == webdriver::CommandType::DismissAlert ||
      command_type == webdriver::CommandType::SetAlertCredentials ||
      command_type == webdriver::CommandType::GetTimeouts ||
      command_type == webdriver::CommandType::SetTimeouts ||
      command_type == webdriver::CommandType::Screenshot ||
      command_type == webdriver::CommandType::ElementScreenshot ||
      command_type == webdriver::CommandType::GetCurrentWindowHandle ||
      command_type == webdriver::CommandType::GetWindowHandles ||
      command_type == webdriver::CommandType::SwitchToWindow) {
    return true;
  }
  return false;
}

void IECommandExecutor::CreateWaitThread(const std::string& deferred_response) {
  this->CreateWaitThread(deferred_response, false);
}

void IECommandExecutor::CreateWaitThread(const std::string& deferred_response,
                                         const bool is_deferred_command_execution) {
  // If we are still waiting, we need to wait a bit then post a message to
  // ourselves to run the wait again. However, we can't wait using Sleep()
  // on this thread. This call happens in a message loop, and we would be
  // unable to process the COM events in the browser if we put this thread
  // to sleep.
  LOG(DEBUG) << "Creating wait thread with deferred response of `" << deferred_response << "`";
  if (is_deferred_command_execution) {
    LOG(DEBUG) << "Command execution will be rescheduled.";
  }
  WaitThreadContext* thread_context = new WaitThreadContext;
  thread_context->window_handle = this->m_hWnd;
  thread_context->is_deferred_command = is_deferred_command_execution;
  thread_context->deferred_response = new CHAR[deferred_response.size() + 1];
  strcpy_s(thread_context->deferred_response,
           deferred_response.size() + 1,
           deferred_response.c_str());

  unsigned int thread_id = 0;
  HANDLE thread_handle = reinterpret_cast<HANDLE>(_beginthreadex(NULL,
                                                                 0,
                                                                 &IECommandExecutor::WaitThreadProc,
                                                                 reinterpret_cast<void*>(thread_context),
                                                                 0,
                                                                 &thread_id));
  if (thread_handle != NULL) {
    ::CloseHandle(thread_handle);
  }
  else {
    LOGERR(DEBUG) << "Unable to create waiter thread";
  }
}

void IECommandExecutor::CreateDelayPostMessageThread(const DWORD delay_time,
                                                     const HWND window_handle,
                                                     const UINT message_to_post) {
  DelayPostMessageThreadContext* context = new DelayPostMessageThreadContext;
  context->delay = delay_time;
  context->window_handle = window_handle;
  context->msg = message_to_post;
  unsigned int thread_id = 0;
  HANDLE thread_handle = reinterpret_cast<HANDLE>(_beginthreadex(NULL,
                                                                 0,
                                                                 &IECommandExecutor::DelayPostMessageThreadProc,
                                                                 reinterpret_cast<void*>(context),
                                                                 0,
                                                                 &thread_id));
  if (thread_handle != NULL) {
    ::CloseHandle(thread_handle);
  } else {
    LOGERR(DEBUG) << "Unable to create waiter thread";
  }
}

bool IECommandExecutor::IsAlertActive(BrowserHandle browser, HWND* alert_handle) {
  LOG(TRACE) << "Entering IECommandExecutor::IsAlertActive";
  HWND dialog_handle = browser->GetActiveDialogWindowHandle();
  if (dialog_handle != NULL) {
    // Found a window handle, make sure it's an actual alert,
    // and not a showModalDialog() window.
    std::vector<char> window_class_name(34);
    ::GetClassNameA(dialog_handle, &window_class_name[0], 34);
    if (strcmp(ALERT_WINDOW_CLASS, &window_class_name[0]) == 0) {
      *alert_handle = dialog_handle;
      return true;
    } else if (strcmp(SECURITY_DIALOG_WINDOW_CLASS, &window_class_name[0]) == 0) {
      *alert_handle = dialog_handle;
      return true;
    } else {
      LOG(WARN) << "Found alert handle does not have a window class consistent with an alert";
    }
  } else {
    LOG(DEBUG) << "No alert handle is found";
  }
  return false;
}

bool IECommandExecutor::HandleUnexpectedAlert(BrowserHandle browser,
                                              HWND alert_handle,
                                              bool force_use_dismiss,
                                              std::string* alert_text) {
  LOG(TRACE) << "Entering IECommandExecutor::HandleUnexpectedAlert";
  clock_t end = clock() + 5 * CLOCKS_PER_SEC;
  bool is_visible = (::IsWindowVisible(alert_handle) == TRUE);
  while (!is_visible && clock() < end) {
    ::Sleep(50);
    is_visible = (::IsWindowVisible(alert_handle) == TRUE);
  }
  Alert dialog(browser, alert_handle);
  *alert_text = dialog.GetText();
  if (!dialog.is_standard_alert()) {
    // The dialog was non-standard. The most common case of this is
    // an onBeforeUnload dialog, which must be accepted to continue.
    dialog.Accept();
    return false;
  }
  if (this->unexpected_alert_behavior_ == ACCEPT_UNEXPECTED_ALERTS ||
      this->unexpected_alert_behavior_ == ACCEPT_AND_NOTIFY_UNEXPECTED_ALERTS) {
    LOG(DEBUG) << "Automatically accepting the alert";
    dialog.Accept();
  } else if (this->unexpected_alert_behavior_.size() == 0 ||
             this->unexpected_alert_behavior_ == DISMISS_UNEXPECTED_ALERTS ||
             this->unexpected_alert_behavior_ == DISMISS_AND_NOTIFY_UNEXPECTED_ALERTS ||
             force_use_dismiss) {
    // If a quit command was issued, we should not ignore an unhandled
    // alert, even if the alert behavior is set to "ignore".
    LOG(DEBUG) << "Automatically dismissing the alert";
    if (dialog.is_standard_alert() || dialog.is_security_alert()) {
      dialog.Dismiss();
    } else {
      // The dialog was non-standard. The most common case of this is
      // an onBeforeUnload dialog, which must be accepted to continue.
      dialog.Accept();
    }
  }

  bool is_notify_unexpected_alert =
    this->unexpected_alert_behavior_.size() == 0 ||
    this->unexpected_alert_behavior_ == IGNORE_UNEXPECTED_ALERTS ||
    this->unexpected_alert_behavior_ == DISMISS_AND_NOTIFY_UNEXPECTED_ALERTS ||
    this->unexpected_alert_behavior_ == ACCEPT_AND_NOTIFY_UNEXPECTED_ALERTS;
  is_notify_unexpected_alert = is_notify_unexpected_alert && dialog.is_standard_alert();
  return is_notify_unexpected_alert;
}

void IECommandExecutor::PostBrowserReattachMessage(const DWORD current_process_id,
                                                   const std::string& browser_id,
                                                   const std::vector<DWORD>& known_process_ids) {
  LOG(TRACE) << "Entering IECommandExecutor::PostBrowserReattachMessage";
  ::Sleep(100);
  BrowserReattachInfo* repost_info = new BrowserReattachInfo;
  repost_info->current_process_id = current_process_id;
  repost_info->browser_id = browser_id;
  repost_info->known_process_ids = known_process_ids;
  ::PostMessage(this->m_hWnd,
                WD_BROWSER_REATTACH,
                NULL,
                reinterpret_cast<LPARAM>(repost_info));
}

void IECommandExecutor::GetNewBrowserProcessIds(std::vector<DWORD>* known_process_ids,
                                                std::vector<DWORD>* new_process_ids) {
  LOG(TRACE) << "Entering IECommandExecutor::GetNewBrowserProcessIds";
  std::vector<DWORD> all_ie_process_ids;
  WindowUtilities::GetProcessesByName(L"iexplore.exe", &all_ie_process_ids);

  // Maximum size of the new process list is if all IE processes are unknown.
  std::vector<DWORD> temp_new_process_ids(all_ie_process_ids.size());
  std::sort(known_process_ids->begin(), known_process_ids->end());
  std::sort(all_ie_process_ids.begin(), all_ie_process_ids.end());
  std::vector<DWORD>::iterator end_iterator = std::set_difference(all_ie_process_ids.begin(),
                                                                  all_ie_process_ids.end(),
                                                                  known_process_ids->begin(),
                                                                  known_process_ids->end(),
                                                                  temp_new_process_ids.begin());
  temp_new_process_ids.resize(end_iterator - temp_new_process_ids.begin());
  *new_process_ids = temp_new_process_ids;
}

int IECommandExecutor::GetCurrentBrowser(BrowserHandle* browser_wrapper) const {
  LOG(TRACE) << "Entering IECommandExecutor::GetCurrentBrowser";
  return this->GetManagedBrowser(this->current_browser_id_, browser_wrapper);
}

int IECommandExecutor::GetManagedBrowser(const std::string& browser_id,
                                         BrowserHandle* browser_wrapper) const {
  LOG(TRACE) << "Entering IECommandExecutor::GetManagedBrowser";

  if (!this->is_valid()) {
    LOG(TRACE) << "Current command executor is not valid";
    return ENOSUCHDRIVER;
  }

  if (browser_id == "") {
    LOG(WARN) << "Browser ID requested was an empty string";
    return ENOSUCHWINDOW;
  }

  BrowserMap::const_iterator found_iterator =
      this->managed_browsers_.find(browser_id);
  if (found_iterator == this->managed_browsers_.end()) {
    LOG(WARN) << "Unable to find managed browser with id " << browser_id;
    return ENOSUCHWINDOW;
  }

  *browser_wrapper = found_iterator->second;
  return WD_SUCCESS;
}

void IECommandExecutor::GetManagedBrowserHandles(std::vector<std::string>* managed_browser_handles) const {
  LOG(TRACE) << "Entering IECommandExecutor::GetManagedBrowserHandles";

  BrowserMap::const_iterator it = this->managed_browsers_.begin();
  for (; it != this->managed_browsers_.end(); ++it) {
    if (it->second->IsValidWindow()) {
      managed_browser_handles->push_back(it->first);
    }

    // Look for browser windows created by showModalDialog().
    it->second->GetActiveDialogWindowHandle();
  }
}

void IECommandExecutor::AddManagedBrowser(BrowserHandle browser_wrapper) {
  LOG(TRACE) << "Entering IECommandExecutor::AddManagedBrowser";

  this->managed_browsers_[browser_wrapper->browser_id()] = browser_wrapper;
  if (this->current_browser_id_ == "") {
    LOG(TRACE) << "Setting current browser id to " << browser_wrapper->browser_id();
    this->current_browser_id_ = browser_wrapper->browser_id();
  }
}

std::string IECommandExecutor::OpenNewBrowsingContext(const std::string& window_type) {
  return this->OpenNewBrowsingContext(window_type, "about:blank");
}

std::string IECommandExecutor::OpenNewBrowsingContext(const std::string& window_type,
                                                      const std::string& url) {
  LOG(TRACE) << "Entering IECommandExecutor::OpenNewBrowsingContext";
  std::wstring target_url = StringUtilities::ToWString(url);
  std::string new_browser_id = "";
  if (window_type == TAB_WINDOW_TYPE) {
    new_browser_id = this->OpenNewBrowserTab(target_url);
  } else {
    new_browser_id = this->OpenNewBrowserWindow(target_url);
  }

  BrowserHandle new_window_wrapper;
  this->GetManagedBrowser(new_browser_id, &new_window_wrapper);
  HWND new_window_handle = new_window_wrapper->GetBrowserWindowHandle();
  this->proxy_manager_->SetProxySettings(new_window_handle);
  new_window_wrapper->cookie_manager()->Initialize(new_window_handle);

  return new_browser_id;
}

std::string IECommandExecutor::OpenNewBrowserWindow(const std::wstring& url) {
  LOG(TRACE) << "Entering IECommandExecutor::OpenNewBrowserWindow";
  bool is_protected_mode_url = ::IEIsProtectedModeURL(url.c_str()) == S_OK;
  if (url.find(L"about:blank") == 0) {
    // Special-case URLs starting with about:blank, so that the new window
    // is in the same Protected Mode zone as the current window from which
    // it's being opened.
    BrowserHandle current_browser;
    this->GetCurrentBrowser(&current_browser);
    is_protected_mode_url = current_browser->IsProtectedMode();
  }
  CComPtr<IWebBrowser2> browser = this->factory_->CreateBrowser(is_protected_mode_url);
  if (browser == NULL) {
    // No browser was created, so we have to bail early.
    // Check the log for the HRESULT why.
    return "";
  }
  LOG(DEBUG) << "New browser window was opened.";
  BrowserHandle new_window_wrapper(new Browser(browser, NULL, this->m_hWnd));
  // It is acceptable to set the proxy settings here, as the newly-created
  // browser window has not yet been navigated to any page. Only after the
  // interface has been marshaled back across the thread boundary to the
  // NewWindow3 event handler will the navigation begin, which ensures that
  // even the initial navigation will get captured by the proxy, if one is
  // set. Likewise, the cookie manager needs to have its window handle
  // properly set to a non-NULL value so that windows messages are routed
  // to the correct window.
  // N.B. DocumentHost::GetBrowserWindowHandle returns the tab window handle
  // for IE 7 and above, and the top-level window for IE6. This is the window
  // required for setting the proxy settings.
  this->AddManagedBrowser(new_window_wrapper);
  return new_window_wrapper->browser_id();
}

std::string IECommandExecutor::OpenNewBrowserTab(const std::wstring& url) {
  LOG(TRACE) << "Entering IECommandExecutor::OpenNewBrowserTab";
  BrowserHandle browser_wrapper;
  this->GetCurrentBrowser(&browser_wrapper);
  HWND top_level_handle = browser_wrapper->GetTopLevelWindowHandle();

  std::vector<HWND> original_handles;
  ::EnumChildWindows(top_level_handle,
                     &IECommandExecutor::FindAllBrowserHandles,
                     reinterpret_cast<LPARAM>(&original_handles));
  std::sort(original_handles.begin(), original_handles.end());

  // IWebBrowser2::Navigate2 will open the specified URL in a new tab,
  // if requested. The Sleep() call after the navigate is necessary,
  // since the IECommandExecutor class doesn't have access to the events
  // to indicate the navigation is completed.
  CComVariant url_variant = url.c_str();
  CComVariant flags = navOpenInNewTab;
  browser_wrapper->browser()->Navigate2(&url_variant,
                                        &flags,
                                        NULL,
                                        NULL,
                                        NULL);
  ::Sleep(500);

  clock_t end_time = clock() + 5 * CLOCKS_PER_SEC;
  std::vector<HWND> new_handles;
  ::EnumChildWindows(top_level_handle,
                     &IECommandExecutor::FindAllBrowserHandles,
                     reinterpret_cast<LPARAM>(&new_handles));
  while (new_handles.size() <= original_handles.size() &&
         clock() < end_time) {
    ::Sleep(50);
    ::EnumChildWindows(top_level_handle,
                       &FindAllBrowserHandles,
                       reinterpret_cast<LPARAM>(&new_handles));
  }
  std::sort(new_handles.begin(), new_handles.end());

  if (new_handles.size() <= original_handles.size()) {
    LOG(WARN) << "No new window handle found after attempt to open";
    return "";
  }

  // We are guaranteed to have at least one HWND difference
  // between the two vectors if we reach this point, because
  // we know the vectors are different sizes.
  std::vector<HWND> diff(new_handles.size());
  std::vector<HWND>::iterator it = std::set_difference(new_handles.begin(),
                                                       new_handles.end(),
                                                       original_handles.begin(),
                                                       original_handles.end(),
                                                       diff.begin());
  diff.resize(it - diff.begin());
  if (diff.size() > 1) {
    std::string handle_list = "";
    std::vector<HWND>::const_iterator it = diff.begin();
    for (; it != diff.end(); ++it) {
      if (handle_list.size() > 0) {
        handle_list.append(", ");
      }
      handle_list.append(StringUtilities::Format("0x%08x", *it));
    }
    LOG(DEBUG) << "Found more than one new window handles! Found "
               << diff.size() << "windows [" << handle_list << "]";
  }
  HWND new_tab_window = diff[0];

  DWORD process_id;
  ::GetWindowThreadProcessId(new_tab_window, &process_id);
  clock_t end = clock() + (DEFAULT_BROWSER_REATTACH_TIMEOUT_IN_MILLISECONDS / 1000 * CLOCKS_PER_SEC);
  bool is_ready = this->factory_->IsBrowserProcessInitialized(process_id);
  while (!is_ready && clock() < end) {
    ::Sleep(100);
    is_ready = this->factory_->IsBrowserProcessInitialized(process_id);
  }

  ProcessWindowInfo info;
  info.dwProcessId = process_id;
  info.hwndBrowser = new_tab_window;
  info.pBrowser = NULL;
  std::string error_message = "";
  this->factory_->AttachToBrowser(&info, &error_message);
  BrowserHandle new_tab_wrapper(new Browser(info.pBrowser,
                                            NULL,
                                            this->m_hWnd));
  // Force a wait cycle to make sure the browser is finished initializing.
  new_tab_wrapper->Wait(NORMAL_PAGE_LOAD_STRATEGY);
  this->AddManagedBrowser(new_tab_wrapper);
  return new_tab_wrapper->browser_id();
}

BOOL CALLBACK IECommandExecutor::FindAllBrowserHandles(HWND hwnd, LPARAM arg) {
  std::vector<HWND>* handles = reinterpret_cast<std::vector<HWND>*>(arg);

  // Could this be an Internet Explorer Server window?
  // 25 == "Internet Explorer_Server\0"
  char name[25];
  if (::GetClassNameA(hwnd, name, 25) == 0) {
    // No match found. Skip
    return TRUE;
  }

  if (strcmp("Internet Explorer_Server", name) == 0) {
    handles->push_back(hwnd);
  }

  return TRUE;
}

int IECommandExecutor::CreateNewBrowser(std::string* error_message) {
  LOG(TRACE) << "Entering IECommandExecutor::CreateNewBrowser";

  DWORD process_id = this->factory_->LaunchBrowserProcess(error_message);
  if (process_id == NULL) {
    LOG(WARN) << "Unable to launch browser, received NULL process ID";
    this->is_waiting_ = false;
    return ENOSUCHDRIVER;
  }

  ProcessWindowInfo process_window_info;
  process_window_info.dwProcessId = process_id;
  process_window_info.hwndBrowser = NULL;
  process_window_info.pBrowser = NULL;
  bool attached = this->factory_->AttachToBrowser(&process_window_info,
                                                  error_message);
  if (!attached) {
    LOG(WARN) << "Unable to attach to browser COM object";
    this->is_waiting_ = false;
    return ENOSUCHDRIVER;
  }

  // Set persistent hover functionality in the interactions implementation.
  //this->input_manager_->StartPersistentEvents();
  LOG(INFO) << "Persistent hovering set to: "
            << this->input_manager_->use_persistent_hover();

  this->proxy_manager_->SetProxySettings(process_window_info.hwndBrowser);
  BrowserHandle wrapper(new Browser(process_window_info.pBrowser,
                                    process_window_info.hwndBrowser,
                                    this->m_hWnd));

  this->AddManagedBrowser(wrapper);
  bool is_busy = wrapper->IsBusy();
  if (is_busy) {
    LOG(WARN) << "Browser was launched and attached to, but is still busy.";
  }
  return WD_SUCCESS;
}

int IECommandExecutor::GetManagedElement(const std::string& element_id,
                                         ElementHandle* element_wrapper) const {
  LOG(TRACE) << "Entering IECommandExecutor::GetManagedElement";
  return this->managed_elements_->GetManagedElement(element_id, element_wrapper);
}

bool IECommandExecutor::AddManagedElement(IHTMLElement* element,
                                          ElementHandle* element_wrapper) {
  LOG(TRACE) << "Entering IECommandExecutor::AddManagedElement";
  BrowserHandle current_browser;
  this->GetCurrentBrowser(&current_browser);
  return this->managed_elements_->AddManagedElement(current_browser, element, element_wrapper);
}

void IECommandExecutor::RemoveManagedElement(const std::string& element_id) {
  LOG(TRACE) << "Entering IECommandExecutor::RemoveManagedElement";
  this->managed_elements_->RemoveManagedElement(element_id);
}

void IECommandExecutor::ListManagedElements() {
  LOG(TRACE) << "Entering IECommandExecutor::ListManagedElements";
  this->managed_elements_->ListManagedElements();
}

int IECommandExecutor::GetElementFindMethod(const std::string& mechanism,
                                            std::wstring* translation) const {
  LOG(TRACE) << "Entering IECommandExecutor::GetElementFindMethod";

  ElementFindMethodMap::const_iterator found_iterator =
      this->element_find_methods_.find(mechanism);
  if (found_iterator == this->element_find_methods_.end()) {
    LOG(WARN) << "Unable to determine find method " << mechanism;
    return EUNHANDLEDERROR;
  }

  *translation = found_iterator->second;
  return WD_SUCCESS;
}

int IECommandExecutor::LocateElement(const ElementHandle parent_wrapper,
                                     const std::string& mechanism,
                                     const std::string& criteria,
                                     Json::Value* found_element) const {
  LOG(TRACE) << "Entering IECommandExecutor::LocateElement";

  std::wstring mechanism_translation = L"";
  int status_code = this->GetElementFindMethod(mechanism,
                                               &mechanism_translation);
  if (status_code != WD_SUCCESS) {
    LOG(WARN) << "Unable to determine mechanism translation for " << mechanism;
    return status_code;
  }

  std::wstring wide_criteria = StringUtilities::ToWString(criteria);
  return this->element_finder()->FindElement(*this,
                                            parent_wrapper,
                                            mechanism_translation,
                                            wide_criteria,
                                            found_element);
}

int IECommandExecutor::LocateElements(const ElementHandle parent_wrapper,
                                      const std::string& mechanism,
                                      const std::string& criteria,
                                      Json::Value* found_elements) const {
  LOG(TRACE) << "Entering IECommandExecutor::LocateElements";

  std::wstring mechanism_translation = L"";
  int status_code = this->GetElementFindMethod(mechanism,
                                               &mechanism_translation);
  if (status_code != WD_SUCCESS) {
    LOG(WARN) << "Unable to determine mechanism translation for " << mechanism;
    return status_code;
  }

  std::wstring wide_criteria = StringUtilities::ToWString(criteria);
  return this->element_finder()->FindElements(*this,
                                             parent_wrapper,
                                             mechanism_translation,
                                             wide_criteria,
                                             found_elements);
}

void IECommandExecutor::PopulateElementFinderMethods(void) {
  LOG(TRACE) << "Entering IECommandExecutor::PopulateElementFinderMethods";

  this->element_find_methods_["id"] = L"id";
  this->element_find_methods_["name"] = L"name";
  this->element_find_methods_["tag name"] = L"tagName";
  this->element_find_methods_["link text"] = L"linkText";
  this->element_find_methods_["partial link text"] = L"partialLinkText";
  this->element_find_methods_["class name"] = L"className";
  this->element_find_methods_["xpath"] = L"xpath";
  this->element_find_methods_["css selector"] = L"css";
}

} // namespace webdriver


text:
---
uid: SolidEdgePart.ReplaceFaces.Item(System.Object)
summary:
remarks:
syntax:
    parameters:
    - id: Index
      description: Variant (input) index.
---

redpajama_set_name: RedPajamaGithub


text:
<?php

/**
 * This file is part of the Spryker Demoshop.
 * For full license information, please view the LICENSE file that was distributed with this source code.
 */

namespace Pyz\Zed\DataImport\Business\Model\Stock;

use Orm\Zed\Stock\Persistence\SpyStockQuery;
use Spryker\Zed\DataImport\Business\Model\DataImportStep\DataImportStepInterface;
use Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface;

class StockWriterStep implements DataImportStepInterface
{
    public const KEY_NAME = 'name';

    /**
     * @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
     *
     * @return void
     */
    public function execute(DataSetInterface $dataSet)
    {
        $stockEntity = SpyStockQuery::create()
            ->filterByName($dataSet[static::KEY_NAME])
            ->findOneOrCreate();

        $stockEntity->save();
    }
}


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace OurCityEngine.Utils {
    /// <summary>
    /// Helper for drawing lines
    /// </summary>
    public class Line {

        static private Texture2D _empty_texture;
        static private bool _set_data = false;

        static public void Init(GraphicsDevice device) {
            _empty_texture = new Texture2D(device, 1, 1, false, SurfaceFormat.Color);
        }

        static public void DrawLine(SpriteBatch batch, Color color,
                                    Vector2 point1, Vector2 point2) {

            DrawLine(batch, color, point1, point2, 0);
        }

        /// <summary>
        /// Draw a line with spritebatch
        /// </summary>
        /// <param name="color">The line color</param>
        /// <param name="point1">Start Point</param>
        /// <param name="point2">End Point</param>
        /// <param name="Layer">Layer or Z position</param>
        static public void DrawLine(SpriteBatch batch, Color color, Vector2 point1,
                                    Vector2 point2, float Layer) {
            //Check if data has been set for texture
            //Do this only once otherwise
            if (!_set_data) {
                _empty_texture.SetData(new[] { Color.White });
                _set_data = true;
            }

            float angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
            float length = (point2 - point1).Length();

            batch.Draw(_empty_texture, point1, null, color,
                       angle, Vector2.Zero, new Vector2(length, 1),
                       SpriteEffects.None, Layer);
        }
    }
}


text:
/**
 * \file rtc-board.c
 * \author Alexander Winiger (alexander.winiger@hslu.ch)
 * \date 21.09.2015
 * \brief MCU RTC timer and low power modes management
 *
 */
#include <math.h>
#include <time.h>
#include "board.h"
#include "rtc-board.h"
#include "fsl_rtc_hal.h"
#include "fsl_clock_manager.h"
#include "fsl_interrupt_manager.h"

#if defined(FSL_RTOS_FREE_RTOS)

void BlockLowPowerDuringTask( bool status )
{
    (void)status;
}
#else

/*----------------------- Local Definitions ------------------------------*/
/*!
 * RTC Time base in us
 */
#define RTC_ALARM_TIME_BASE                             122.07

/*!
 * MCU Wake Up Time
 */
#define MCU_WAKE_UP_TIME                                3400

/*----------------------- Local Functions ------------------------------*/
/*!
 * \brief Start the Rtc Alarm (time base 1s)
 */
static void RtcStartWakeUpAlarm( uint32_t timeoutValue );

/*!
 * \brief Read the MCU internal Calendar value
 *
 * \retval Calendar value
 */
static TimerTime_t RtcGetCalendarValue( void );

/*!
 * \brief Clear the RTC flags and Stop all IRQs
 */
static void RtcClearStatus( void );

/*------------------------ Local Variables -------------------------------*/
/* Table of base addresses for RTC instances. */
RTC_Type * const g_rtcBase[RTC_INSTANCE_COUNT] = RTC_BASE_PTRS;

/*!
 * \brief Indicates if the RTC is already Initalized or not
 */
static bool RtcInitalized = false;

/*!
 * \brief Flag to indicate if the timestamps until the next event is long enough
 * to set the MCU into low power mode
 */
static bool RtcTimerEventAllowsLowPower = false;

/*!
 * \brief Flag to disable the LowPower Mode even if the timestamps until the
 * next event is long enough to allow Low Power mode
 */
static bool LowPowerDisableDuringTask = false;

/*!
 * Keep the value of the RTC timer when the RTC alarm is set
 */
static TimerTime_t RtcTimerContext = 0;

/*!
 * RTC external clock configuration
 */
rtc_osc_user_config_t rtcOscConfig =
{
    .freq = RTC_XTAL_FREQ,
    .enableCapacitor2p = RTC_SC2P_ENABLE_CONFIG,
    .enableCapacitor4p = RTC_SC4P_ENABLE_CONFIG,
    .enableCapacitor8p = RTC_SC8P_ENABLE_CONFIG,
    .enableCapacitor16p = RTC_SC16P_ENABLE_CONFIG,
    .enableOsc = RTC_OSC_ENABLE_CONFIG,
};

void RtcInit( void )
{
    if ( RtcInitalized == false ) {
        rtc_datetime_t datetime;
        uint32_t srcClock = 0;
        uint32_t seconds = 0;
        uint16_t preScaler = 0;
        uint64_t tmp = 0;

        /* Enable clock gate to RTC module */
        CLOCK_SYS_EnableRtcClock(0U);

        /* Initialize the general configuration for RTC module.*/
        RTC_HAL_Init (RTC_BASE_PTR);

        /* Clear pending interrupts before enabling them */
        NVIC_ClearPendingIRQ (RTC_IRQn);
        INT_SYS_EnableIRQ(RTC_IRQn);

        /* Configure RTC external clock */
        CLOCK_SYS_RtcOscInit(0U, &rtcOscConfig);

        // Set a start date time and start RT.
        datetime.year = 2014U;
        datetime.month = 12U;
        datetime.day = 25U;
        datetime.hour = 19U;
        datetime.minute = 0;
        datetime.second = 0;

        RTC_HAL_ConvertDatetimeToSecs(&datetime, &seconds);

        if ( (srcClock = CLOCK_SYS_GetRtcFreq(0U)) != 32768U ) {
            /* As the seconds register will not increment every second, we need to adjust the value
             * programmed to the seconds register */
            tmp = (uint64_t) seconds * (uint64_t) srcClock;
            preScaler = (uint32_t)(tmp & 0x7FFFU);
            seconds = (uint32_t)(tmp >> 15U);
        }
        /* Set time in seconds */
        RTC_HAL_EnableCounter(RTC_BASE_PTR, false);
        /* Set seconds counter*/
        RTC_HAL_SetSecsReg(RTC_BASE_PTR, seconds);
        /* Set time counter*/
        RTC_HAL_SetPrescaler(RTC_BASE_PTR, preScaler);
        /* Enable the counter*/
        RTC_HAL_EnableCounter(RTC_BASE_PTR, true);
        RtcInitalized = true;
    }
}

void RtcStopTimer( void )
{
    RtcClearStatus();
}

uint32_t RtcGetMinimumTimeout( void )
{
    return (ceil(3 * RTC_ALARM_TIME_BASE));
}

void RtcSetTimeout( uint32_t timeout )
{
    uint32_t timeoutValue = 0;

    timeoutValue = timeout;

    if ( timeoutValue < (3 * RTC_ALARM_TIME_BASE) ) {
        timeoutValue = 3 * RTC_ALARM_TIME_BASE;
    }

    if ( timeoutValue < 55000 ) {
        // we don't go in Low Power mode for delay below 50ms (needed for LEDs)
        RtcTimerEventAllowsLowPower = false;
    } else {
        RtcTimerEventAllowsLowPower = true;
    }

    if ( (LowPowerDisableDuringTask == false) && (RtcTimerEventAllowsLowPower == true) ) {
        timeoutValue = timeoutValue - MCU_WAKE_UP_TIME;
    }

    RtcStartWakeUpAlarm(timeoutValue);
}

uint32_t RtcGetTimerElapsedTime( void )
{
    TimerTime_t CalendarValue = 0;

    CalendarValue = RtcGetCalendarValue();

    return ((uint32_t)(ceil(((CalendarValue - RtcTimerContext) + 2) * RTC_ALARM_TIME_BASE)));
}

TimerTime_t RtcGetTimerValue( void )
{
    TimerTime_t CalendarValue = 0;

    CalendarValue = RtcGetCalendarValue();

    return ((CalendarValue + 2) * RTC_ALARM_TIME_BASE);
}

static void RtcClearStatus( void )
{
    /* Deactivate the Alarm interrupt */
    RTC_HAL_SetAlarmIntCmd(RTC_BASE_PTR, false);
}

static void RtcStartWakeUpAlarm( uint32_t timeoutValue )
{
    uint32_t srcClock = 0;
    uint32_t alrmSeconds = 0;
    uint32_t currSeconds = 0;
    uint64_t tmp = 0;

    RtcClearStatus();

    RtcTimerContext = RtcGetCalendarValue();

    /* Get the current time */
    currSeconds = RTC_HAL_GetSecsReg(RTC_BASE_PTR);
    alrmSeconds = timeoutValue / RTC_ALARM_TIME_BASE;

    if ( (srcClock = CLOCK_SYS_GetRtcFreq(0)) != 32768U ) {
        /* As the seconds register will not increment every second, we need to adjust the value
         * programmed to the alarm register */
        tmp = (uint64_t) alrmSeconds * (uint64_t) srcClock;
        alrmSeconds = (uint32_t)(tmp >> 15U);
    }

    alrmSeconds += currSeconds;

    /* set alarm in seconds*/
    RTC_HAL_SetAlarmReg(RTC_BASE_PTR, alrmSeconds);

    /* Activate the Alarm interrupt */
    RTC_HAL_SetAlarmIntCmd(RTC_BASE_PTR, true);
}

void RtcEnterLowPowerStopMode( void )
{
    if ( (LowPowerDisableDuringTask == false) && (RtcTimerEventAllowsLowPower == true) ) {
        // Disable IRQ while the MCU is being deinitialized to prevent race issues
        INT_SYS_DisableIRQGlobal();
//      \todo Implement RtcEnterLowPowerStopMode
//        BoardDeInitMcu();

        INT_SYS_EnableIRQGlobal();
    }
}

void RtcRecoverMcuStatus( void )
{
    if ( TimerGetLowPowerEnable() == true ) {
        if ( (LowPowerDisableDuringTask == false) && (RtcTimerEventAllowsLowPower == true) ) {
            // Disable IRQ while the MCU is not running on HSE
            INT_SYS_DisableIRQGlobal();

//          \todo Implement RtcRecoverMcuStatus
//            BoardInitMcu();

            INT_SYS_EnableIRQGlobal();
        }
    }
}

/*!
 * \brief RTC IRQ Handler on the RTC Alarm
 */
void RTC_IRQHandler( void )
{
    if ( RTC_HAL_HasAlarmOccured (RTC_BASE_PTR) ) {
//        RtcRecoverMcuStatus();

        TimerIrqHandler();

        /* Writing to the alarm register clears the TAF flag in the Status register */
        RTC_HAL_SetAlarmReg(RTC_BASE_PTR, 0x0);
        RTC_HAL_SetAlarmIntCmd(RTC_BASE_PTR, false);
    }
}

/*!
 * \brief RTC IRQ Handler on the RTC Seconds interrupt
 */
void RTC_Seconds_IRQHandler( void )
{
    /* Disables the time seconds interrupt (TSIE) bit. */
    RTC_HAL_SetSecsIntCmd(RTC_BASE_PTR, false);
}

void BlockLowPowerDuringTask( bool status )
{
    if ( status == true ) {
        RtcRecoverMcuStatus();
    }
    LowPowerDisableDuringTask = status;
}

void RtcDelayMs( uint32_t delay )
{
    TimerTime_t delayValue = 0;
    TimerTime_t timeout = 0;

    delayValue = (TimerTime_t)(delay * 1000);

    // Wait delay ms
    timeout = RtcGetTimerValue();
    while (((RtcGetTimerValue() - timeout)) < delayValue) {
        __NOP();
    }
}

TimerTime_t RtcGetCalendarValue( void )
{
    uint32_t seconds = 0;
    uint32_t srcClock = 0;
    uint64_t tmp = 0;

    RTC_HAL_GetDatetimeInSecs(RTC_BASE_PTR, &seconds);

    if ( (srcClock = CLOCK_SYS_GetRtcFreq(0U)) != 32768U ) {
        /* In case the input clock to the RTC counter is not 32KHz, the seconds register will not
         * increment every second, therefore the seconds register value needs to be adjusted.
         * to get actual seconds. We then add the prescaler register value to the seconds.
         */
        tmp = (uint64_t) seconds << 15U;
        tmp |= (uint64_t)(RTC_HAL_GetPrescaler(RTC_BASE_PTR) & 0x7FFFU);
        seconds = tmp / srcClock;
    }

    return (TimerTime_t) seconds;
}
#endif


text:
struct IFileChangeListener
{
	virtual void OnFileChange( const char* sFilename ) = 0;
protected:
  virtual ~IFileChangeListener() {}
};

struct IFileChangeMonitor
{
	// Register the path of a file or directory to monitor
	// Path is relative to game directory, e.g. "Libs/WoundSystem/" or "Libs/WoundSystem/HitLocations.xml"
	virtual bool RegisterListener(IFileChangeListener *pListener, const char* sMonitorItem ) = 0;
	virtual bool UnregisterListener(IFileChangeListener *pListener) = 0;

protected:
  virtual ~IFileChangeMonitor() {}
};

#endif //_IFILECHANGEMONITOR_H_


text:
from sys import argv
from os.path import exists

script, from_file, to_file = argv

print ("Copying from %s to %s" % (from_file, to_file))

# we could do these two on one line, how?
in_file = open(from_file)
indata = in_file.read()

# The above could be done in one line via:
#indata = open(from_file).read()

print ("The input file is %d bytes long" % len(indata))

print ("Does the output file exist? %r" % exists(to_file))
print ("Ready, hit RETURN to continue, CTRL-C to abort.")
input()

out_file = open(to_file, 'w')
out_file.write(indata)

print ("Alright, all done")

out_file.close()
in_file.close()


text:
set -e -o pipefail

# Set up env variables for production
export AIO_GITHUB_TOKEN=$(head -c -1 /aio-secrets/GITHUB_TOKEN 2>/dev/null)
export AIO_PREVIEW_DEPLOYMENT_TOKEN=$(head -c -1 /aio-secrets/PREVIEW_DEPLOYMENT_TOKEN 2>/dev/null)

# Start the upload-server instance
# TODO(gkalpak): Ideally, the upload server should be run as a non-privileged user.
#                (Currently, there doesn't seem to be a straight forward way.)
action=$([ "$1" == "stop" ] && echo "stop" || echo "start")
pm2 $action $AIO_SCRIPTS_JS_DIR/dist/lib/upload-server \
  --log /var/log/aio/upload-server-prod.log \
  --name aio-upload-server-prod \
  ${@:2}


text:
<?php
declare(strict_types=1);

namespace Gdbots\Iam;

use Gdbots\Ncr\NcrProjector;

class NcrUserProjector extends NcrProjector
{
    public static function getSubscribedEvents()
    {
        return [
            'gdbots:iam:event:user-roles-granted' => 'onNodeEvent',
            'gdbots:iam:event:user-roles-revoked' => 'onNodeEvent',

            // deprecated mixins, will be removed in 3.x
            'gdbots:iam:mixin:user-roles-granted' => 'onNodeEvent',
            'gdbots:iam:mixin:user-roles-revoked' => 'onNodeEvent',
        ];
    }
}


text:
package com.lothrazar.cyclic.block.cable.item;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import com.google.common.collect.Maps;
import com.lothrazar.cyclic.block.TileBlockEntityCyclic;
import com.lothrazar.cyclic.block.cable.CableBase;
import com.lothrazar.cyclic.block.cable.EnumConnectType;
import com.lothrazar.cyclic.registry.ItemRegistry;
import com.lothrazar.cyclic.registry.TileRegistry;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class TileCableItem extends TileBlockEntityCyclic implements MenuProvider {

  private static final int FLOW_QTY = 64; // fixed, for non-extract motion
  private int extractQty = 64; // default
  ItemStackHandler filter = new ItemStackHandler(1) {

    @Override
    public boolean isItemValid(int slot, ItemStack stack) {
      return stack.getItem() == ItemRegistry.FILTER_DATA.get();
    }
  };
  private Map<Direction, LazyOptional<IItemHandler>> flow = Maps.newHashMap();

  public TileCableItem(BlockPos pos, BlockState state) {
    super(TileRegistry.ITEM_PIPE.get(), pos, state);
    for (Direction f : Direction.values()) {
      flow.put(f, LazyOptional.of(TileCableItem::createHandler));
    }
  }

  public static void serverTick(Level level, BlockPos blockPos, BlockState blockState, TileCableItem e) {
    e.tick();
  }

  public static <E extends BlockEntity> void clientTick(Level level, BlockPos blockPos, BlockState blockState, TileCableItem e) {
    e.tick();
  }

  private static ItemStackHandler createHandler() {
    return new ItemStackHandler(1);
  }

  List<Integer> rawList = IntStream.rangeClosed(
      0,
      5).boxed().collect(Collectors.toList());

  //  @Override
  public void tick() {
    for (Direction extractSide : Direction.values()) {
      EnumConnectType connection = this.getBlockState().getValue(CableBase.FACING_TO_PROPERTY_MAP.get(extractSide));
      if (connection.isExtraction()) {
        IItemHandler sideHandler = flow.get(extractSide).orElse(null);
        tryExtract(sideHandler, extractSide, extractQty, filter);
      }
    }
    normalFlow();
  }

  private void normalFlow() {
    IItemHandler sideHandler;
    Direction outgoingSide;
    for (Direction incomingSide : Direction.values()) {
      sideHandler = flow.get(incomingSide).orElse(null);
      //thise items came from that
      Collections.shuffle(rawList);
      boolean validAdjacent = false;
      for (Integer i : rawList) {
        outgoingSide = Direction.values()[i];
        if (outgoingSide == incomingSide) {
          continue;
        }
        EnumConnectType connection = this.getBlockState().getValue(CableBase.FACING_TO_PROPERTY_MAP.get(outgoingSide));
        if (connection.isExtraction() || connection.isBlocked()) {
          continue;
        }
        validAdjacent = validAdjacent || this.moveItems(outgoingSide, 64, sideHandler);
      }
      if (!validAdjacent) {
        this.moveItems(incomingSide, FLOW_QTY, sideHandler);
      }
    }
  }

  @Override
  public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
    if (side != null && cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
      if (!CableBase.isCableBlocked(this.getBlockState(), side)) {
        return flow.get(side).cast();
      }
    }
    return super.getCapability(cap, side);
  }

  @SuppressWarnings("unchecked")
  @Override
  public void load(CompoundTag tag) {
    extractQty = tag.getInt("extractCount");
    LazyOptional<IItemHandler> item;
    for (Direction f : Direction.values()) {
      item = flow.get(f);
      item.ifPresent(h -> {
        CompoundTag itemTag = tag.getCompound("item" + f.toString());
        ((INBTSerializable<CompoundTag>) h).deserializeNBT(itemTag);
      });
    }
    filter.deserializeNBT(tag.getCompound("filter"));
    super.load(tag);
  }

  @SuppressWarnings("unchecked")
  @Override
  public void saveAdditional(CompoundTag tag) {
    tag.put("filter", filter.serializeNBT());
    tag.putInt("extractCount", extractQty);
    LazyOptional<IItemHandler> item;
    for (Direction f : Direction.values()) {
      item = flow.get(f);
      item.ifPresent(h -> {
        CompoundTag compound = ((INBTSerializable<CompoundTag>) h).serializeNBT();
        tag.put("item" + f.toString(), compound);
      });
    }
    super.saveAdditional(tag);
  }

  @Override
  public void setField(int field, int value) {
    this.extractQty = value;
  }

  @Override
  public int getField(int field) {
    return this.extractQty;
  }

  @Override
  public Component getDisplayName() {
    return new TextComponent(getType().getRegistryName().getPath());
  }

  @Override
  public AbstractContainerMenu createMenu(int i, Inventory playerInventory, Player playerEntity) {
    return new ContainerCableItem(i, level, worldPosition, playerInventory, playerEntity);
  }
}


text:
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.ai.metricsadvisor.models;

import com.azure.core.annotation.Fluent;

/**
 * Defines alerting settings for anomalies detected by a detection
 * configuration.
 */
@Fluent
public final class MetricAnomalyAlertConfiguration {
    private final String detectionConfigurationId;
    private boolean negationOperation;
    private MetricAnomalyAlertScope alertScope;
    private MetricAnomalyAlertConditions alertConditions;
    private MetricAnomalyAlertSnoozeCondition snoozeCondition;

    /**
     * Creates a new instance of MetricAnomalyAlertConfiguration.
     *
     * @param detectionConfigurationId The anomaly detection configuration id.
     * @param alertScope The scope for the alert.
     */
    public MetricAnomalyAlertConfiguration(String detectionConfigurationId,
        MetricAnomalyAlertScope alertScope) {
        this(detectionConfigurationId, alertScope, false);
    }

    /**
     * Creates a new instance of MetricAnomalyAlertConfiguration.
     *
     * @param detectionConfigurationId The anomaly detection configuration id.
     * @param alertScope The scope for the alert.
     * @param negationOperation if true anomalies in this configuration will not be included in alerts,
     *     instead result of applying the configuration become an expression to take part in the operation
     *     performed among corresponding series in other configurations. The result of expression is true
     *     only if there is no anomaly found in the series.
     */
    public MetricAnomalyAlertConfiguration(String detectionConfigurationId,
                                           MetricAnomalyAlertScope alertScope,
                                           boolean negationOperation) {
        this.detectionConfigurationId = detectionConfigurationId;
        this.negationOperation = negationOperation;
        this.alertScope =  alertScope;
    }

    /**
     * Gets the anomaly detection configuration id.
     *
     * @return The detection configuration id.
     */
    public String getDetectionConfigurationId() {
        return this.detectionConfigurationId;
    }

    /**
     * True if this configuration is used only as an expression to take part in the operation
     * performed among corresponding series in other configurations.
     *
     * @return The negation operation value.
     */
    public boolean isNegationOperationEnabled() {
        return this.negationOperation;
    }

    /**
     * Gets the alert scope.
     *
     * @return The alert scope.
     */
    public MetricAnomalyAlertScope getAlertScope() {
        return this.alertScope;
    }

    /**
     * Gets the conditions applied on the detected anomalies to decide whether it should be included
     * in the alert or not.
     *
     * @return The alert conditions.
     */
    public MetricAnomalyAlertConditions getAlertConditions() {
        return this.alertConditions;
    }

    /**
     * Gets the condition to snooze the inclusion of anomalies in the upcoming alerts.
     *
     * @return The snooze condition.
     */
    public MetricAnomalyAlertSnoozeCondition getAlertSnoozeCondition() {
        return this.snoozeCondition;
    }

    /**
     * Sets the negation value.
     *
     * if true anomalies in this configuration will not be included in alerts,
     * instead result of applying the configuration become an expression to take part in the operation
     * performed among corresponding series in other configurations. The result of expression is true
     * only if there is no anomaly found in the series.
     *
     * @param negationOperation The negation operation value.
     * @return The MetricAnomalyAlertConfiguration object itself.
     */
    public MetricAnomalyAlertConfiguration setNegationOperation(boolean negationOperation) {
        this.negationOperation = negationOperation;
        return this;
    }

    /**
     * Sets the alert scope.
     *
     * @param alertScope The alert scope.
     * @return The MetricAnomalyAlertConfiguration object itself.
     */
    public MetricAnomalyAlertConfiguration setScopeOfAlertTo(MetricAnomalyAlertScope alertScope) {
        this.alertScope = alertScope;
        return this;
    }

    /**
     * Sets the conditions to be applied on the detected anomalies to decide whether it should be included
     * in the alert or not.
     *
     * @param alertConditions The alert conditions.
     * @return The MetricAnomalyAlertConfiguration object itself.
     */
    public MetricAnomalyAlertConfiguration setAlertConditions(MetricAnomalyAlertConditions alertConditions) {
        this.alertConditions = alertConditions;
        return this;
    }

    /**
     * Sets the snooze condition. Once anomalies are alerted, this condition will be used to snooze the inclusion
     * of anomalies in upcoming alerts.
     *
     * @param snoozeCondition The snooze condition.
     * @return The MetricAnomalyAlertConfiguration object itself.
     */
    public MetricAnomalyAlertConfiguration setAlertSnoozeCondition(MetricAnomalyAlertSnoozeCondition snoozeCondition) {
        this.snoozeCondition = snoozeCondition;
        return this;
    }
}


text:
require 'puppet'
require 'puppetlabs_spec_helper/module_spec_helper'
require 'rspec-puppet-facts'

include RspecPuppetFacts
Puppet::Util::Log.level = :debug
Puppet::Util::Log.newdestination(:console)

fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures'))
RSpec.configure do |c|
  #c.treat_symbols_as_metadata_keys_with_true_values = true
  c.include PuppetlabsSpec::Files
  c.hiera_config = 'spec/fixtures/hiera/hiera.yaml'
  c.module_path = File.join(fixture_path, 'modules')
  c.manifest_dir = File.join(fixture_path, 'manifests')
  c.template_dir = File.join(fixture_path, 'templates')
end

at_exit { RSpec::Puppet::Coverage.report! }


text:
package at.knallgrau.textcat;

import java.util.Comparator;
import java.util.Map.Entry;

/**
 * @author Thomas Hammerl
 *
 * Comparator for NGram entries in the FingerPrint collection. sorts a set of
 * Entry<String,Integer> entries in a FingerPrint collection in descending
 * order.
 *
 */
class NGramEntryComparator implements Comparator<Entry<String, Integer>> {

    /**
         * @param entry1
         *                an NGram entry
         * @param entry2
         *                another NGram entry
         * @return a number indicating the relation of entry1 to entry2
         */
    public int compare(Entry<String, Integer> entry1,
	    Entry<String, Integer> entry2) {
	if (entry2.getValue() - entry1.getValue() == 0) {
	    if (entry1.getKey().length() - entry2.getKey().length() == 0) {
		return entry1.getKey().compareTo(entry2.getKey());
	    }
	    return entry1.getKey().length() - entry2.getKey().length();
	}
	return entry2.getValue() - entry1.getValue();
    }

}


text:
const Discord = require('discord.js');
const client = new Discord.Client
const fs = require('fs')
client.commands = new Discord.Collection()
const moment = require('moment');

module.exports.run = async (client, message, args) => {
	exports.run = async (client, message, args) => {
			if (!message.guild.available) {
					try{await message.channel.send('Discord API Error');}catch(e) {logger.error(e)}
					return;
			}
			let guild = message.guild;
			let name = guild.name;
			let createdAt = moment(guild.createdAt).format('MMMM Do YYYY, h:mm:ss a');
			let channels = guild.channels.size;
			let owner = guild.owner.user.tag;
			let memberCount = guild.memberCount;
			let large = guild.large;
			let iconUrl = guild.iconURL;
			let region = guild.region;
			let afk = message.guild.channels.get(guild.afkChannelID) === undefined ? 'None' : message.guild.channels.get(guild.afkChannelID).name;

			const embed = new Discord.RichEmbed()
					.setTitle('Guild Information')
					.addField('Channels', `**Channel Count:** ${channels}\n**AFK Channel:** ${afk}`, true)
					.addField('Members', `**Member Count:** ${memberCount}\n**Owner:** ${owner}\n**Owner ID:** ${guild.owner.id}`, true)
					.addField('More', `**Created at:** ${createdAt}\n**Large Guild?:** ${large ? 'Yes' : 'No'}\n**Region:** ${region}`)
					.setThumbnail(iconUrl)
					.setFooter('May')
					.setTimestamp();

			try{await message.channel.send({embed});}catch(e){logger.error(e)}
	};
}

module.exports.help = {
    name: "guildinfo",
		botPerm: ['Official Bots']
}


text:
from django.db import migrations, models

class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Organization',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('company', models.CharField(max_length=100, unique=True)),
                ('description', models.TextField()),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
            ],
        ),
    ]


text:
package Paws::CodePipeline::ThirdPartyJobData;
  use Moose;
  has ActionConfiguration => (is => 'ro', isa => 'Paws::CodePipeline::ActionConfiguration', request_name => 'actionConfiguration', traits => ['NameInRequest']);
  has ActionTypeId => (is => 'ro', isa => 'Paws::CodePipeline::ActionTypeId', request_name => 'actionTypeId', traits => ['NameInRequest']);
  has ArtifactCredentials => (is => 'ro', isa => 'Paws::CodePipeline::AWSSessionCredentials', request_name => 'artifactCredentials', traits => ['NameInRequest']);
  has ContinuationToken => (is => 'ro', isa => 'Str', request_name => 'continuationToken', traits => ['NameInRequest']);
  has EncryptionKey => (is => 'ro', isa => 'Paws::CodePipeline::EncryptionKey', request_name => 'encryptionKey', traits => ['NameInRequest']);
  has InputArtifacts => (is => 'ro', isa => 'ArrayRef[Paws::CodePipeline::Artifact]', request_name => 'inputArtifacts', traits => ['NameInRequest']);
  has OutputArtifacts => (is => 'ro', isa => 'ArrayRef[Paws::CodePipeline::Artifact]', request_name => 'outputArtifacts', traits => ['NameInRequest']);
  has PipelineContext => (is => 'ro', isa => 'Paws::CodePipeline::PipelineContext', request_name => 'pipelineContext', traits => ['NameInRequest']);
1;

### main pod documentation begin ###

=head1 NAME

Paws::CodePipeline::ThirdPartyJobData

=head1 USAGE

This class represents one of two things:

=head3 Arguments in a call to a service

Use the attributes of this class as arguments to methods. You shouldn't make instances of this class.
Each attribute should be used as a named argument in the calls that expect this type of object.

As an example, if Att1 is expected to be a Paws::CodePipeline::ThirdPartyJobData object:

  $service_obj->Method(Att1 => { ActionConfiguration => $value, ..., PipelineContext => $value  });

=head3 Results returned from an API call

Use accessors for each attribute. If Att1 is expected to be an Paws::CodePipeline::ThirdPartyJobData object:

  $result = $service_obj->Method(...);
  $result->Att1->ActionConfiguration

=head1 DESCRIPTION

Represents information about the job data for a partner action.

=head1 ATTRIBUTES

=head2 ActionConfiguration => L<Paws::CodePipeline::ActionConfiguration>

  Represents information about an action configuration.

=head2 ActionTypeId => L<Paws::CodePipeline::ActionTypeId>

  Represents information about an action type.

=head2 ArtifactCredentials => L<Paws::CodePipeline::AWSSessionCredentials>

  Represents an AWS session credentials object. These credentials are
temporary credentials that are issued by AWS Secure Token Service
(STS). They can be used to access input and output artifacts in the
Amazon S3 bucket used to store artifact for the pipeline in AWS
CodePipeline.

=head2 ContinuationToken => Str

  A system-generated token, such as a AWS CodeDeploy deployment ID, that
a job requires in order to continue the job asynchronously.

=head2 EncryptionKey => L<Paws::CodePipeline::EncryptionKey>

  The encryption key used to encrypt and decrypt data in the artifact
store for the pipeline, such as an AWS Key Management Service (AWS KMS)
key. This is optional and might not be present.

=head2 InputArtifacts => ArrayRef[L<Paws::CodePipeline::Artifact>]

  The name of the artifact that will be worked upon by the action, if
any. This name might be system-generated, such as "MyApp", or might be
defined by the user when the action is created. The input artifact name
must match the name of an output artifact generated by an action in an
earlier action or stage of the pipeline.

=head2 OutputArtifacts => ArrayRef[L<Paws::CodePipeline::Artifact>]

  The name of the artifact that will be the result of the action, if any.
This name might be system-generated, such as "MyBuiltApp", or might be
defined by the user when the action is created.

=head2 PipelineContext => L<Paws::CodePipeline::PipelineContext>

  Represents information about a pipeline to a job worker.

=head1 SEE ALSO

This class forms part of L<Paws>, describing an object used in L<Paws::CodePipeline>

=head1 BUGS and CONTRIBUTIONS

The source code is located here: https://github.com/pplu/aws-sdk-perl

Please report bugs to: https://github.com/pplu/aws-sdk-perl/issues

=cut


text:
module Excalibur
  # the Duplicator module helps in making sure duplication of nested objects
  # functions properly and is used by the class Configuration and the class
  # TruncableContent
  module Duplicator
    # duplicates TruncatableContent and calls the deep_dup method on any form
    # of Hash. Otherwise return the object as not all objects do not need to
    # be duplicated.
    def dup_instance(obj)
      if obj.is_a?(TruncateableContent)
        obj.dup
      elsif obj.is_a?(Hash)
        obj.deep_dup
      else
        obj
      end
    end
  end
end


text:
<?php

namespace kalibao\common\models\brand;

use Yii;
use kalibao\common\models\product\Product;

/**
 * This is the model class for table "brand".
 *
 * @property integer $id
 * @property string $name
 *
 * @property Product[] $products
 *
 * @package kalibao\common\models\brand
 * @version 1.0
 */
class Brand extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'brand';
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        return [
            'insert' => [
                'name'
            ],
            'update' => [
                'name'
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('kalibao.backend','label_brand_id'),
            'name' => Yii::t('kalibao.backend','label_brand_name'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getProducts()
    {
        return $this->hasMany(Product::className(), ['brand_id' => 'id']);
    }
}